无法通过谷歌解决我的问题,所以我想在这里试试我的运气。我是c#的新手,正在编写一个程序,它随机生成一个数字,用户可以猜测,直到它得到正确。一切都运行得很顺利,但问题是,如果输入的是字母而不是数字,则不会触发else语句,也不会向用户提供错误消息,而只是转到第一个if语句。
这里是代码,感谢任何帮助!我希望格式至少是可读的。Random slumpat = new Random();
int speltal = slumpat.Next(1, 20);
bool spela = true;
do
{
Console.Write("ntGissa på ett tal mellan 1 och 20: ");
if (Int32.TryParse(Console.ReadLine(), out int tal))
{
if (tal < speltal)
{
Console.WriteLine("tDet inmatade talet " + tal + " är för litet, försök igen:");
}
else if (tal > speltal)
{
Console.WriteLine("tDet inmatade talet " + tal + " är för stort, försök igen:");
}
else if (tal == speltal)
{
Console.WriteLine("tGrattis, du gissade rätt!");
Console.ReadLine();
spela = false;
}
}
else
Console.WriteLine("tDu måste ange en siffra!");
Console.ReadLine();
} while (spela == true);
设置randomNumber
值为正整数—您可以在解析用户输入时通过简单的条件userNumber > 0
检查用户输入的是正数。如果用户输入一个负数或字母或其他任何东西-else
语句将被触发。
翻译你的例子和编辑去做事。查看注释以了解代码中发生了什么:
static void Main(string[] args)
{
Random random = new Random();
int randomNumber = random.Next(1, 20); // Only positive digits from 1 to 20
// This variable isn't neccessary. We start loop and running it until user close program manually or loop is breaked from inside.
// bool play = true;
do
{
Console.Write("Guess a number between 1 and 20: ");
if (int.TryParse(Console.ReadLine(), out int userNumber) && userNumber > 0) // Right here you check that user's guess number is positive integer
{
if (userNumber < randomNumber)
Console.WriteLine("The entered number " + userNumber + " is less than the intended one, please try again!");
else if (userNumber > randomNumber)
Console.WriteLine("The entered number " + userNumber + " is greater than intended one, please try again!");
else if (userNumber == randomNumber)
{
// Edited this part to ask user for replay without restarting program
Console.Write("Congratulations, you guessed it!nWant play again? (Y/N): ");
if (Console.ReadLine().ToLower() == "y") // Player want to replay again
{
randomNumber = random.Next(1, 20); // Generate new randomNumber
Console.WriteLine(new string('-', 25) + "n"); // Separator
}
else
break; // Player doesn't want play again so breaking loop
}
}
else // User inputed negative number or 0 or NaN (Not A Number)
Console.WriteLine("You must enter a non-negative number!");
} while (true);
Console.WriteLine("nThanks for playing!"); // Don't forget to thank your player!
Console.WriteLine("Press any key to close the program...");
Console.ReadKey();
}
这是一个简单的版本,允许用户重新输入数字,直到他猜对了。
可以设置为硬模式所以用户可以尝试猜测正确的数字:
static void Main(string[] args)
{
Random random = new();
int randomNumber = random.Next(1, 20);
int maxAttempts = 3; // Declare number or possible attempts for user to guess a number
do
{
if (maxAttempts == 0) // User didn't guess right number for 3 attempts so break the loop
{
Console.WriteLine("Sorry, but you didn't guessed through 3 attempts.");
Console.WriteLine("GAME OVER."); // No replay available
break;
}
Console.Write("Guess a number between 1 and 20 (" + maxAttempts + " attempts left): ");
if (int.TryParse(Console.ReadLine(), out int userNumber) && userNumber > 0)
{
maxAttempts--; // Subtracting attempts
if (userNumber == randomNumber)
{
Console.Write("Congratulations, you guessed it!nWant play again? (Y/N): ");
if (Console.ReadLine().ToLower() == "y")
{
randomNumber = random.Next(1, 20);
maxAttempts = 3; // Reset maximum attempts back to 3
Console.WriteLine(new string('-', 25) + "n");
}
else
break;
}
else
{
// Do not tell player that he entered a number greater or less than the intended one, it is HARD MODE of your game
Console.WriteLine("You didn't guess, please try again!");
}
}
else
Console.WriteLine("You must enter a non-negative number!");
} while (true);
Console.WriteLine("nThanks for playing!");
Console.WriteLine("Press any key to close the program...");
Console.ReadKey();
}