新手尝试结合 while 和 if 语句
Noob trying to combine while and if statements
我正在尝试学习 C# 并想尝试从头开始制作一个简单的程序,要求用户输入 1-3 之间的数字,每个数字都会产生彩票中奖,写“exit”应该退出该程序。当回答一个号码时,应该提示用户再次回答,直到选择退出。无论我在尝试组合 while 循环和 if 语句时做什么,我都会收到错误或只是无限循环不会停止。可能是一些简单的语法误解。任何帮助将不胜感激。
到目前为止,这是我的代码:
static void Main(string[] args)
{
string userInput = "";
Console.Write("Pick a number between 1-3, type 'exit' to stop the program: ");
userInput = Console.ReadLine();
while (userInput != "exit")
if (userInput == "1")
{
Console.WriteLine("You won a car");
}
else if (userInput == "2")
{
Console.WriteLine("You won a boat");
}
else if (userInput == "3")
{
Console.WriteLine("Sorry, no luck this time. Try again");
}
else if (userInput == "exit")
{
Console.WriteLine("Exiting...");
break
}
else
{
Console.WriteLine("The number has to be between 1-3, try again.");
}
Console.ReadLine()
}
}
}
}
userInput
的值在循环中永远不会更新,这意味着每次循环运行时,值都保持不变。
一个解决方案是将提示和阅读移动到循环的开头。
补充一下 Zayenz 的回答,这是所有无限循环的关键;决定是否开始循环的评估环境始终为真。如果你再次遇到这个问题,你只需要查看代码以确保任何应该让你跳出循环的东西实际上改变了它需要的任何标准以使条件为假;
这应该有效
static void Main(string[] args)
{
string userInput = "";
Console.Write("Pick a number between 1-3, type 'exit' to stop the program: ");
userInput = Console.ReadLine();
while (userInput != "exit")
{
if (userInput == "1")
{
Console.WriteLine("You won a car");
}
else if (userInput == "2")
{
Console.WriteLine("You won a boat");
}
else if (userInput == "3")
{
Console.WriteLine("Sorry, no luck this time. Try again");
}
else if (userInput == "exit")
{
Console.WriteLine("Exiting...");
break
}
else
{
Console.WriteLine("The number has to be between 1-3, try again.");
}
userInput = Console.ReadLine()
}
}
}
我正在尝试学习 C# 并想尝试从头开始制作一个简单的程序,要求用户输入 1-3 之间的数字,每个数字都会产生彩票中奖,写“exit”应该退出该程序。当回答一个号码时,应该提示用户再次回答,直到选择退出。无论我在尝试组合 while 循环和 if 语句时做什么,我都会收到错误或只是无限循环不会停止。可能是一些简单的语法误解。任何帮助将不胜感激。
到目前为止,这是我的代码:
static void Main(string[] args)
{
string userInput = "";
Console.Write("Pick a number between 1-3, type 'exit' to stop the program: ");
userInput = Console.ReadLine();
while (userInput != "exit")
if (userInput == "1")
{
Console.WriteLine("You won a car");
}
else if (userInput == "2")
{
Console.WriteLine("You won a boat");
}
else if (userInput == "3")
{
Console.WriteLine("Sorry, no luck this time. Try again");
}
else if (userInput == "exit")
{
Console.WriteLine("Exiting...");
break
}
else
{
Console.WriteLine("The number has to be between 1-3, try again.");
}
Console.ReadLine()
}
}
}
}
userInput
的值在循环中永远不会更新,这意味着每次循环运行时,值都保持不变。
一个解决方案是将提示和阅读移动到循环的开头。
补充一下 Zayenz 的回答,这是所有无限循环的关键;决定是否开始循环的评估环境始终为真。如果你再次遇到这个问题,你只需要查看代码以确保任何应该让你跳出循环的东西实际上改变了它需要的任何标准以使条件为假;
这应该有效
static void Main(string[] args)
{
string userInput = "";
Console.Write("Pick a number between 1-3, type 'exit' to stop the program: ");
userInput = Console.ReadLine();
while (userInput != "exit")
{
if (userInput == "1")
{
Console.WriteLine("You won a car");
}
else if (userInput == "2")
{
Console.WriteLine("You won a boat");
}
else if (userInput == "3")
{
Console.WriteLine("Sorry, no luck this time. Try again");
}
else if (userInput == "exit")
{
Console.WriteLine("Exiting...");
break
}
else
{
Console.WriteLine("The number has to be between 1-3, try again.");
}
userInput = Console.ReadLine()
}
}
}