C#怎么比较三个数的大小并按从大到小的顺序输出?要用if语句。

2025-01-01 09:22:57
推荐回答(5个)
回答1:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("请输入三个数:");
double a = Convert.ToDouble (Console .ReadLine ());
double b = Convert.ToDouble(Console.ReadLine());
double c = Convert.ToDouble(Console.ReadLine());
double ? max=null ;
double ? middle =null ;
double ? min=null ;
if (a >= b && b >= c)
{
max = a;
middle = b;
min = c;
}
else
if(a>=c&&c>=b)
{
max = a;
middle = c;
min = b;

}
else
if (b >= a && a >= c)
{
max = b;
middle = a;
min = c;

}
else
if (b >= c && c >= a)
{
max = b;
middle = c;
min = a;

}
else
if (c>= a && a >= b)
{
max = c;
middle = a;
min = b;

}
else

if (c >= b && b >= a)

{
max = c;
middle = b;
min = a;

}
Console.WriteLine ("排序后的序列为(从大到小):{0}{1}{2}{3}{4}",max ,",",middle,"," ,min);

}
}
}
测试过了绝对能用!是控制台应用程序!写的有点复杂!也可以用冒泡排序什么的写。。希望对你有用!谢谢!

回答2:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("请输入三个数:");
double a = Convert.ToDouble (Console .ReadLine ());
double b = Convert.ToDouble(Console.ReadLine());
double c = Convert.ToDouble(Console.ReadLine());
double ? max=null ;
double ? middle =null ;
double ? min=null ;
if (a >= b && b >= c)
{
max = a;
middle = b;
min = c;
}
else
if(a>=c&&c>=b)
{
max = a;
middle = c;
min = b;

}
else
if (b >= a && a >= c)
{
max = b;
middle = a;
min = c;

}
else
if (b >= c && c >= a)
{
max = b;
middle = c;
min = a;

}
else
if (c>= a && a >= b)
{
max = c;
middle = a;
min = b;

}
else

if (c >= b && b >= a)

{
max = c;
middle = b;
min = a;

}
Console.WriteLine ("排序后的序列为(从大到小):{0}{1}{2}{3}{4}",max ,",",middle,"," ,min);

}
}
}

回答3:

用冒泡排序
int [] scores = new int [3];//定义数组
int i,j;
int temp;
console.writeline("输入三位数");
for(i=0;i<3;i++){
console.writeline("输入第{0}个数",i+1);
scores[i]=int.parse(console.readline());
}
for(i=0;ifor(j=0;jif(scores[j]>scores[j+1]){
temp=scores[j];
scores[j]=scores[j+1];
scores[j+1]=temp;
}
}
}
console.writeline("排序后的数字为");
for(i=0;iconsole.write("{0}\t",scores[i]);
}
给我分吧,我是用手机写的这段程序,花了20分钟呢,可怜的我,

回答4:

if(a>b && a>c)

max=a;
else if(b>a && b>c)
max=b;
else if(c>a && c>b) (为了你明白我写出来了,可以直接写个else)
max=c;

if(a
min=a;
else if(bmin=b;
else if(c
min=c;

if(a>b && amid=a;
else if(b>a && bmid=b;
else if(c>a && cmid=c;
最简音的写法为MAX=(a>b)?((a>c)?a:c):((b>c)?b:c)剩下的就充分发挥你的逻辑能力吧

回答5:

用控制台做的:
using System;
class compare
{
public static void Main()
{
//任意声明个数组,也可以很多元素,或者自己输入赋值给元素;
int[] myarray = { 3, 2, 9 };
//利用冒泡排序对数组进行排序,这个写代码对任何一维数组都可以排序;
for (int i = 0; i < myarray.Length - 1; i++)
{
for (int j = 0; j < myarray.Length - 1 - i; j++)
{
if (myarray[j] < myarray[j + 1])
{
int temp = myarray[j + 1];
myarray[j + 1] = myarray[j];
myarray[j] = temp;
}
}
}
//输出结果;
for(int i = 0; i < myarray.Length;i++)
Console.Write(myarray[i]+" ");
Console.ReadLine();
}
}
测试了,行得通,希望你能学到知识……