为什么每次都要求连续2次用户输入?

时间:2015-02-23 12:18:53

标签: java user-input

我希望用户插入他们想要从桌子上取下多少根棍子(从1到3支)然后我希望程序打印出移动后留在桌子上的棍子数量。

我做到了这一点,但我无法理解为什么它要求我在打印结果之前连续两次输入用户输入(并且第二个用户输入是重要的用户输入)。

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);
            }
        }
    }
}

4 个答案:

答案 0 :(得分:1)

您正在调用方法makeHumanMove两次。进入 Main 方法,然后再进入getNumberOfSticksOnBoard方法。如果您删除方法中的makeHumanMove(sticksToTake);,那么它可以正常工作,因为它只是在getNumberOfSticksOnBoard方法中调用一次。

答案 1 :(得分:0)

它以这种方式工作,因为您正在调用makeHumanMove方法两次。第一次在main,第二次在getNumberOfSticksOnBoard。

答案 2 :(得分:0)

因为你两次调用makeHumanMove方法。 第一个

 makeHumanMove(sticksToTake);

和getNumberOfSticksOnBoard方法中的第二个;

 System.out.println(getNumberOfSticksOnBoard());

您可以尝试这样做(也可以修改它,我只尝试输入一次。);

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;

    }
}

答案 3 :(得分:0)

只需删除

行即可
makeHumanMove(sticksToTake);

在你的主方法中。 在下一行

 System.out.println(getNumberOfSticksOnBoard());
方法内的

getNumberOfSticksOnBoard()
你打电话

makeHumanMove(sticksToTake) 

一次。这就是你连续两次做输入的原因。结果也是错误的。