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

问题描述 投票:0回答:4

这个问题在这里已有答案:

我希望用户插入他们想要从桌子上取下多少根棍子(从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);
            }
        }
    }
}
java user-input
4个回答
1
投票

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


0
投票

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


0
投票

只需删除该行

makeHumanMove(sticksToTake);

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

 System.out.println(getNumberOfSticksOnBoard());

在方法内部

getNumberOfSticksOnBoard()

你打电话

makeHumanMove(sticksToTake) 

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


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;

    }
}
© www.soinside.com 2019 - 2024. All rights reserved.