我得到的錯誤數組索引超出範圍! Java的運行計數
java.lang.ArrayIndexOutOfBoundsException:20
我不知道如果我想有一個while循環或打破它,所以它沒有按」噸超過陣列的長度。我看到其他職位,說明爲什麼發生,但我沒有看到任何顯示如何解決它。請幫忙!
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package secondarray;
import java.util.Random;
/**
*
* @author iii
*/
public class SecondArray {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Random generator = new Random();
int[]run= new int[20];
for (int i= 0;i<run.length;i++)
{
run[i]=generator.nextInt(6)+1;
}
marking(run);
}
public static void marking(int[] run){
for (int i =0;i<run.length;i++){
while(run[i]<= run[20]){
if(run[i]==run[i-1]&&run[i]==run[i+1]){
System.out.print(run[i]);
}
if(run[i]!=run[i-1]&&run[i]==run[i+1]){
System.out.print("("+run[i]);
}
if(run[i]==run[i-1]&& run[i]!=run[i+1]){
System.out.print(run[i]+")");
}
if(run[i]!=run[i-1]&& run[i] == run[i+1]){
System.out.print(run[i]);
}
}
}
}
}
'對(INT I = 0;我<= run.length;我++){'應該是'對(INT I = 0; I < run.length - 1; i ++){'自從您訪問'i + 1'後。你也應該從'i = 1'開始,因爲你也可以訪問'i-1'。 –
你的for(int i = 0; i <= run.length; i ++){'應該是''<='就像它在'for(int i = 0; i