I am trying to make a small game:
There are 2 heroes you can choose: 1 Warrior and 2 Mage.
Next, you should choose how to travel: 1 by Horse 2 Teleportation (available to Mage only).
Finally, choose a weapon: 1 Sword 2 Staff (*Mage can only use Staff; Warrior can use both).
I created a loop for my first question (choosing a hero) so that if the user enters something else aside from 1 or 2, the program will repeat the question ("Choose your hero: ...). I need the same done for my second and third question (especially since there are some restrictions, e. g. if the user chose Warrior, he can't choose Teleportation as his travel option).
public static void main(String[] args) {
int hero, travel, weapon;
Scanner scan = new Scanner(System.in);
loop:
while (true) {
System.out.println("Choose your hero: 1 for Warrior, 2 for Mage");
hero = scan.nextInt();
switch (hero) {
case 1:
System.out.println("Choose your travel option: 1 for Horse; 2 for Teleportation");
travel = scan.nextInt();
break loop;
case 2:
System.out.println("Choose your travel option: 1 for Horse; 2 for Teleportation");
travel = scan.nextInt();
break loop;
default:
break;
}
}
}
I don't know how to use a loop inside another loop properly. I've tried several options but it always returns an error.