哪种解决方案最适合打破用户输入循环?
Which solution is best for breaking from a loop on user input?
我目前正在学习编程,我经常偶然发现你在控制台中输入不确定数量的行的任务(每行都是一个命令,我稍后必须在 main 中使用 switch 或 if-else 语句过滤掉)和最后你输入 "END" 表示你将停止写行(通常它会导致程序结束但有时我必须打印一些最终代码)。在最长的时间里,我使用此解决方案来选择所需的选择:
while (true) {
var input = Console.ReadLine().Split();
if (input[0] == "END") break;
switch (input[0]) {
//Task specific cases
}
}
但是今天我突然想到了一个主意。 (在我做了一些研究之后)我可以使用单词 return 在 switch 语句中添加 "END" case。唯一的问题是因为我的代码在 main 中,它会自动结束程序。解决方案是将代码转移到另一种方法,但是这个解决方案更好还是有点过于复杂?
*这是第二种解决方案的样子:
static void Main(string[] args) {
Logic();
//Other stuff to do, if the task requires it
}
public static void Logic() {
while (true) {
var info = Console.ReadLine().Split();
switch (info[0]) {
case "END": return;
//Other task specific cases
}
}
}
**如果 post 读起来很混乱,我很抱歉,我对编程术语不太了解。
使用 break 或 return 的任何解决方案都是正确的,但具有不同的行为。您可以使用您认为更好的那个。
The break statement is used to terminate the loop or statement in
which it present. After that, the control will pass to the statements
that present after the break statement, if available. If the break
statement present in the nested loop, then it terminates only those
loops which contains break statement.
The return statement terminates the execution of the method and
returns the control to the calling method. It returns an optional
value. If the type of method is void, then the return statement can be
excluded.
您可以在这里阅读更多内容:https://www.geeksforgeeks.org/c-sharp-jump-statements-break-continue-goto-return-and-throw/
这是另一种可能的实现方式:
public static void Logic()
{
bool exit = false;
do
{
var info = Console.ReadLine().Split();
switch (info[0])
{
case "END":
exit = true;
break;
}
}
while (!exit);
}
我目前正在学习编程,我经常偶然发现你在控制台中输入不确定数量的行的任务(每行都是一个命令,我稍后必须在 main 中使用 switch 或 if-else 语句过滤掉)和最后你输入 "END" 表示你将停止写行(通常它会导致程序结束但有时我必须打印一些最终代码)。在最长的时间里,我使用此解决方案来选择所需的选择:
while (true) {
var input = Console.ReadLine().Split();
if (input[0] == "END") break;
switch (input[0]) {
//Task specific cases
}
}
但是今天我突然想到了一个主意。 (在我做了一些研究之后)我可以使用单词 return 在 switch 语句中添加 "END" case。唯一的问题是因为我的代码在 main 中,它会自动结束程序。解决方案是将代码转移到另一种方法,但是这个解决方案更好还是有点过于复杂?
*这是第二种解决方案的样子:
static void Main(string[] args) {
Logic();
//Other stuff to do, if the task requires it
}
public static void Logic() {
while (true) {
var info = Console.ReadLine().Split();
switch (info[0]) {
case "END": return;
//Other task specific cases
}
}
}
**如果 post 读起来很混乱,我很抱歉,我对编程术语不太了解。
使用 break 或 return 的任何解决方案都是正确的,但具有不同的行为。您可以使用您认为更好的那个。
The break statement is used to terminate the loop or statement in which it present. After that, the control will pass to the statements that present after the break statement, if available. If the break statement present in the nested loop, then it terminates only those loops which contains break statement.
The return statement terminates the execution of the method and returns the control to the calling method. It returns an optional value. If the type of method is void, then the return statement can be excluded.
您可以在这里阅读更多内容:https://www.geeksforgeeks.org/c-sharp-jump-statements-break-continue-goto-return-and-throw/
这是另一种可能的实现方式:
public static void Logic()
{
bool exit = false;
do
{
var info = Console.ReadLine().Split();
switch (info[0])
{
case "END":
exit = true;
break;
}
}
while (!exit);
}