用C#从键盘输入三个整数,从大到小依次输出这三个数字

2025-01-04 07:29:35
推荐回答(4个)
回答1:

这种简单的问题,自己多动动手

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

namespace Test
{
class Program
{
static void Main(string[] args)
{
var numbers = new List();

for (var i = 0; i < 3; i++)
{
Console.Write("请输入第{0}个数:", i + 1);
var num = Console.ReadLine();
numbers.Add(Convert.ToInt32(num));
}

numbers = numbers.OrderByDescending(n => n).ToList();

var numStr = new StringBuilder();
foreach (var num in numbers)
{
numStr.Append(num + " ");
}

Console.WriteLine("排序后的数字:" + numStr);
Console.ReadLine();
}
}
}

回答2:

int[] num = new int[3];
Console.WriteLine("请输入三个整数");
for (int i = 0; i < num.Length; i++)
{
Console.WriteLine("请输入第{0}个数字",i+1);
num[i] = Convert.ToInt32(Console.ReadLine());
}
Array.Sort(num);
Array.Reverse(num);
Console.WriteLine("数字从大到小依次为:");
for (int i = 0; i < num.Length; i++)
{
Console.WriteLine(num[i]);
}

回答3:

把三个输入的数比较一下,然后输出

回答4:

排下序就行了