C# 如何使程序返回到C代码的特定行#
我正在创建一个基于文本的“选择你自己的冒险”游戏,如果在程序中选择了其他死角选项,我希望能够使程序跳回特定的代码行。我是C#新手,还在学习,所以如果这是一个简单的解决方案,请原谅我。我目前正在使用return;停止程序。这是我的代码示例C# 如何使程序返回到C代码的特定行#,c#,C#,我正在创建一个基于文本的“选择你自己的冒险”游戏,如果在程序中选择了其他死角选项,我希望能够使程序跳回特定的代码行。我是C#新手,还在学习,所以如果这是一个简单的解决方案,请原谅我。我目前正在使用return;停止程序。这是我的代码示例 Console.Write("Type OPEN or KNOCK: "); string doorChoice = Console.ReadLine(); string capDoor = d
Console.Write("Type OPEN or KNOCK: ");
string doorChoice = Console.ReadLine();
string capDoor = doorChoice.ToUpper();
Console.WriteLine();
if (capDoor == "OPEN")
{
Console.WriteLine(" The door is locked! See if one of your three keys will open it.");
Console.Write("Enter a number (1-3): ");
string keyChoice = Console.ReadLine();
//Respone to the preferred key choice
switch (keyChoice)
{
case "1":
Console.WriteLine(" You fumble getting the key into the lock, but it works!\n You open the door to find the room as if it were untouched. Strange.\n TRY AGAIN.");
return;
case "2":
Console.WriteLine(" You choose the second key. The door doesn't open.\n TRY AGAIN");
return;
case "3":
Console.WriteLine(" You choose the second key. The door doesn't open.\n TRY AGAIN");
return;
}
}
else if (capDoor == "KNOCK")
{
Console.WriteLine(" A voice behind the door speaks to you. It says, \"Answer this riddle: \"");
Console.WriteLine(" \"Poor people have it. Rich people need it. If you eat it you die. What is it?\"");
}
我希望最终让程序跳转到行控制台;这样用户就可以进行另一个选择,而不是重新启动。我肯定有一个简单的解决办法,我就是不太明白。提前谢谢 Put console.write在它自己的方法中,并随时调用该方法,例如从if语句或case语句调用。您不会让代码转到特定的行号。这不是像BASIC那样的线性规划 例如,我创建了一个名为EnterNumber()的方法: 在本例中,我设置了逻辑,要求用户在capDoor=“open”和keyChoice=1或2时输入一个数字。但关键是你可以随时要求用户输入一个数字
这有帮助吗?您可以使用do-while循环 我不确定你的退出条件是什么
int x;
do
{
bool result = int.TryParse(Console.ReadLine(), out x);
switch (x)
{
case 1:
{
// some code
break;
}
case 2:
{
some code
break;
}
default:
{
if (x == 3)
{
Console.WriteLine("Exit");
}
else
{
Console.WriteLine("Choose a number from 1 or 2");
}
break;
}
}
} while (x!=3);
您可以使用goto语句
您需要将查询放在一个循环中,只有在做出正确选择时才会离开。因为你使用的是一个开关,它在每一个案例之后都使用关键字
break
,而我提倡的是一个循环,它使用break
来离开,我们需要稍微改变结构,因为我们不能通过在开关案例中发出break
来摆脱循环
while(true){ //loop forever unless we break
Console.Write("Enter a number (1-3): ");
string keyChoice = Console.ReadLine();
//Respone to the preferred key choice
switch (keyChoice)
{
case "1":
Console.WriteLine(" You fumble ...");
break; //break out of the switch but not the loop
case "2":
Console.WriteLine(" You choose ...");
continue; //restart the loop
case "3":
Console.WriteLine(" You choose ...");
continue; //restart the loop
default:
Console.WriteLine(" That isn't a valid key number, enter 1, 2 or 3");
continue; //restart the loop
}
break; //break out of the loop
}
我已经修改了您的开关,使其具有默认情况;之前,您的代码将无法处理输入垃圾的用户
我们还可以使用一个变量控制循环,当我们想要停止循环时,我们可以设置该变量:
bool keepLooping = true;
while(keepLooping){
Console.Write("Enter a number (1-3): ");
string keyChoice = Console.ReadLine();
switch (keyChoice)
{
case "1":
Console.WriteLine(" You fumble ...");
keepLooping = false; //stop the loop from running next time
break;
case "2":
Console.WriteLine(" You choose ...");
break;
case "3":
Console.WriteLine(" You choose ...");
break;
default:
...
}
}
或者您可以放弃开关/外壳,使用if
和break
退出循环:
while(true){
Console.Write("Enter a number (1-3): ");
string keyChoice = Console.ReadLine();
if(keyChoice == "1"){
Console.WriteLine(" You fumble ...");
break; //exit the loop
} else
Console.WriteLine(" You choose ...");
}
如果用户输入了错误的密钥或垃圾,则只会发出“错误密钥”消息
尽量不要把你的代码看作是“如果”的话,而是更像是“在某些条件不满足时重复这段代码”——这是一个微妙的区别,但它会鼓励你考虑你需要做的循环
聚苯乙烯;如果你能提出问题并给出回答,你的生活就会变得简单一些:
public static string Ask(string question){
Console.WriteLine(question + " ");
return Console.ReadLine();
}
像这样使用它:
string keyChoice = Ask("Enter a key 1-3:");
int keyChoice = AskNumber("Which key? Enter 1, 2 or 3", 1, 3);
我们可以改进措施,防止用户进入垃圾:
public static int AskNumber(string question, int lower, int upper){
Console.WriteLine(question + " ");
int result; //variable for the result
bool isNumber = int.TryParse(Console.ReadLine(), out result); //try turning the string into a number
//while not a number was entered or number was out of range
while(!isNumber || result < lower || result > upper) {
//repeat the question
Console.WriteLine(question + " ");
//try parse their input again
isNumber = int.TryParse(Console.ReadLine(), out result);
}
return result;
}
您可以确定响应将是1、2或3,这样您就不必在每个开关中处理垃圾等虽然使用
While(){}
循环的建议确实是正确的,而且在高级语言中非常可取,但仍然有可能完全按照您在c#中的要求执行,通过使用标签和goto
命令:
<some code>
My_label:
<some other code>
goto My_label;
My_标签:
转到我的标签;
注意,这是一个相当低级的构造,因此完全由您来确保不会出现任何问题(例如,这可能会在没有通知的情况下创建一个无休止的循环…)。
尽管如此,它还是一个有用的构造,例如可以用来跳出嵌套的代码块
检查Microsoft文档:
假设你想要一个循环。考虑你想回到一个“特定的部分”而不是一个特定的“行”。它将帮助您更好地维护。对于您的用例,使用循环和函数应该足够好。不要将其视为让程序跳回,而应将其视为执行循环的程序,例如使用或,直到没有更多的工作要做。goto的经验法则:除非您绝对必须使用它。即使这样,至少也要三思。后藤的经验法则:除非你万不得已,否则不要使用它。即便如此,至少要三思。在我的整个C#编码生涯中,“自1.0发布以来”,我从未使用过这两种说法
do
并不是特别有害,但向初学者推荐goto
绝对是错误的做法,因为它将他们的学习导向错误的方向。如果我在POC上变得懒惰,我会使用goto。或者如果我想让一段代码不可读。如果EnterNumber做了一些事情,比如获取输入、检查它是一个数字、反馈它等等,这将是一个有用的点。。但这并不能回答问题是的,我本可以花更多的时间来回答,但我只是想证明他是如何根据条件而不是具体的线路号进行分支的。我不想给他编逻辑。但是谢谢你的反馈。
int keyChoice = AskNumber("Which key? Enter 1, 2 or 3", 1, 3);
<some code>
My_label:
<some other code>
goto My_label;