c# - C# 转到循环的开头(继续还是中断?)
问题描述
该程序随机创建一个用户需要猜测的 1 到 50 之间的数字。当用户的猜测不正确时,我想重新启动 while 循环。我尝试过使用 break 并继续这样做,但要么我做错了,要么这不是最好的解决方案。此外,当用户输入不正确时,系统会询问他们是否要再次猜测。我还尝试使用 break 退出该循环或继续获得类似的结果。有什么建议么?
Console.WriteLine("I am thinking of a whole number between 1 and 50. Enter your guess.");
string userGuess;
Random rnd = new Random();
int compNum = rnd.Next(1, 50);
int guessToInt;
int numOfTries = 0;
string cont;
bool correct = false;
//TODO Delete this line when finished
Console.WriteLine($"Answer " + compNum);
//Trying to get new user input if guess is wrong
while (correct == false)
{
//correct = false;
numOfTries++;
userGuess = Console.ReadLine();
guessToInt = Int32.Parse(userGuess);
if (guessToInt == compNum)
{
numOfTries++;
correct = true;
Console.WriteLine($"Your guess is correct! The number was " + compNum + ". Congratulations. It only took you " + numOfTries + " guess(es).");
}
else if (guessToInt < 1 || guessToInt > 50)
{
Console.WriteLine("Incorrect input. Enter a whole number between 1 and 50. Try again.");
//numOfTries++;
correct = false;
continue;
}
else if (guessToInt != compNum)
{
//How do I get new user input?
Console.WriteLine("Your guess is incorrect. Would you like to try again? Y or N?");
//numOfTries++;
correct = false;
cont = Console.ReadLine();
if (cont == "Y")
{
continue;
}
else Console.WriteLine("Bye."); Environment.Exit(0);
}
}
解决方案
几个问题:
- 您不必每次都将布尔值设置为 false。它在开始时已设置为 false
else Console.WriteLine("Bye."); Environment.Exit(0);
由于您没有添加大括号(我强烈建议您永远不要这样做),
因此只有第一个命令将在语句下运行。
“Environment.Exit”总是会发生。
对于其余部分,我将修复添加到您的代码中。
Console.WriteLine("I am thinking of a whole number between 1 and 50. Enter your guess.");
string userGuess;
Random rnd = new Random();
int compNum = rnd.Next(1, 50);
int guessToInt;
int numOfTries = 0;
string cont;
bool correct = false;
//TODO Delete this line when finished
Console.WriteLine($"Answer " + compNum);
//Trying to get new user input if guess is wrong
while (correct == false)
{
//correct = false;
numOfTries++;
userGuess = Console.ReadLine();
guessToInt = Int32.Parse(userGuess);
if (guessToInt == compNum)
{
numOfTries++;
correct = true;
Console.WriteLine($"Your guess is correct! The number was " + compNum + ". Congratulations. It only took you " + numOfTries + " guess(es).");
}
else if (guessToInt < 1 || guessToInt > 50)
{
Console.WriteLine("Incorrect input. Enter a whole number between 1 and 50. Try again.");
//numOfTries++;
//**correct = false; --> No need, correct was already false
//**continue; --> No need, will continue anyway
}
else if (guessToInt != compNum)
{
//How do I get new user input?
Console.WriteLine("Your guess is incorrect. Would you like to try again? Y or N?");
cont = Console.ReadLine();
//numOfTries++;
//**correct = false; --> No need, correct was already false
//** if (cont == "Y") --> No need, will continue anyway
//** {
//** continue;
//** }
// else Console.WriteLine("Bye."); Environment.Exit(0); -->
//** "else" without brackets will
if (cont != "Y")
{
break;
}
Console.WriteLine("Enter your next guess:");
}
}
注意:我没有触及“numOfTries”功能,因为问题中没有提到
推荐阅读
- node.js - docker push 说图片不存在
- sql-server - 如何将 DATEDIFF 结果转换为十进制?
- javascript - 链接到锚点 ID 时删除移动菜单
- java - 这是使用 ReentrantLock.isHeldByCurrentThread 的正确方法吗?
- javascript - 限制可折叠项目基于特定点击保持折叠状态
- css - IE11 消息栏从底部向上飞而不是从顶部缓动
- c++ - __bridge 在 C++ 和 Objective-C++ 中强制转换
- .htaccess - 如何使用 htaccess 重定向 url?
- ios - 当 UIViewController 被解除时重新加载 TableView?
- python - PostgreSQL全文搜索构建GIN索引