暂停代码并等待输入java

时间:2016-01-14 17:56:45

标签: java arraylist

我现在正在为学校开始编程,我正在努力改进我已经完成的作业。我想要做的是向用户请求x次信息,并将该信息放在ArrayList问题中,我需要等到用户在代码前使用Scanner输入信息可以继续,但它试图继续,我得到一个越界错误。

class Marbles {
    private static List _colors = new ArrayList<String>();
    private static List _numbers = new ArrayList<Integer>();
    private static Scanner _input;

    Marbles(Scanner input) {
        _input = input;
        int marbleCount = getMarbleCount(); // => 5
        int marbleIndex = 0;

        //If you don't want to play, that's okay too.

        if (marbleCount == 0) {
            System.out.print("Goodbye!");
            System.exit(0);
        }

        //populate the marble arrays
        for (; marbleCount > 0; marbleCount--) {

            String color;
            Integer numberOfMarbles;
            //Count the marbles up from 1 to display out
            int currentMarble = marbleIndex + 1;
            //current index to fill the arrays
            System.out.printf("Color for marble %d: ", currentMarble);
            color = _input.nextLine();

            //it should pause here but it isn't

            System.out.printf("Number of %s marbles: ", _colors.get(marbleIndex));

            _colors.add(color);
            _numbers.add(currentMarble);
            marbleIndex++;
        }
    }
    ...
}

0 个答案:

没有答案