gpt4 book ai didi

c# - 重新开始这部分提问

转载 作者:太空宇宙 更新时间:2023-11-03 12:47:32 25 4
gpt4 key购买 nike

我有一个愚蠢的小编码项目。我正在尝试使用它来学习 C#,我的问题是我遇到了它,但我不知道如何使用 default 并将其返回到上一个问题到它必须询问的地方再次询问用户是否没有输入可以选择的内容。我试过循环,我一直在打破,我试过 if/else 并且我只编写了很短的时间,所以这在很大程度上是很陌生的。任何帮助,将不胜感激。

Console.WriteLine("question blah blah blah");

string choice1 = "blah1";
string choice2 = "blah2";
string choice3 = "blah3";

string userChoice = Console.ReadLine();

switch (userChoice)
{
case "1":
//Dostuff
case "2":
//Dostuff
case "3":
//Dostuff
default:
Console.WriteLine("you didn't enter something i could recognize")
break
}

最佳答案

一种选择是使用 do-while 循环和 bool 变量来控制执行。

bool loop;

do
{
Console.WriteLine("question blah blah blah");

string choice1 = "blah1";
string choice2 = "blah2";
string choice3 = "blah3";

string userChoice = Console.ReadLine();

// Set to false by default
loop = false;

switch (userChoice)
{
case "1":
//Dostuff
break;
case "2":
//Dostuff
break;
case "3":
//Dostuff
break;
default:
Console.WriteLine("you didn't enter something i could recognize");
// Set to true to iterate again.
loop = true;
break;
}
} while(loop);

关于c# - 重新开始这部分提问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36733543/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com