为什么它执行两个'else if'语句?

时间:2021-03-20 19:59:02

标签: c xcode

我采用了一段较大的代码来强调。代码工作正常(良好的输出),但我有一个问题。在下面的代码中,为什么看起来正在执行两个“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;
}

1 个答案:

答案 0 :(得分:2)

输入“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 个单独的变量,您的代码可以更加更简单且不易出错。