我阅读了所有类似主题的文章,但我找不到适合我的解决方案。我正在学习C#,我用数组编写了简单的代码(猜测数字从1到100)。当用户点击正确的号码时,应用程序会按用户显示所有插入的号码。 这些天我正在学习列表。我想编写类似的代码,但是使用List,限制尝试为10并显示所有插入的数字。无法弄清楚如何用List记住所有插入的数字。我在几个地方找到了命令 out ,但有没有简单的方法来做到这一点。如果我理解列表比数组更实用。我不需要确定用户可以尝试多少次尝试... 显然,编写InsertedNumber.Add(c)是不够的 代码简单而简短,我想我可以复制所有代码。提前谢谢。
static void Main(string[] args)
{
Random rnd = new Random();
int b = rnd.Next(1, 100);
Console.WriteLine(b);
int c;
int NumberOfTries = 1;
Console.WriteLine("Guess number from 1 to 100. You have 10 tries.");
for (int i = 0; i <= 10; i++)
{
if (i == 10) { Console.WriteLine("\nYou tried 10 times. Game is
over!"); break; }
Console.Write("\nGuess number: ");
List<int> InsertedNumbers = new List<int>();
c = Convert.ToInt32(Console.ReadLine());
InsertedNumbers.Add(c);
if (c == b)
{
Console.WriteLine("Bravo. You had {0} tries.", NumberOfTries);
foreach (var item in InsertedNumbers)
{
Console.WriteLine(item);
}
for (int j = 0; j < InsertedNumbers.Count; j++)
{
Console.WriteLine("{0}", InsertedNumbers[j]);
}
break;
}
else if (c < b)
{
Console.WriteLine("No. My number is larger then " + c);
NumberOfTries++;
}
else if (c > b)
{
Console.WriteLine("No. My number is smaller then " + c);
NumberOfTries++;
}
}
Console.ReadLine();
答案 0 :(得分:2)
如果我正确理解您的问题,您打印的问号列表无效。
这是因为您在循环的每次迭代中都创建了一个新的List。所以一切正常。但是每次迭代都会创建一个新的List,覆盖上一次迭代中的那个。所以只需在开始迭代之前创建一次List,一切都应该没问题。
private static void Main(string[] args)
{
List<int> InsertedNumbers = new List<int>(); //Create List before the loop
Random rnd = new Random();
int b = rnd.Next(1, 100);
Console.WriteLine(b);
int c;
int NumberOfTries = 1;
Console.WriteLine("Guess number from 1 to 100. You have 10 tries.");
for (int i = 0; i <= 10; i++)
{
if (i == 10)
{
Console.WriteLine("\nYou tried 10 times. Game is over!");
break;
}
Console.Write("\nGuess number: ");
c = Convert.ToInt32(Console.ReadLine());
InsertedNumbers.Add(c);
if (c == b)
{
Console.WriteLine("Bravo. You had {0} tries.", NumberOfTries);
foreach (int item in InsertedNumbers)
{
Console.WriteLine(item);
}
for (int j = 0; j < InsertedNumbers.Count; j++)
{
Console.WriteLine("{0}", InsertedNumbers[j]);
}
break;
}
else if (c < b)
{
Console.WriteLine("No. My number is larger then " + c);
NumberOfTries++;
}
else if (c > b)
{
Console.WriteLine("No. My number is smaller then " + c);
NumberOfTries++;
}
}
Console.ReadLine();
}
答案 1 :(得分:2)
首先:提取方法,不要将所有内容塞入单个Main
。当InsertedNumbers
是PlayGame
的本地变量时,它更容易维护。
如果您要打印所有InsertedNumbers
项,可以尝试使用String.Join
方法
private static int GuessNumber() {
Console.Write("\nGuess number: ");
int result = 0;
// Check User Input: what if the inserted value is "bla-bla-bla"?
while (!int.TryParse(Console.ReadLine(), out result))
Console.WriteLine("Incorrect format; please, put an integer value");
return result;
}
private static void PlayGame(int value, int maxTries = 10) {
List<int> InsertedNumbers = new List<int>();
while (InsertedNumbers.Count < maxTries) {
int guess = GuessNumber();
InsertedNumbers.Add(guess);
if (guess < value)
Console.WriteLine($"No. My number is larger then {guess}");
else if (guess > value)
Console.WriteLine($"No. My number is smaller then {guess}");
else {
Console.WriteLine($"Bravo. You had {InsertedNumbers.Count} tries.");
// Combine all items in InsertedNumbers into single string
Console.WriteLine(string.Join(Environment.NewLine, InsertedNumbers));
return;
}
}
Console.WriteLine("\nYou tried maxTries times. Game is over!");
}
最后
static void Main(string[] args) {
Random rnd = new Random();
PlayGame(rnd.Next(1, 100), 10);
}