Java基本整数数组

时间:2015-05-09 03:17:35

标签: java arrays

我无法弄清楚为什么我的结果不是:

Your first array element is: 0
1
2
3
4
5
6

(预期产出)

我的代码:

public class day7 {
    public static void main(String[] args){
        int arrayZ[] = new int[7];

        arrayZ[0] = 0;
        System.out.println("Your first array element is: " + arrayZ[0]);

        int i = 1;
        while (i <= 6){
            arrayZ[i] = i;
            i++;
        System.out.println(arrayZ[i]);
        }
    System.out.println("Each entry of the array is: " + arrayZ[0] + " " + arrayZ[1] + " " + arrayZ[2] + " " + arrayZ[3] + " " + arrayZ[4] + " " + arrayZ[5] + " " + arrayZ[6]);
    System.out.println("And, the sum of all array elements are: " + arrayZ[0] + arrayZ[1] + arrayZ[2] + arrayZ[3] + arrayZ[4] + arrayZ[5] + arrayZ[6]);
    }
}

结果/实际产出:

Your first array element is: 0
0
0
0
0
0
  

线程中的异常&#34; main&#34; java.lang.ArrayIndexOutOfBoundsException:7     在day7.main(day7.java:14)

5 个答案:

答案 0 :(得分:2)

您的代码是正确的,问题依次为incrementingprinting

public static void main(String[] args) {
    int arrayZ[] = new int[7];

    arrayZ[0] = 0;
    System.out.println("Your first array element is: " + arrayZ[0]);

    int i = 1;
    while (i <= 6)  {
        arrayZ[i] = i;
        System.out.println(arrayZ[i]);
        i++;
    }
    System.out.println("Each entry of the array is: " + arrayZ[0] + " " + arrayZ[1] + " " + arrayZ[2] + " " + arrayZ[3] + " " + arrayZ[4] + " " + arrayZ[5] + " " + arrayZ[6]);
    System.out.println("And, the sum of all array elements are: " + (arrayZ[0] + arrayZ[1] + arrayZ[2] + arrayZ[3] + arrayZ[4] + arrayZ[5] + arrayZ[6]));
}

此外,您需要先对项目求和,然后需要使用括号,然后使用+String将其他变量转换为String并显示连接结果。输出是:

  

你的第一个数组元素是:0
  1
  2
  3
  4
  5
  6
  阵列的每个条目是:0 1 2 3 4 5 6
  并且,所有数组元素的总和是:21

如果你有一个固定的循环计数器边界(在你的情况下是1到6),那么最好使用for循环。

答案 1 :(得分:1)

只需切换i ++和System.out.println(arrayZ [i])之间的界限;

如果你想计算总和,只需将它们添加到一个新变量中或使用();

public static void main(String[] args) {
    int arrayZ[] = new int[7];

    arrayZ[0] = 0;
    System.out.println("Your first array element is: " + arrayZ[0]);

    int i = 1;
    while (i <= 6) {
        arrayZ[i] = i;
        System.out.println(arrayZ[i]);
        i++;
    }
    System.out.println("Each entry of the array is: " + arrayZ[0] + " " + arrayZ[1] + " " + arrayZ[2] + " " + arrayZ[3] + " " + arrayZ[4] + " " + arrayZ[5] + " " + arrayZ[6]);
    System.out.println("And, the sum of all array elements are: " + (arrayZ[0] + arrayZ[1] + arrayZ[2] + arrayZ[3] + arrayZ[4] + arrayZ[5] + arrayZ[6]));
}

答案 2 :(得分:1)

更改为:

while (i <= 6){
    arrayZ[i] = i;
    System.out.println(arrayZ[i]);
    i++;
}

因为你使用它的方式,它会尝试打印不存在的arrayZ [7]。

答案 3 :(得分:1)

  • 只需切换i ++和System.out.println(arrayZ [i])之间的界限;

  • 如果你想计算总和,只需将它们添加到一个新变量或 use();

    public static void main(String[] args) {
    int arrayZ[] = new int[7];
    
    arrayZ[0] = 0;
    System.out.println("Your first array element is: " + arrayZ[0]);
    
    int i = 1;
    while (i <= 6) {
        arrayZ[i] = i;
        System.out.println(arrayZ[i]);
        i++;
    }
    System.out.println("Each entry of the array is: " + arrayZ[0] + " " + arrayZ[1] + " " + arrayZ[2] + " " + arrayZ[3] + " " + arrayZ[4] + " " + arrayZ[5] + " " + arrayZ[6]);
    System.out.println("And, the sum of all array elements are: " + (arrayZ[0] + arrayZ[1] + arrayZ[2] + arrayZ[3] + arrayZ[4] + arrayZ[5] + arrayZ[6]));
    

    }

答案 4 :(得分:-1)

更改while循环代码如下

while (i <= 6){
    arrayZ[i] = i;
    System.out.println(arrayZ[i]);
    i++;
}