因此,我有此作业需要循环用户输入6次。循环完成后,再次循环3次。我之前没有添加for lop,所以我不知道该如何处理。

这是该方法的代码:

public static int[] getPlayerNumbers(int[] playNums) {
    Scanner input = new Scanner(System.in);

    for (int i = 0; i < playNums.length; i++) {
        System.out.println("Please enter numbers from 1-9: " + i);
        playNums[i] = input.nextInt();

        while (playNums[i] < 1 || playNums[i] > 9) {
            System.out.println("Invlaid input. Please only enter 1-9. ");
            playNums[i] = input.nextInt();
        }
    }

    return playNums;
}


我放置了i以查看索引,然后将它移到05,然后返回到0。我没办法了,请帮忙。

最佳答案

看来您的playNums超过了6。

public static int[] getPlayerNumbers(int[] playNums) {
Scanner input = new Scanner(System.in);

for (int i = 0; i < 6; i++) {
    System.out.println("Please enter numbers from 1-9: " + i);
    playNums[i] = input.nextInt();

    while (playNums[i] < 1 || playNums[i] > 9) {
        System.out.println("Invlaid input. Please only enter 1-9. ");
        playNums[i] = input.nextInt();
    }
}

return playNums;}

09-18 14:45