public class StudentDemo { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 判断类型并循环输入 String stuNo, name, str; double english, math, Java, testScore = 1.0; double sum2 = 0, sum3 = 0; Student s1 = null; Student s2 = null; Student s3 = null; while (in.hasNext()) { String line = in.nextLine(); String[] fields = line.split(" "); //字符串 line 按照空格分隔符进行拆分,拆分后的结果存储在一个名为 fields 的字符串数组中 stuNo = fields[0]; //取 fields 数组的第一个元素,即该语句将 fields 数组的第一个元素的值赋给了变量 stuNo name = fields[1]; english = Double.parseDouble(fields[2]); math = Double.parseDouble(fields[3]); Java = Double.parseDouble(fields[4]); if (fields.length == 5) { // 学生PT s1 = new StudentPT(stuNo, name, english, math, Java, testScore); } else if (fields[5].equals("SystemFramework")) { // 学生SJ s2 = new StudentSJ(stuNo, name, english, math, Java, testScore); sum2 = s2.sum(); } else if (fields[5].equals("GroupLeader")) { // 学生ZZ s3 = new StudentZZ(stuNo, name, english, math, Java, testScore); sum3 = s3.sum(); } else { break; } } //按顺序输出 System.out.println(s1.toString()); System.out.println(s2.toString() + " task:SystemFramework"); System.out.println(s3.toString() + " duty:GroupLeader"); //比较大小 double max = Math.max(sum2,sum3); if(max == sum2){ System.out.println("Lisi's total score is more than Wangwu"); }else if(max == sum3){ System.out.println("Lisi's total score is less than Wangwu"); } } } 在输出时需要再次输入回车才可输出第三组数据,但这不符合要求格式,且后续无法终止输出进程
时间: 2024-01-05 16:02:45 浏览: 86
这是因为你的程序在输入完最后一个学生信息后,需要再次输入回车才能停止循环并输出结果。你可以在输入完最后一个学生信息后,加上一行代码 `break;` 来结束循环,然后直接输出结果即可。修改后的代码如下:
```
public class StudentDemo {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String stuNo, name, str;
double english, math, Java, testScore = 1.0;
double sum2 = 0, sum3 = 0;
Student s1 = null;
Student s2 = null;
Student s3 = null;
while (in.hasNext()) {
String line = in.nextLine();
String[] fields = line.split(" ");
stuNo = fields[0];
name = fields[1];
english = Double.parseDouble(fields[2]);
math = Double.parseDouble(fields[3]);
Java = Double.parseDouble(fields[4]);
if (fields.length == 5) {
s1 = new StudentPT(stuNo, name, english, math, Java, testScore);
} else if (fields[5].equals("SystemFramework")) {
s2 = new StudentSJ(stuNo, name, english, math, Java, testScore);
sum2 = s2.sum();
} else if (fields[5].equals("GroupLeader")) {
s3 = new StudentZZ(stuNo, name, english, math, Java, testScore);
sum3 = s3.sum();
} else {
break; // 结束循环
}
}
System.out.println(s1.toString());
System.out.println(s2.toString() + " task:SystemFramework");
System.out.println(s3.toString() + " duty:GroupLeader");
double max = Math.max(sum2,sum3);
if(max == sum2){
System.out.println("Lisi's total score is more than Wangwu");
}else if(max == sum3){
System.out.println("Lisi's total score is less than Wangwu");
}
}
}
```
这样修改后,你可以直接输入所有学生信息,程序会自动结束循环并输出结果。
阅读全文