C#产生并输出8个随机2位数;然后,排序输出。
答案:3 悬赏:20 手机版
解决时间 2021-03-17 13:16
- 提问者网友:饥饿走向夜
- 2021-03-16 14:45
C#程序设计教程。。。
最佳答案
- 五星知识达人网友:动情书生
- 2021-03-16 16:16
static void Main(string[] args)
{
int numlenth=8;
int[] num = new int[numlenth];
int temp;
for (int i = 0; i < num.Length; i++)
{
Random rand = new Random();
num[i] = rand.Next(10, 99);
Thread.Sleep(200); //这里需要using System.Threading;因为程序执行太快,让它睡会,才会得到不同的随机数,不然就会得到8个相等的数,或者在new Random()这里做个时间种子....
}
Console.WriteLine("排序前:");
foreach (int i in num)
{
Console.Write(i);
Console.Write(" ");
}
Console.WriteLine();
for (int i = 0; i < num.Length - 1; i++)
{
for (int j = 0; j < num.Length - 1 - i; j++)
{
if (num[j] > num[j + 1])
{
temp = num[j];
num[j] = num[j + 1];
num[j + 1] = temp;
}
}
}
Console.WriteLine("排序后:");
foreach (int i in num)
{
Console.Write(i);
Console.Write(" ");
}
Console.ReadLine();
}
{
int numlenth=8;
int[] num = new int[numlenth];
int temp;
for (int i = 0; i < num.Length; i++)
{
Random rand = new Random();
num[i] = rand.Next(10, 99);
Thread.Sleep(200); //这里需要using System.Threading;因为程序执行太快,让它睡会,才会得到不同的随机数,不然就会得到8个相等的数,或者在new Random()这里做个时间种子....
}
Console.WriteLine("排序前:");
foreach (int i in num)
{
Console.Write(i);
Console.Write(" ");
}
Console.WriteLine();
for (int i = 0; i < num.Length - 1; i++)
{
for (int j = 0; j < num.Length - 1 - i; j++)
{
if (num[j] > num[j + 1])
{
temp = num[j];
num[j] = num[j + 1];
num[j + 1] = temp;
}
}
}
Console.WriteLine("排序后:");
foreach (int i in num)
{
Console.Write(i);
Console.Write(" ");
}
Console.ReadLine();
}
全部回答
- 1楼网友:深街酒徒
- 2021-03-16 18:25
form中放两个txt txt1为奇数 txt2为偶数
private void btn_click(object sender, eventargs e)
{
arraylist arr = new arraylist();
random rd = new random();
for (int i = 0; i {
arr.add(rd.next(1, 101));
}
arr.sort();
foreach (int i in arr)
{
if ((i % 2) == 0)
{
txt2.text += i + ",";
}
else
{
txt1.text += i + ",";
}
}
txt1.text = txt1.text.trim(',');
txt2.text = txt2.text.trim(',');
} 执行前:
执行后:
- 2楼网友:纵马山川剑自提
- 2021-03-16 17:35
Random r = new Random();
int n=8;
int[] a;
a = new int[n];
for (int i = 0; i < n; i++)
{
int x; //= r.Next(100);
do
{
x=r.Next (100);
} while (!(x >= 10 && x <= 99));
a[i] = x;
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯