Reputation: 43
public class WereWolfenstein2D {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
char c;
String input;
String difficulty;
int difficulty1;
String easy;
System.out.println("Do you want to play? y/n");
input = sc.nextLine( );
input = input.toLowerCase();
if (input.equals("y")) {
System.out.println("Choose Difficulty ");
System.out.println(" easy: type 1");
System.out.println(" medium: type 2");
System.out.println(" hard: type 3");
difficulty = sc.nextLine( );
}
}
}
I have this code and I want to have it so if the user enters 1 then a bunch of stuff happens, 2 something else happens etc. But I'm not sure how to go about it, I'm not entirely sure how to place the if else statements or how to tell the computer to go to said statement depending on what the user types.
Upvotes: 0
Views: 61
Reputation: 44965
Here is how you should proceed
int difficulty = sc.nextInt();
switch (difficulty) {
case 1:
// code for easy
break;
case 2:
// code for medium
break;
case 3:
// code for hard
break;
default:
throw new IllegalArgumentException("Unknown Difficulty");
}
Upvotes: 3
Reputation: 57
You can try to extract the character at the index 0:
char input= difficulty.charAt(0);
That allows you to work with only the number that the user has typed and then you can use regular if/else statements to do your bidding or use switch statements as well.
Upvotes: 0