【问题标题】:Why does it execute two 'else if' statements?为什么它执行两个“else if”语句?
【发布时间】:2021-03-20 19:59:02
【问题描述】:

我采用了一段较大的代码来强调。该代码工作正常(良好的输出),但我有一个问题。在下面的代码中,为什么会出现两个“else if”语句正在执行?我了解到拥有一系列“if”语句与“else if”之间的区别在于所有“if”语句都被执行,但是当使用一系列“else if”语句时,当一个语句被执行时,程序不会在该线程上继续。看来输入 1 1 2 2 会执行下面的两个代码,但它不会只执行第一个“else if”语句吗?如果是这样,为什么会有 ixlarge = 4 的输出?

          else if (x3 >= x4 && x3 >= x2 && x3 >= x1)
              xlarge = x3;
          else if(x4 >= x3 && x4 >= x2 && x4 >= x1)
              xlarge = x4;

#include <stdio.h>

int main( )
{
    int x1, x2, x3, x4;
    int xlarge, xsmall, ixlarge, ixsmall;

    while( 1 )
    {
        printf( "enter x1, x2, x3, x4:\n" );
        scanf( "%d%d%d%d", &x1, &x2, &x3, &x4 );

          xlarge = -1;
          xsmall = -1;
          if(x1 >= x2 && x1 >= x3 && x1 >= x4 )
              xlarge = x1;
          else if((x2 >= x3 && x2 >= x4 && x2 >= x1))
              xlarge = x2;
          else if (x3 >= x4 && x3 >= x2 && x3 >= x1)
              xlarge = x3;
          else if(x4 >= x3 && x4 >= x2 && x4 >= x1)
              xlarge = x4;

        printf("%d",xlarge);

          if(x1 == xlarge)
              ixlarge = 1;
          if(x2 == xlarge)
              ixlarge = 2;
          if(x3 == xlarge)
              ixlarge = 3;
          if(x4 == xlarge)
              ixlarge = 4;
        printf("%d",ixlarge);
         /*if(x3 == xlarge && x4 == xlarge)
             ixlarge = 4;*/


          if(x1 <= x2 && x1 <= x3 && x1 <= x4 )
              xsmall = x1;
          else if((x2 <= x3 && x2 <= x4 && x2 <= x1))
              xsmall = x2;
          else if (x3 <= x4 && x3 <= x2 && x3 <= x1)
              xsmall = x3;
          else if(x4 <= x3 && x4 <= x2 && x4 <= x1)
              xsmall = x4;

          
        
          if(x1 == xsmall)
              ixsmall = 1;
          if(x2 == xsmall)
              ixsmall = 2;
          if(x3 == xsmall)
              ixsmall = 3;
          if(x4 == xsmall)
              ixsmall = 4;
          if(x3 == xsmall && x4 == xsmall)
              ixsmall = 3;
          if(x2 == xsmall && x1 == xsmall)
              ixsmall = 1;
          if(x1 == xsmall && x3 == xsmall)
              ixsmall = 1;
          if(x1 == xsmall && x4 == xsmall)
              ixsmall = 1;
          if(x2 == xsmall && x3 == xsmall)
              ixsmall = 2;
          if(x2 == xsmall && x4 == xsmall)
              ixsmall = 2;
          if(x1 == xsmall && x3 == xsmall && x2 == xsmall)
              ixsmall = 1;
          if(x1 == xsmall && x4 == xsmall && x2 == xsmall)
              ixsmall = 1;

        printf( "largest = %4d at position %d, ", xlarge, ixlarge );
        printf( "smallest = %4d at position %d\n", xsmall, ixsmall );
    }

    return 0;
}

【问题讨论】:

  • 您是否尝试过使用调试器逐语句逐句执行代码,看看真正会发生什么?
  • 您正在查看错误的代码部分。您突出显示的内容将设置xlarge = x3,即xlarge = 2。但是您的问题是关于如何设置ixlarge。查看以if(x1 == xlarge) 开头的代码部分,注意那里有no else 块。另请注意,由于x3x4 具有相同的值,所以表达式x3 == xlargex4 == xlarge 都是正确的。
  • 不是else if。是 if 语句将 ixlarge 设置为 3,然后设置为 4。为了便于调试,在每个 ifelse ifelse 中放置一个 printf 以查看程序流程。跨度>
  • 顺便说一句,我希望这段冗长乏味的代码能成为学习循环的良好动力。
  • 我重申我关于使用调试器的观点...如果您使用调试器单步执行代码,您可能会在比编写问题更短的时间内发现问题本身。能够调试自己的代码是任何程序员的关键强制性知识。

标签: c xcode


【解决方案1】:

输入“1 1 2 2”时,x3x4 的值都是 2,所以这个块:

      else if (x3 >= x4 && x3 >= x2 && x3 >= x1)
          xlarge = x3;

是被执行的那个,尽管如果另一个被执行,xlarge 仍然具有相同的值。表示x4最大的部分是这样的:

      if(x3 == xlarge)
          ixlarge = 3;
      if(x4 == xlarge)
          ixlarge = 4;

因为x3x4 具有相同的值,所以这两个if 块都会执行,而ixlarge 的值是4。

您可以通过更改这组 if 语句来解决此问题:

      if(x1 == xlarge)
          ixlarge = 1;
      if(x2 == xlarge)
          ixlarge = 2;
      if(x3 == xlarge)
          ixlarge = 3;
      if(x4 == xlarge)
          ixlarge = 4;

if/else:

      if(x1 == xlarge)
          ixlarge = 1;
      else if(x2 == xlarge)
          ixlarge = 2;
      else if(x3 == xlarge)
          ixlarge = 3;
      else if(x4 == xlarge)
          ixlarge = 4;

并在设置ixsmall时执行类似的更改。

在侧节点上,如果您使用数组和循环而不是 4 个单独的变量,则可以使您的代码大大更简单且不易出错。

【讨论】:

  • 如何为 x4 分配 xlarge?它只执行了您显示的第一个“if else”,因此只有 x3 被分配给 xlarge。那么你怎么能运行这两个 if 语句呢?该程序对您显示的第二个 if 语句视而不见,因为 x4 从未被分配 xlarge。也许我想得太狭隘了,因为需要分配一个值,否则它来自哪里?看起来就像是凭空而来。
  • @JoshMcCloud x4 没有被分配给xlargex3 是。但是,x3x4 具有相同的值。因此,x3 == xlarge 为真,x4 == xlarge 为真。前者将ixlarge 设置为 3,然后后者将其再次设置为 4。
  • 它如何知道它具有相同的值?它是我们看不到的自动的东西吗?
  • @JoshMcCloud 因为您为两者都输入了值 2。所以在初始 if/else 块之后,x3x4xlarge 的值都为 2。
  • @JoshMcCloud 没有“自动绑定” 您将 x3 设置为 2,然后将 x4 设置为 2。然后将 xlarge 设置为 x3 的值,即 2。所以现在x3 是 2,x4 是 2,xlarge 是 2。
猜你喜欢
  • 2020-03-24
  • 2017-08-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-22
相关资源
最近更新 更多