展开全部
处于严禁和各种32313133353236313431303231363533e58685e5aeb931333363383461考虑(习惯了),代码比较严谨,你那去交作业不会被骂的,就是内容多点;public static void main(String[] args) {
try {
boolean type = true;
Scanner scanner = null;
int num = 0;
double sum = 0;
while(type){
System.out.println("请输入你要计算的科目数:");
scanner = new Scanner(System.in);
try {
num = scanner.nextInt();
break;
} catch (InputMismatchException e) {
System.err.println("输入数据有误,请输入数字!!");
}
}
double[] results = new double[num];
type = true;
for (int i = 0; i
while(type){
System.out.println("请输入第" + (i+1) + "门成绩:");
scanner = new Scanner(System.in);
try {
results[i] = scanner.nextDouble();
break;
} catch (InputMismatchException e) {
System.err.println("输入数据有误,请输入数字!!");
}
}
}
System.out.println("您的各科成绩如下:");
for (int i = 0; i
double resu = results[i];
System.out.println((i+1) + ":" + new DecimalFormat("0.00").format(resu));
sum += resu;
}
System.out.println("您的总分为:" + new DecimalFormat("0.00").format(num) + "\r\n平均分为:" + new DecimalFormat("0.00").format(sum / num));
} catch (Exception e) {
e.printStackTrace();
}
}
所有地方地方做了保留两位小数的处理
对于输入成绩时非数字抛出异常做了重新获取用户输入的处理