程序使用nested-if查找四个数字中的最大值

问题描述 投票:0回答:1

先生/女士,我不知道我做错了什么。从逻辑上讲,一切对我看来都是正确的。大括号似乎也放置在正确的位置。那为什么我收到以下错误消息?

32 8 [错误]预期在'('标记之前的构造函数,析构函数或类型转换33 2 [错误]在“返回”之前预期的不合格ID34 1 [错误]预期在'}'标记之前的声明

此特定程序我在做什么错?而且,我希望使用相同的语句而不是其他任何语句进行更正。

请帮助!

程序:

#include <stdio.h>
main()
{
    int a, b, c, d, big;
    printf("\n Enter value to a, b, c, d: ");
    scanf("%d %d %d %d", &a, &b, &c, &d);
    if (a >b)
    {
        if(a>c)
        {
            if(a>d)
            big = a;
            else
            big=d;
        }
        else
           {
           if(c>d)
            big = c;
            else
            big = d;
        }
        else
            if(b>c)
            {
                if(b>d)
                big = b;
                else
                big = d;
            }
    }
    printf("\n The biggest number is %d", big);
    return 0;
}
c nested-if
1个回答
-1
投票

a <= b或它放置在错误的位置(在a < b块内部时,您没有考虑这种情况。

您可以想象决策树中的问题类似于this(3个数字,更清楚。)>

#include <stdio.h>

main() {
  int a, b, c, d, big;
  printf("\n Enter value to a, b, c, d: ");
  scanf("%d %d %d %d", &a, &b, &c, &d);

  if (a > b) {
    if (a > c) {
      if (a > d)
        big = a;
      else
        big = d;
    } else {
      if (c > d)
        big = c;
      else
        big = d;
    }

  } else {
    if (b > c) {
      if (b > d)
        big = b;
      else
        big = d;
    }
    else {
      if (c > d)
        big = c;
      else
        big = d;
    }
  }

  printf("\n The biggest number is %d", big);

  return 0;
}
© www.soinside.com 2019 - 2024. All rights reserved.