0

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);
            }
        }
    }
}
Wojciech Wirzbicki
  • 3,887
  • 6
  • 36
  • 59
Tymu888
  • 29
  • 6

4 Answers4

1

You are calling the method makeHumanMove twice. Once in the Main method and then again in the getNumberOfSticksOnBoard method. If you remove makeHumanMove(sticksToTake); in the Main method then it works fine, as it just calls it once in the getNumberOfSticksOnBoard method.

James Fox
  • 691
  • 7
  • 24
0

It works this way because you are calling makeHumanMove method twice. First time in main, second time in getNumberOfSticksOnBoard.

MJ01
  • 21
  • 1
0

Because you're calling makeHumanMove method twice. First one

 makeHumanMove(sticksToTake);

and second one inside the getNumberOfSticksOnBoard method;

 System.out.println(getNumberOfSticksOnBoard());

You can try like this(Also you can modify this, I only try toput input for once. );

public class GameOfSticks {
    public static int sticks = 10;
    public static int sticksToTake;

    public static void main(String[] args) {
        System.out.println("The number of sticks on the table is " + sticks + ".");
        System.out.print("Insert how many sticks you want to take: ");
        Scanner userInputScanner = new Scanner(System.in);
        int enteredNumber;
        while (true) {

            try {
                enteredNumber = userInputScanner.nextInt();
            } catch (NumberFormatException e) {
                System.out.print("Not a number, enter a number from 1 to 3: ");
                userInputScanner = new Scanner(System.in);
                continue;
            }
            makeHumanMove(enteredNumber);
            System.out.println(getNumberOfSticksOnBoard());
        }



    }

    public static int getNumberOfSticksOnBoard() {
        sticks = sticks - makeHumanMove(sticksToTake);
        return sticks;
    }

    public static int makeHumanMove(int enteredNumber) {

        if (enteredNumber < 1 || enteredNumber > 3) {
            System.out.print("Incorrect number, enter a number from 1 to 3: ");
        } else {
            sticksToTake = enteredNumber;
            return (sticksToTake);
        }
        return sticksToTake;

    }
}
Semih Eker
  • 2,389
  • 1
  • 20
  • 29
0

Just remove the line

makeHumanMove(sticksToTake);

in your main-method. On the next line

 System.out.println(getNumberOfSticksOnBoard());

inside the method

getNumberOfSticksOnBoard()

you call

makeHumanMove(sticksToTake) 

again. Thats the reason you get to do an input twice in a row. The result is wrong this way as well.

Officer Bacon
  • 724
  • 1
  • 7
  • 22