我寫了一個代碼,但我找不到輸出中顯示的錯誤。該程序輸入一些數字,我把它放在一個數組中。線程「主」java.lang.ArrayIndexOutOfBoundsException異常,數組數組
它有三個方法:
- 倒置的陣列;
- 找到最大值和最小值;
- 插入數字的平均值。
這裏是我的代碼:
package sequenza;
import java.util.Scanner;
public class SequenzaClass {
Scanner input = new Scanner(System.in);
int[] numeri;
int dim;
int max;
int min;
int media;
int somma;
int DIMENSIONE_MAX;
public SequenzaClass(int dim, int max, int min, int media, int somma, int DIMENSIONE) {
this.dim = dim;
this.max = max;
this.min = min;
this.media = media;
this.somma = somma;
this.DIMENSIONE_MAX = 10;
}
SequenzaClass() {
numeri = new int[DIMENSIONE_MAX];
int dim = 0;
int max = 0;
int min = 0;
int media = 0;
int somma = 0;
int DIMENSIONE_MAX = 10;
}
public Scanner getInput() {
return input;
}
public void setInput(Scanner input) {
this.input = input;
}
public void Inserisci(int dim) {
System.out.print("How many numbers do you want to insert? ");
dim = input.nextInt();
int i;
for (i = 0; i < dim; i++) {
System.out.print("Number for position [" + i + "]:");
numeri[i] = input.nextInt();
}
}
public int getDim() {
return dim;
}
public void setDim(int dim) {
this.dim = dim;
}
public int[] getNumeri() {
return numeri;
}
public void setNumeri(int[] numeri) {
this.numeri = numeri;
}
public int getMax() {
int max = numeri[0];
int i;
for (i = 0; i < dim; i++) {
if (numeri[i] > max) {
max = numeri[i];
}
}
return max;
}
public void setMax(int max) {
this.max = max;
}
public int getMin() {
int min = numeri[0];
int i;
for (i = 0; i < dim; i++) {
if (numeri[i] < min) {
min = numeri[i];
}
}
return min;
}
public void setMin(int min) {
this.min = min;
}
public int getSomma() {
int i;
for (i = 0; i < dim; i++) {
somma = somma + numeri[i];
}
return somma;
}
public void setSomma(int somma) {
this.somma = somma;
}
public int getMedia() {
media = getSomma()/dim;
return media;
}
public void setMedia(int media) {
this.media = media;
}
public int getDIMENSIONE_MAX() {
return DIMENSIONE_MAX;
}
public void setDIMENSIONE_MAX(int DIMENSIONE_MAX) {
this.DIMENSIONE_MAX = DIMENSIONE_MAX;
}
public void Contrario(int dim) {
int i;
for (i = 0; i < (dim/2); i++) {
int k = numeri[dim - i - 1];
numeri[dim - i - 1] = k;
}
System.out.println("The inverted vector is: " +numeri[i]);
}
}
package sequenza;
import java.util.Scanner;
public class main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int DIMENSIONE_MAX = 10;
int[] numeri = new int[DIMENSIONE_MAX];
int dim = 0;
int max = 0;
int min = 0;
int media = 0;
int somma = 0;
int scelta = 1;
SequenzaClass sequenza1 = new SequenzaClass();
sequenza1.Inserisci(dim);
while (scelta != 0) {
System.out.println("1) Invert numbers");
System.out.println("2) Max and min");
System.out.println("3) Avarage");
scelta = input.nextInt();
switch (scelta) {
case 1:
sequenza1.Contrario(dim);
break;
case 2:
System.out.println(sequenza1.getMax());
System.out.println(sequenza1.getMin());
break;
case 3:
System.out.println(sequenza1.getMedia());
break;
}
}
}
}
發佈完整的stacktrace,以便我們可以看到哪行代碼拋出異常。 – csmckelvey