How do I write a general user input statement to be used multiple times with different values?
That is your solution my man!
I put your switch in a do...while loop, so after every user choice you can ask him/her if they want to choose another step, and it perfectly run after the user doesn't want to continue.
import java.util.Scanner;
public class AdventureTest {
private static Scanner input = new Scanner(System.in);
private static void text(String body) {
System.out.println("" + body);
}
public static void main(String[] args) {
char direction,answer = 0;
do{
System.out.print("Go n,e,s or w?: ");
direction = input.nextLine().charAt(0);
switch( direction )
{
case 'n': {
text("You go North");
System.out.println("Do you want to continue? (y/n)");
answer = input.nextLine().charAt(0);
}
break;
case 'e': {
text("You go East");
System.out.println("Do you want to continue? (y/n)");
answer = input.nextLine().charAt(0);
}
break;
case 's':{
text("You go South");
System.out.println("Do you want to continue? (y/n)");
answer = input.nextLine().charAt(0);
}
break;
case 'w':{
text("You go West");
System.out.println("Do you want to continue? (y/n)");
answer = input.nextLine().charAt(0);
}
break;
default:
text("You fall down a hole!");
System.exit(0);
}
}while(answer == 'y');
text("Program continues");
}
}
How to ask for user input a limited number of times in C#
I am using a label to return to the beginning of the loop. Is this a bad idea?
Yes. Today would be a great day to learn about while
. Two ways to think about while
:
(1) You know what if
does. if(condition) statement
evaluates condition
, and if it is true, executes statement
once.
while
does almost the same thing. while(condition) statement
evaluates condition
, and if it is true, executes statement
, and then checks condition
again, ... and so on until condition
is false.
(2) If you understand goto, you can express while
in terms of goto
. while(condition) statement
is the same as:
Continue: if (!condition) goto Break;
statement
goto Continue;
Break:;
If you can program with goto
, you can program the same thing more clearly with while
.
In your particular case though, you likely want to use a do
loop, which is an upside-down while
:
do statement while(condition);
has the behavior of "do the statement, then check the condition; if true, do the statement again... until the condition is false. do
has the semantics:
Restart: statement
Continue: if (condition) goto Restart;
Break:;
How to print user input multiple times in multiple lines
Using a loop
for i in range(height):
print(mark)
Related Topics
Get Discord User Id from Username
How to Count the Number of Messages
Best Way to Get the Max Value in a Spark Dataframe Column
How to Allocate Array With Shape and Data Type
How to Make a Tkinter Label Background Transparent
Macos: How to Downgrade Homebrew Python
How to Clear/Delete the Contents of a Tkinter Text Widget
Python: How to Check If Cell in CSV File Is Empty
Python Selenium - Element Is Not Currently Interactable and May Not Be Manipulated
How to Change Python Version in Anaconda Spyder
Replace Single Quote With Double Quote in a String Python
How to Divide Each Column of Pandas Dataframe by a Series
Python Format Size Application (Converting B to Kb, Mb, Gb, Tb)
How to Verify If a Button Is Enabled and Disabled in Webdriver Python