c#比较3个整数的大小,求出最大值最小值
答案:4 悬赏:50 手机版
解决时间 2021-04-06 23:02
- 提问者网友:浮克旳回音
- 2021-04-06 16:34
c#比较3个整数的大小,求出最大值最小值
最佳答案
- 五星知识达人网友:青尢
- 2021-04-06 17:41
int a=12;
int b=13;
int c=14;
int max=a;//默认最大值
int min=a;//默认最小值
max=max>b?(max>c?max:c):(b>c?b:c);
min=min//int a=(b //转成if条件是这样的:
if(max>b){//如果默认最大值max大于b
max=b;//把b赋给最大值
}else if(max>c){//如果最大值max大于c
max=c;//把c赋给最大值
}else{
//不做任何处理,max就是最大值
}
//求最小值只需要把上面的大于符号(>)换成小于符号(<)
int b=13;
int c=14;
int max=a;//默认最大值
int min=a;//默认最小值
max=max>b?(max>c?max:c):(b>c?b:c);
min=min//int a=(b
if(max>b){//如果默认最大值max大于b
max=b;//把b赋给最大值
}else if(max>c){//如果最大值max大于c
max=c;//把c赋给最大值
}else{
//不做任何处理,max就是最大值
}
//求最小值只需要把上面的大于符号(>)换成小于符号(<)
全部回答
- 1楼网友:过活
- 2021-04-06 20:57
作业自己做的好……这么简单的……追问那你做给我看看呀追答// 初始化长度为三的数组
int[] numbers = new int[3];
numbers[0] = 1;
numbers[1] = 2;
numbers[2] = 3;
// 随便在数组里取个值作为初始值
int maxNumber = numbers[0];
int minNumber = numbers[0];
for (int i = 0; i < numbers.length; i++) {
maxNumber = numbers[i] >= maxNumber ? numbers[i] : maxNumber;
minNumber = numbers[i] <= minNumber ? numbers[i] : minNumber;
}
//不止适用于三个数比大小,多少个数都可以,根据数组长度来就行了
//maxNumber和minNumber的输出语句就不写了
int[] numbers = new int[3];
numbers[0] = 1;
numbers[1] = 2;
numbers[2] = 3;
// 随便在数组里取个值作为初始值
int maxNumber = numbers[0];
int minNumber = numbers[0];
for (int i = 0; i < numbers.length; i++) {
maxNumber = numbers[i] >= maxNumber ? numbers[i] : maxNumber;
minNumber = numbers[i] <= minNumber ? numbers[i] : minNumber;
}
//不止适用于三个数比大小,多少个数都可以,根据数组长度来就行了
//maxNumber和minNumber的输出语句就不写了
- 2楼网友:低血压的长颈鹿
- 2021-04-06 19:21
int min, max;
int a, b, c;
a = 30; b = 6; c = 5;
if (a < b)
{
min = a;
max = b;
}
else
{
min = b;
max = a;
}
if (min > c)
{
min = c;
}
if (max < c)
{
max = c;
}
Console.WriteLine("最小值 :" + min);
Console.WriteLine("最大值 :" + max);
int a, b, c;
a = 30; b = 6; c = 5;
if (a < b)
{
min = a;
max = b;
}
else
{
min = b;
max = a;
}
if (min > c)
{
min = c;
}
if (max < c)
{
max = c;
}
Console.WriteLine("最小值 :" + min);
Console.WriteLine("最大值 :" + max);
- 3楼网友:执傲
- 2021-04-06 18:45
static void Main(string[] args)
{
int a, b, c;
int max, min;
Console.WriteLine("请输入 第 1 个数:");
a = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("请输入 第 2 个数:");
b = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("请输入 第 3 个数:");
c = Convert.ToInt32(Console.ReadLine());
max = (a > b ? a : b) > c ? (a > b ? a : b) : c;
min = (a < b ? a : b) < c ? (a < b ? a : b) : c;
Console.WriteLine("min = {0},max = {1}", min, max);
Console.ReadKey();
}
{
int a, b, c;
int max, min;
Console.WriteLine("请输入 第 1 个数:");
a = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("请输入 第 2 个数:");
b = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("请输入 第 3 个数:");
c = Convert.ToInt32(Console.ReadLine());
max = (a > b ? a : b) > c ? (a > b ? a : b) : c;
min = (a < b ? a : b) < c ? (a < b ? a : b) : c;
Console.WriteLine("min = {0},max = {1}", min, max);
Console.ReadKey();
}
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯