import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Assingment3 {
public static void main(String[] args) {
System.out.println("how many course do your students have?");
Scanner cs = new Scanner(System.in);
int CN = cs.nextInt();
List<String> ID = new ArrayList<>();//对学生的ID创建一个动态数组
int SN = 1;//学生数量初始值
double[] tm;//输出每个学生总成绩
double[] am;//每个学生平均分
String[] mk;//每个学生等级
String ii;
double[][] MARK;//每个学生的每门课成绩
int v=0;
do {
double TM = 0;
double AM = 0;
double[][] mark = new double[SN][CN];
double[] Tm = new double[SN];
double[] Am = new double[SN];
String[] MK = new String[SN];
System.out.println("What's the ID of # " + SN + "student: ");
Scanner id = new Scanner(System.in);
String id1 = id.next();
ID.add(id1);
for (int b = 1; b <= CN; b++) {
System.out.println("What the mark of course " + b);
Scanner Mark1 = new Scanner(System.in);
int m1 = Mark1.nextInt();
mark[SN - 1][b - 1] = m1;
TM = TM + m1;
AM = TM / CN;
}
Tm[SN - 1] = TM;
Am[SN - 1] = AM;
if (AM <= 100 && AM >= 86)
v = 1;
else if (AM >= 75 && AM < 86)
v = 2;
else if (AM >= 60 && AM < 75)
v = 3;
else if (AM >= 56 && AM < 60)
v = 4;
else if (AM >= 0 && AM < 56)
v = 5;
else
System.out.println("Wrong input");
switch (v) {
case 1:
MK[SN - 1] = "A";
break;
case 2:
MK[SN - 1] = "B";
break;
case 3:
MK[SN - 1] = "C";
break;
case 4:
MK[SN - 1] = "D";
break;
case 5:
MK[SN - 1] = "F";
break;
default:
MK[SN - 1] = "Wrong input";
}
tm = Tm;
am = Am;
mk = MK;
MARK = mark;
System.out.println("Do you want to continue?(Type Y for yes, N for no)");
Scanner i = new Scanner(System.in);
ii = i.next().toLowerCase();
SN++;
} while (ii.equals("y"));
System.out.print("Student ID | Course: ");
for (int i = 1; i <= CN; i++)
System.out.print(i + " ");
System.out.println(" | Total | Average | Grade");
for (int c = 0; c < SN; c++) {
System.out.print(ID.get(c));
for (int j = 0; j < CN; j++) {
System.out.print(" "+ (MARK[c][j]));
}
System.out.println(" " + tm[c] + " " + am[c] + " " + mk[c]);
}
}
}
输出后提示错误:
请大佬们帮我看看哪里错了需要如何改正谢谢!!!
7 个解决方案
for (int c = 0; c < SN-1; c++)
SN比ID长度多1
数组越界了,就是数组的下标超过了数组的长度,仔细检查下边界就行了
for (int b = 0; b <= CN-1; b++)
你赋值的地方也要从0开始。所有的for循环都是0开始
楼主在每次输入之后,都将Mark=mark,导致每次都只记录了最新的数据内容,所以第一行的值都是空的,如果楼主输入3行,那么前两行都是空的,只有最后一行。楼主只是练习java中的各个语法么?如果是你已经达到了。