帮助我!!!!!!!:线程“主”中的例外情况java.lang.ArrayIndexOutOfBoundsException:0 [英] Help me!!!!!!!: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
本文介绍了帮助我!!!!!!!:线程“主”中的例外情况java.lang.ArrayIndexOutOfBoundsException:0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我构建项目时,我的主课有问题..
我收到消息:
[성적처리프로그램]
몇명의학생들의数据를입력하시겠습니까?
2
1번째학생
이름을입력하세요:
chos
线程main中的异常java.lang.ArrayIndexOutOfBoundsException:0
HW02_2的
。 addinfo(HW02_2.java:18)
$ 03 $ b在HW02_2.main(HW02_2.java:95)
I have a problem with my main class when I build my project..
I get message :
[성적처리 프로그램]
몇명의 학생들의 data를 입력하시겠습니까?
2
1번째 학생
이름을 입력하세요 :
chos
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at HW02_2.addinfo(HW02_2.java:18)
at HW02_2.main(HW02_2.java:95)
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
public class HW02_2 {
public static void addinfo(int num, int[][] array) throws IOException{ // InputStream 객체에 리더를 연결
int student_num = num;
for(int i = 0; i < num ; i++){
//get name info
int intname = 0;
InputStreamReader s = new InputStreamReader(System.in);
BufferedReader in = new BufferedReader(s);
System.out.println(i+1+"th student\n enter name : ");
intname = s.read();
array[i][0] = intname;
// codenumber는?[1]
System.out.println("enter code number : ");
Scanner codenum = new Scanner(System.in);
array[i][1] = codenum.nextInt();
codenum.close();
//korean score[2]
System.out.println("enter korean score : ");
Scanner korscore = new Scanner(System.in);
array[i][2] = korscore.nextInt();
korscore.close();
//English score[3]
System.out.println("enter english score : ");
Scanner engscore = new Scanner(System.in);
array[i][3] = engscore.nextInt();
engscore.close();
//math score[4]
System.out.println("enter math score : ");
Scanner mathscore = new Scanner(System.in);
array[i][4] = mathscore.nextInt();
mathscore.close();
}
show(student_num, array);
}
static void show(int num, int[][] array){
for(int i = 0; i < num ; i++){
calculate(i, array);
}
// sorting by average
int temp[] = null;
for(int i = 0; i < num-1; i++){
for(int j = i+1; j < num; j++){
if(array[i][6] < array[j][6]){
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}
// print result
System.out.println("[Result]");
for(int i = 0; i < num; i++){
char grade = 'F';
if(array[i][6] >= 90){
grade = 'A';
}else if(array[i][6] >= 80 && array[i][6] <= 89){
grade = 'B';
}else if(array[i][6] >= 70 && array[i][6] <= 79){
grade = 'C';
}else if(array[i][6] >= 60 && array[i][6] <= 69){
grade = 'D';
}else if(array[i][6] < 60){
grade = 'F';
}
System.out.print((char)array[i][0]+""+array[i][1]+" "+array[i][2]+" "
+array[i][3]+" "+array[i][4]+" "+array[i][5]+" "+array[i][6]+" "+grade+" "+i+1);
}
}
static void calculate(int i, int[][]array){
//total[5]
for(int j = 2; j == 4; j++){
array[i][5] += array[i][j];
}
//average[6]
array[i][6] = array[i][5] / 3;
}
public static void main(String[] args) throws IOException {
int[][] studentinfo = {};
int students_num = 0;
System.out.println("[성적처리 프로그램]\n몇명의 학생들의 data를 입력하시겠습니까?");
Scanner s = new Scanner(System.in);
students_num = s.nextInt();
addinfo(students_num, studentinfo);
s.close();
}
}
推荐答案
所以看看array
并查看它被声明为的大小。
x维度中没有足够的元素,因此i溢出它,或者更少在y维度中超过7:array [x] [y]
。
我们不能为你做到这一点 - 所以使用调试器,看看发生了什么!
So look atarray
and see what sizes it is declared as.
Either there are not enough elements in the "x" dimension so "i" is overflowing it, or there are less than 7 in the "y" dimension:array[x][y]
.
We can't do that for you - so use the debugger and look at what is happening!
你可能想要改变这个部分
You may want to change this part
public static void main(String[] args) throws IOException {
int[][] studentinfo = {};
int students_num = 0;
System.out.println("[성적처리 프로그램]\n몇명의 학생들의 data를 입력하시겠습니까?");
Scanner s = new Scanner(System.in);
students_num = s.nextInt();
addinfo(students_num, studentinfo);
s.close();
}
As
As
public static void main(String[] args) throws IOException {
int students_num = 0;
System.out.println("[성적처리 프로그램]\n몇명의 학생들의 data를 입력하시겠습니까?");
Scanner s = new Scanner(System.in);
students_num = s.nextInt();
int[][] studentinfo = new int[students_num][7];
addinfo(students_num, studentinfo);
s.close();
}
这篇关于帮助我!!!!!!!:线程“主”中的例外情况java.lang.ArrayIndexOutOfBoundsException:0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文