我正在尝试制作2个表,其中一个将存储数字,第二个将显示此数字两次,用户也决定将生成多少个数字。
Example:
Table1 -> 67 9 4 -78 -29
Table2 -> 67 67 9 9 4 4 -78 -78 -29 -29
当前代码:
#region TablesTest
Console.Write("Enter n: ");
int n = Convert.ToInt32(Console.ReadLine());
int[] table1 = new int[n];
int[] table2 = new int[n];
Random rnd = new Random();
for (int i = 0; i < n; i++)
{
table1[i] = rnd.Next(-100, 100);
foreach (int x in table1)
{
table2[i] = table1[i] + table1[i];
Console.Write(table2[i]);
}
}
Console.ReadKey();
#endregion
答案 0 :(得分:2)
如果我理解你的问题,这应该有效:
Console.Write("Enter n: ");
int n;
if (Int32.TryParse(Console.ReadLine(), out n))
{
int[] table1 = new int[n];
Random rnd = new Random();
//put n randon numbers into table1
Console.Write("Table1 -> ");
for (int i = 0; i<n; i++)
{
table1[i] = rnd.Next(-100, 100);
Console.Write(table1[i] + " ");
}
Console.WriteLine();
//foreach number in table1, add two to table2
int[] table2 = new int[n * 2];
Console.Write("Table2 -> ");
for (int i = 0; i<n; ++i)
{
table2[i] = table1[i];
table2[i + 1] = table1[i];
Console.Write(table2[i] + " ");
Console.Write(table2[i + 1] + " ");
}
Console.ReadKey();
}
它将n
个随机数放入table1
,然后将相同的数字加到table2
两次。