I'm creating a text based Choose your own adventure game and I want to be able to make the program jump back to a specific line of code if an otherwise dead end option is chosen within the program. I'm new to C# and still learning so forgive me if this is a simple solution. I'm currently using return; to just stop the program. Here is a sample of my code...
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?\"");
}
I would like to ultimately have the program jump to the line Console.Write("Enter a number (1-3): "); So that the user can just make another selection rather than restart. I'm sure there is an easy solution, I just cant quite figure it out. Thanks in advance!
You need to put your querying in a loop that you only leave when the correct choice is made. Because you're using a switch, which uses the keyword break
after every case, and I'm advocating a loop, which uses break
to leave, we need to structure things slightly differently because we can't get out of the loop by issuing a break
inside a switch case
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
}
I've modified your switch to have a default case; before your code would not have been able to handle the user entering garbage
We can also control a loop with a variable that we set when we want to stop looping:
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:
...
}
}
Or you can ditch the switch/case and use if
and break
to exit the loop:
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 ...");
}
This just issues the "wrong key" message if the user enters either the wrong key, or garbage.
Try not to see your code as "go to point X if" but more like "repeat this section of code while some condition isn't met" - it's a subtle difference, but one that will encourage you to think about the looping you need to make
ps; your life will get somewhat simpler if you make a method that asks questions and gives the response back:
public static string Ask(string question){
Console.WriteLine(question + " ");
return Console.ReadLine();
}
Use it like:
string keyChoice = Ask("Enter a key 1-3:");
We can improve things to prevent users entering garbage:
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;
}
This is another example of code that is "loop until a desirable condition is met" - the desirable condition being the user enters a valid input
Use it like:
int keyChoice = AskNumber("Which key? Enter 1, 2 or 3", 1, 3);
You can be sure the response will be 1, 2 or 3 so you don't have to handle garbage in each switch etc
While the suggestions to use a while(){}
loop are indeed correct, and very advisable in a high level language, there still is the possibility to do exactly what you require in c#, by using labels and the goto
command:
<some code>
My_label:
<some other code>
goto My_label;
Watch out, this is a rather low-level construct, and as such it's entirely up to you to ensure that no problems arise (eg this might create an endless loop without notice...). Still, it's an useful construct that for example can be used to jump out of nested blocks of code.
Check Microsoft docs: .NET docs
You can use a do-while loop I am not sure what is your exit condition
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);
You can use goto statement https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/goto
using System;
public class Program
{
public static void Main()
{
Console.Write("Type OPEN or KNOCK: ");
string doorChoice = Console.ReadLine();
string capDoor = doorChoice.ToUpper();
Console.WriteLine();
if (capDoor == "OPEN")
{
One:
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.");
goto One;
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 in it's own method and call that method whenever you want, such as from an if statement or case statement. You don't make code go to a specific line number. This isn't linear programming like BASIC.
For example, I created a method called EnterNumber():
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.");
EnterNumber();
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.");
EnterNumber();
return;
case "2":
Console.WriteLine(" You choose the second key. The door doesn't open.\n TRY AGAIN");
EnterNumber();
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?\"");
}
private void EnterNumber()
{
Console.Write("Enter a number (1-3): ");
}
In this case, I have set the logic to ask the user to enter a number if capDoor = "open" and if keyChoice = 1 or 2. But the point is you can ask the user to enter a number whenever you want.
Does that help?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.