-1

我在大学项目中遇到了 for 循环的问题。我的循环试图从 kb 读取和存储值并存储在数组中。我已经为我的数组创建了最大值,但是当我到达最终输入时,循环超出了界限。我知道这可能很简单,但无法从以前回答的问题中找到解决方案。任何帮助将非常感激。

    public void getAu() {
    final int max = 7;
    String[] days = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday" +
            "Saturday", "Sunday"};
    int[] totalAu = new int[max];

    for (int index = 0; index <= max; index++) {
        System.out.println("Please enter day of the week: ");
        days [index] = kb.next();
        System.out.println("Please enter Au: " );
        totalAu [index] = kb.nextInt();
        
     }//for
4

1 回答 1

0

包含 N 个元素的数组的索引介于 0 和 N-1 之间。您应该继续循环index < max,而不是index <= max

for (int index = 0; index < max; index++) {
    // Here --------------^
于 2020-12-05T12:46:56.260 回答