在使用TryParse捕获用户是否在何处输入字符串而不是int时,我的代码无法正常工作。如果我按现在的样子使用它,则仅当输入了除int之外的其他值时,我的基本值才为0。我希望它向用户显示错误消息。
尝试过尝试使用TryParse的多种不同方法,但没有一种真的有用。
static void Main(string[] args)
{
Random r = new Random();
int speltal = r.Next(1,21);
bool play = false;
int myNum;
while (!play)
{
Console.Write("\n\tGuess a number between 1 and 20: ");
Int32.TryParse(Console.ReadLine(), out myNum);
if (myNum < guessNum)
{
Console.WriteLine("\tThe number you have guessed is to low");
Console.ReadLine();
}
if (myNum > guessNum)
{
Console.WriteLine("\tThe number you have guessed is to high");
Console.ReadLine();
}
if (myNum == guessNum)
{
Console.WriteLine("\tCongratulations you guessed the right number!");
Console.ReadLine();
}
如果用户输入除int以外的任何内容,我希望它向用户显示错误消息。根据我的老师,还必须包括TryParse
答案 0 :(得分:4)
您没有捕获bool
的{{1}}输出,因此您不知道是否输入了非数字值。尝试这样的事情:
TryParse
答案 1 :(得分:1)
TryParse
方法只能将整数放在传递的变量中(因为它是int
类型),因此,如果传递给它的值不能解析为整数,则默认值为int
(0)中的分配给变量。
TryParse
告诉您是否成功解析了数字的方法是返回布尔值指示符。
您可以尝试以下方法:
while (true)
{
bool valid = int.TryParse(Console.ReadLine(), out myNum);
if(valid)
break;
Console.WriteLine("Input invalid. Please try again");
}
答案 2 :(得分:0)
您应该使用bool
返回的TryParse
。看起来像这样:
更新的答案:
static void Main(string[] args)
{
Random random = new Random();
int guessNum = random.Next(1, 21);
while(true)
{
Console.WriteLine("Guess the number between 1 and 21.");
if (Int32.TryParse(Console.ReadLine(), out int input))
{
if (input == guessNum)
{
Console.WriteLine($"You guessed it right. The number is {input}");
if(ShouldContinue())
{
guessNum = random.Next();
continue;
}
else
{
break;
}
}
if (input < guessNum)
Console.WriteLine("The number you guessed is smaller.");
else
Console.WriteLine("The number you guessed is bigger");
}
else
{
Console.WriteLine("Please enter a number between 1 and 21 as your guess");
}
}
}
static bool ShouldContinue()
{
while (true)
{
Console.WriteLine($"Do you want to continue playing? (y/n)");
string continueInput = Console.ReadLine().Trim().ToLower();
if (continueInput == "y")
return true;
else if (continueInput == "n")
return false;
else
Console.WriteLine("Invalid input!. Please choose 'y' or 'n'.");
}
}
旧答案:
while (true)
{
if (Int32.TryParse(inputInt, out int myNum))
{
// your logic goes here, too low/high or correct answer.
}
}
答案 3 :(得分:0)
TryParse
返回一个布尔值,该布尔值指示输入字符串是否已成功解析。上面的两个答案在如何处理无效输入方面都是正确的,但是,这是一个更干净的版本,它还将支持规则,between 1 and 20
:
while (true)
{
Console.Write("\n\tGuess a number between 1 and 20: ");
//Checks to see if the input was successfully parsed to a integer also, performs a Trim on the input as to remove any accidental white spaces that a user might have typed in, e.g. "1000 "
if (int.TryParse(Console.ReadLine().Trim(), out myNum))
{
//Checks to see if the parsed number is in the specified range
if ((myNum > 0) && (myNum < 21))
{
break;
}
Console.WriteLine("\tThe input number was out of the specified range.");
}
else
{
Console.WriteLine("\tFailed to parse the input text.");
}
//Optional, makes the thread sleep so the user has the time to read the error message.
Thread.Sleep(1500);
//Optional, clears the console as to not create duplicates of the error message and the value of Console.Write
Console.Clear();
}
// Continue here, if (myNum < guessNum) . . .