3 回答
TA贡献1804条经验 获得超7个赞
就我个人而言,我会尝试重写代码,使其更具可读性,但这样的事情应该可行:
bool keepTellingJokes = true;
while (keepTellingJokes)
{
// your joke code here
Console.WriteLine("Do you want to hear another joke?");
string option2 = Console.ReadLine();
// break out of loop
if (option2 == "no")
{
keepTellingJokes = false;
}
}
// code after escaping joke loop
免责声明:我不是每天都写 c#。
TA贡献1815条经验 获得超6个赞
流程图可以细分为三个主要部分:
提出初步问题以决定您是否应该开始讲笑话
讲笑话
结束程序
中间部分(从msgbox "joke()"
到msgbox "would you like to hear another joke?"
)使用do
/while
循环编码。使它成为一个单独的方法会很有用:
private static void TellJokes() {
bool shouldContinue;
do {
shouldContinue = true;
... // Tell a joke, ask if they liked it, and ask if they want more
if (answer == "no") {
shouldContinue = false;
}
} while (shouldContinue);
}
请注意,您可以将打印移到"Have a nice day!"程序底部,因为无论如何所有分支最终都会打印它。
- 3 回答
- 0 关注
- 187 浏览
添加回答
举报