我的 do while 循环 运行 多了一次(我认为)
My do while loop is running once more than it should (I think)
这是一个数据库程序,通过输入数字 0-7,您可以对数据文件执行不同的操作。每当我尝试通过输入 0 退出时,它都会让我再次进入循环,然后一旦我输入另一个 0,它就会退出。
static void Main(string[] args)
{
do
{
DoAQuery();
Console.WriteLine();
} while (DoAQuery() != "0");
}
static string DoAQuery()
{
string prompts = "0: Quit \n" +
"1: Who wrote <song name> \n" +
"2: What does <musician name> play \n" +
"3: What songs were written by <composer> \n" +
"4: Who plays in the <band name> \n" +
"5: Who's recorded <song name> \n" +
"6: What songs has the <band name> recorded \n" +
"7: Has the <band name> recorded <song name> \n";
Console.WriteLine(prompts);
Console.Write("Enter a command number: ");
string cmd = Console.ReadLine();
switch (cmd)
{
case "0" :
return cmd;
case "1" :
Case1();
return cmd;
case "2" :
Case2();
return cmd;
case "3":
Case3();
return cmd;
case "4":
Case4();
return cmd;
case "5":
Case5();
return cmd;
case "6":
Case6();
return cmd;
case "7":
Case7();
return cmd;
default:
Console.WriteLine("!!Command must be a number 0-7!!");
return "1";
}
这是它打印的内容
loaded 8 tunes
0: Quit
1: Who wrote <song name>
2: What does <musician name> play
3: What songs were written by <composer>
4: Who plays in the <band name>
5: Who's recorded <song name>
6: What songs has the <band name> recorded
7: Has the <band name> recorded <song name>
Enter a command number: 0
0: Quit
1: Who wrote <song name>
2: What does <musician name> play
3: What songs were written by <composer>
4: Who plays in the <band name>
5: Who's recorded <song name>
6: What songs has the <band name> recorded
7: Has the <band name> recorded <song name>
Enter a command number: 0
基本上我只想输入一个零立即退出程序。提前致谢!!
发生这种情况是因为您在代码中调用了 DoAQuery()
两次。
你可以这样重构它:
string response;
do
{
response = DoAQuery();
Console.WriteLine();
} while (response != "0");
因此您捕获用户选择一次,然后多次使用它。
这是一个数据库程序,通过输入数字 0-7,您可以对数据文件执行不同的操作。每当我尝试通过输入 0 退出时,它都会让我再次进入循环,然后一旦我输入另一个 0,它就会退出。
static void Main(string[] args)
{
do
{
DoAQuery();
Console.WriteLine();
} while (DoAQuery() != "0");
}
static string DoAQuery()
{
string prompts = "0: Quit \n" +
"1: Who wrote <song name> \n" +
"2: What does <musician name> play \n" +
"3: What songs were written by <composer> \n" +
"4: Who plays in the <band name> \n" +
"5: Who's recorded <song name> \n" +
"6: What songs has the <band name> recorded \n" +
"7: Has the <band name> recorded <song name> \n";
Console.WriteLine(prompts);
Console.Write("Enter a command number: ");
string cmd = Console.ReadLine();
switch (cmd)
{
case "0" :
return cmd;
case "1" :
Case1();
return cmd;
case "2" :
Case2();
return cmd;
case "3":
Case3();
return cmd;
case "4":
Case4();
return cmd;
case "5":
Case5();
return cmd;
case "6":
Case6();
return cmd;
case "7":
Case7();
return cmd;
default:
Console.WriteLine("!!Command must be a number 0-7!!");
return "1";
}
这是它打印的内容
loaded 8 tunes
0: Quit
1: Who wrote <song name>
2: What does <musician name> play
3: What songs were written by <composer>
4: Who plays in the <band name>
5: Who's recorded <song name>
6: What songs has the <band name> recorded
7: Has the <band name> recorded <song name>
Enter a command number: 0
0: Quit
1: Who wrote <song name>
2: What does <musician name> play
3: What songs were written by <composer>
4: Who plays in the <band name>
5: Who's recorded <song name>
6: What songs has the <band name> recorded
7: Has the <band name> recorded <song name>
Enter a command number: 0
基本上我只想输入一个零立即退出程序。提前致谢!!
发生这种情况是因为您在代码中调用了 DoAQuery()
两次。
你可以这样重构它:
string response;
do
{
response = DoAQuery();
Console.WriteLine();
} while (response != "0");
因此您捕获用户选择一次,然后多次使用它。