2015-05-09 23 views
1

我想不通为什么我的结局是不是:Java的基本整数数组

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 

异常在线程 “主” java.lang.ArrayIndexOutOfBoundsException:7 在day7.main(day7.java:14)

+0

呼叫我++;在System.out.println(arrayZ [i])之后; – DoubleMa

+0

单步执行调试器中的代码(例如,使用IntelliJ IDEA),您将看到它出错的地方。 – Jerry101

+0

当你使用'while'循环时,依赖于inc/decrement sentinel值(即:在你的情况下为'i')for循环通常是更简洁的选项。 'for(int i = 0; i <= 6; ++ i){...}'。 – ChiefTwoPencils

回答

1

刚刚切换线之间我++和的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])); 
} 
+0

谢谢:)现在我明白了 –

1

改成这样:

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

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

+0

这就是我的想法。 :3 –

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
阵列的每个条目是:0 1 2 3 4 5 6
而且,所有阵列元素的总和为:21

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

+0

非常感谢。您的评论非常清晰,非常有帮助。 –

+0

很高兴帮助你:) –

+0

你只需要将i ++替换为System.out行......因为它的数组将绑定到i ++的最后一个索引上。 –

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])); 
    

    } while循环代码

-1

变化如下

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