C#语句编写“求三个数中最大数”的程序。

我是这样写的,看不出错到哪儿了。
int a=1, b, c,d;
string s, q, l;
s = Console.ReadLine();
a = int.Parse(s);
q = Console.ReadLine();
b = int.Parse(q);
l = Console.ReadLine();
c = int.Parse(l);
d = a;
if (a < b)
{
d = b;
a = b;
}
if (a < c)
d = c;

Console.WriteLine(a);

            int[] _int = new int[]{10,5,8 };//定义,可以把你自己输入的数字加入进去
            int max = 0;//定义最大数的变量
            for (int y = 0; y < _int.Length; y++) //来个循环对比
            {
                if (_int[y] > max) 
                {
                    max = _int[y];//如果当前是最大数就重新赋值
                }
            }

追问

程序要求是输入三个数,然后比较大小。
请问用这条语句有错么?编写过程没错误提醒啊。

string s, q, l;
s = Console.ReadLine();
a = int.Parse(s);

追答

你的没有错!只是比较原始而已。我给你的代码不公可以判断三组数大小,还可以判断10个,100个,1000个都可以!如果以你的方法判断个100可能都头大了

温馨提示:内容为网友见解,仅供参考
第1个回答  2015-06-18

将判断部分做如下修改:

d = a; 
if(a < b)

    d = b; 
}
if(d < c)
{
    d = c;  
}
Console.WriteLine(d);

因为只是要找到最大的值,所以不用那么麻烦的交换。

而对于你的问题就是,你在a<b的if语句中将a和d都等于b,而最后的判断中仅将d等于c,那么输出a是没有意义的。

追问

谢谢,改过了。

第2个回答  2018-06-11
#include <stdio.h>
int main()
{
int a,b,c,max;
printf("输入3个数字\n");
scanf("%d%d%d",&a,&b,&c);
if(a>b)
max=a;
else
max=b;
if(c>max)
max=c;
printf("max=%d",max);
return 0;
}
相似回答