作业要求
冒泡法排序:实现用户随机输入6个整数,从大到小排序输出。
代码
...
namespace _2
{
class Program
{
static void Main(string[] args)
{
try
{
Double[] jg = new Double[6];
Console.WriteLine("请输入6个数字");
Console.WriteLine("");
for (int i = 0; i < jg.Length; i++)
{
Console.WriteLine("");
Console.WriteLine("请输入第{0}个数字:", i + 1);
jg[i] = Convert.ToDouble(Console.ReadLine());
}
for (int i = 0; i < jg.Length - 1; i++)
{
for (int j = 0; j <jg.Length -1- i; j++)
{
if (jg[j] < jg[j + 1])
{
Double temp = jg[j];
jg[j] = jg[j + 1];
jg[j + 1] = temp;
}
}
}
Console.WriteLine("");
Console.WriteLine("排序后的数字为:");
foreach (Double var in jg)
{
Console.Write(var); //依次读取字符串中的元素
Console.Write(" ");
}
}
catch
{
Console.WriteLine("格式错误");
}
Console.ReadKey();
}
}
}
...