I want the user to insert how many sticks they want to take off from the table (from 1 to 3 sticks) and then I want the program to print out the number of sticks that are left on the table after the move.
I got this far, but I just can't understand why it asks me to put the user input twice in a row before printing the result (and the second user input is the one that counts).
import java.util.Scanner;
import java.io.InputStream;
public class GameOfSticks {
public static int sticks = 10;
public static int sticksToTake;
public static void main(String[] args) {
Scanner userInputScanner = new Scanner(System.in);
System.out.println("The number of sticks on the table is " + sticks + ".");
System.out.print("Insert how many sticks you want to take: ");
makeHumanMove(sticksToTake);
System.out.println(getNumberOfSticksOnBoard());
}
public static int getNumberOfSticksOnBoard() {
sticks = sticks - makeHumanMove(sticksToTake);
return sticks;
}
public static int makeHumanMove(int sticksToTake) {
Scanner userInputScanner = new Scanner(System.in);
while (true) {
int enteredNumber;
try {
enteredNumber = userInputScanner.nextInt();
} catch (Exception e) {
System.out.print("Not a number, enter a number from 1 to 3: ");
userInputScanner = new Scanner(System.in);
continue;
}
if (enteredNumber < 1 || enteredNumber > 3) {
System.out.print("Incorrect number, enter a number from 1 to 3: ");
continue;
} else {
sticksToTake = enteredNumber;
return(sticksToTake);
}
}
}
}