我无法显示我在控制台中键入的所有数字。我需要创建一个列表,以显示我猜到的所有数字。
for (int i = 0; i < 100000; i++)
{
z++;
Console.Write("\nGuess the number I imagined: ");
int c = Convert.ToInt32(Console.ReadLine()); // all of numbers that I type should appear.
if (b == c)
{
Console.WriteLine("\n Congratulations you guessed in {0} attempts. :)",z); // this is just showing how many attempts I had.
List<int> Attemptsnums= new List<int>();
Attemptsnums.Add(c); // here I've put c but It only shows last number I entered, I need all of them in line.
foreach (var element in Attemptsnums)
{
Console.WriteLine("Numbers: {0}", element);
}
break;
}
}
答案 0 :(得分:1)
您当前正在做的事情有1个主要问题。在“ for循环”中,您始终将“ Attemptsnums”创建为新列表,这意味着在添加“ c”之前,它将始终为空。在“ for循环”之前声明列表,每次都会在列表中添加“ c”。
我还假设出于某种原因,您正在运行多达100,000个for循环。但是,有趣的是,您从未使用过变量“ i”,而是每次都将“ z”加1。 “ z”的开头是什么?如果它从0开始,则可以仅在for循环中删除“ z”,而在显示“ z”的位置可以仅显示“ i”。
答案 1 :(得分:0)
移动:
List<int> Attemptsnums= new List<int>();
循环外
Attemptsnums.Add(c);
如果
答案 2 :(得分:0)
尝试
List<int> Attemptsnums= new List<int>();
for (int i = 0; i < 100000; i++)
{
z++;
Console.Write("\nGuess the number I imagined: ");
int c = Convert.ToInt32(Console.ReadLine()); // all of numbers that I type should appear.
if (b == c)
{
Console.WriteLine("\n Congratulations you guessed in {0} attempts. :)",z); // this is just showing how many attempts I had.
Attemptsnums.Add(c); // here I've put c but It only shows last number I entered, I need all of them in line.
foreach (var element in Attemptsnums)
{
Console.WriteLine("Numbers: {0}", element);
}
break;
}
}