Java学生Vector数组无法打印姓名问题求助及代码修复
Java数组存储学生信息后无法打印姓名问题修复
问题描述
我正在学习Java数组(题目中提及的Vector指此),练习时遇到问题:代码无法打印学生姓名字符串,不清楚原因。最终项目需求是打印平均分≥6的学生姓名,因姓名无法打印,我临时改为打印合格学生的平均分来验证功能,请帮忙修复?
现有代码
主类代码
package Program; import java.util.Scanner; import java.util.Locale; import Entities.Student; public class Program { public static void main(String[] args) { Locale.setDefault(Locale.US); Scanner sc = new Scanner(System.in); String name; double grade1; double grade2; double average; System.out.printf("how many students will you enter? "); int n = sc.nextInt(); Student[] vect = new Student[n]; // 存储学生对象的数组 for (int i = 0; i < vect.length; i++) { System.out.printf("imput the name, first and second grade for the student number %d%n ", i + 1); name = sc.nextLine(); // 读取姓名 sc.nextLine(); grade1 = sc.nextDouble(); grade2 = sc.nextDouble(); average = (grade1 + grade2) / 2; vect[i] = new Student(name, grade1, grade2, average); } for (int i = 0; i < vect.length; i++) { if (vect[i].getAverage() >= 6.0) { System.out.printf("%s%n", vect[i].getName()); System.out.printf("%.2f%n", vect[i].getAverage()); } } } }
Student类代码
package Entities; public class Student { private String name; private double grade1; private double grade2; private double average; public Student(String name, double grade1, double grade2, double average) { this.name = name; this.grade1 = grade1; this.grade2 = grade2; this.average = average; } public String getName() { return name; } public void setName(String name) { this.name = name; } public double getGrade1() { return grade1; } public void setGrade1(double grade1) { this.grade1 = grade1; } public double getGrade2() { return grade2; } public void setGrade2(double grade2) { this.grade2 = grade2; } public double getAverage() { return average; } public void setAverage(double average) { this.average = average; } }
当前控制台输出
how many students will you enter? 5 imput the name, first and second grade for the student number 1 John 10.0 9.0 imput the name, first and second grade for the student number 2 Mary 5.0 4.5 imput the name, first and second grade for the student number 3 Peter 8.5 9.5 imput the name, first and second grade for the student number 4 Carl 9.9 7.5 imput the name, first and second grade for the student number 5 Patricia 2.5 8.0 9.50 9.00 8.70
预期输出
John 9.5 Peter 9.0 Carl 8.70
问题分析与修复
核心问题
问题出在Scanner的输入缓冲区处理:
- 调用
sc.nextInt()读取学生数量后,输入缓冲区中残留了换行符,第一个sc.nextLine()会读取这个空换行符,导致name被赋值为空字符串。 - 后续多余的
sc.nextLine()会跳过用户实际输入的姓名,进一步导致所有学生姓名为空。
修复后的主类代码
package Program; import java.util.Scanner; import java.util.Locale; import Entities.Student; public class Program { public static void main(String[] args) { Locale.setDefault(Locale.US); Scanner sc = new Scanner(System.in); String name; double grade1; double grade2; double average; System.out.printf("how many students will you enter? "); int n = sc.nextInt(); sc.nextLine(); // 读取nextInt()遗留的换行符,避免干扰后续输入 Student[] vect = new Student[n]; for (int i = 0; i < vect.length; i++) { System.out.printf("input the name, first and second grade for the student number %d%n", i + 1); name = sc.nextLine(); // 正确读取姓名 grade1 = sc.nextDouble(); grade2 = sc.nextDouble(); sc.nextLine(); // 读取nextDouble()遗留的换行符,保证下一次循环的姓名读取正常 average = (grade1 + grade2) / 2; vect[i] = new Student(name, grade1, grade2, average); } // 输出合格学生信息 for (int i = 0; i < vect.length; i++) { if (vect[i].getAverage() >= 6.0) { System.out.printf("%s%n", vect[i].getName()); System.out.printf("%.2f%n%n", vect[i].getAverage()); } } sc.close(); // 关闭Scanner资源 } }
额外优化(可选)
Student类中的average字段可以通过计算动态生成,无需存储,避免数据不一致问题:
修改后的Student类代码:
package Entities; public class Student { private String name; private double grade1; private double grade2; public Student(String name, double grade1, double grade2) { this.name = name; this.grade1 = grade1; this.grade2 = grade2; } public String getName() { return name; } public void setName(String name) { this.name = name; } public double getGrade1() { return grade1; } public void setGrade1(double grade1) { this.grade1 = grade1; } public double getGrade2() { return grade2; } public void setGrade2(double grade2) { this.grade2 = grade2; } // 动态计算平均分 public double getAverage() { return (grade1 + grade2) / 2; } }
此时主类中创建Student对象的代码需改为:
vect[i] = new Student(name, grade1, grade2);
内容的提问来源于stack exchange,提问作者Laura
相关产品推荐
相关产品推荐

