好的,我的问题是这个,我目前有3个可能的答案:是,否,以及其他一切.我希望我的程序写下"原谅我?" 直到我回答是或否...我是新手使用c#并且仍在学习,请尽可能简单.谢谢
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Pitalica
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Kvisko");
Console.WriteLine("\"Hello Traveller!!\" says a man. \"What's your name?\"");
string playerName = Console.ReadLine();
Console.WriteLine("\"Hi " + playerName + ", welcome to the beautiful city of Osijek!\nI would like to give you a tour of our little town, but I don't have time to do so right now.\nI need to go to class.\"");
Console.WriteLine("He looks at you with your backpack on your back.\"But I could show you later if you're up to?\"");
string answer1 = Console.ReadLine();
if (answer1 == "yes")
Console.WriteLine("\"We have a deal, " + playerName + "!\"");
else if (answer1 == "no")
Console.WriteLine("\"Your loss," + playerName + "...\"");
else
{
Console.WriteLine("Pardon me?");
}
Console.WriteLine("After some time...");
}
}
}
Run Code Online (Sandbox Code Playgroud)
从技术上讲,你可以实现任何循环,但我建议while
...
string answer1 = Console.ReadLine();
while (answer1 != "yes" && answer1 != "no") {
Console.WriteLine("Pardon me?");
answer1 = Console.ReadLine();
}
...
Run Code Online (Sandbox Code Playgroud)
只是不断询问,而 answer1不是正确的.
编辑:正如Hogan在评论中建议的那样,我们应该对用户很好:让他/她在任何带有前导和尾随空格的寄存器中输入YES/no:
...
// with Trim() and ToUpper() all "Yes", " yes", "YES " are OK
string answer1 = Console.ReadLine().Trim().ToUpper();
while (answer1 != "YES" && answer1 != "NO") {
Console.WriteLine("Pardon me?");
answer1 = Console.ReadLine().Trim().ToUpper();
}
...
Run Code Online (Sandbox Code Playgroud)
编辑2:退出程序(请参阅注释中的其他问题),只需从以下位置返回Main:
...
if (answer1 == "NO") {
Console.WriteLine("Your loss");
return; // return from Main will exit the program
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
66 次 |
| 最近记录: |