0 c# while-loop
我试图在2周时间内完成C#的练习,为我的时间限制测试做准备,并且一直试图完成我在书籍和互联网上发现的练习.
该练习要求使用while循环来要求用户输入他们的名字,如果它不是"XXX",那么它可以继续循环.但是我得到的问题是,在编写循环之后,它只是继续,用户无法输入"XXX"来停止程序,所以我想知道是否有人知道这个解决方案?
这是我到目前为止写的代码..
String sName;
//Declaring the variable
Console.Write("Enter a name (or XXX to end): ");
sName = Console.ReadLine();
//Prompting user to enter the name or to end the program by telling them to type XXX
while (sName != "XXX")
{
Console.Write("The Name is: " + sName);
Console.WriteLine();
}
//Start loop
Console.WriteLine("You are now past the while loop");
//If XXX is typed, message is displayed
Console.WriteLine("Press any key to close");
Console.ReadKey();
//Prevent program from closing
Run Code Online (Sandbox Code Playgroud)
您的输入语句应该在循环中,否则如果第一个输入不等于,您将最终得到无限循环 XXX
String sName="";
while (sName != "XXX")
{
Console.Write("Enter a name (or XXX to end): ");
sName = Console.ReadLine();
Console.Write("The Name is: " + sName);
Console.WriteLine();
}
Console.WriteLine("You are now past the while loop");
Console.WriteLine("Press any key to close");
Console.ReadKey();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
177 次 |
| 最近记录: |