我正在研究向量,在练习一些练习时,我陷入了这个特定的练习,我的代码似乎没有打印名称字符串,我不知道为什么。在最终项目中,我必须仅打印平均数 >=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]; // vector that will store the students created
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(); // reads the name
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
@Getter
@RequiredArgsConstructor
public final class Student {
private final String name;
private final double grade1;
private final double grade2;
private final double avg;
}
public class Program {
public static void main(String... args) {
Scanner scan = new Scanner(System.in);
scan.useLocale(Locale.US);
System.out.print("How many students will you enter? ");
int total = scan.nextInt();
Student[] students = readStudents(total, scan);
for (int i = 0; i < students.length; i++) {
if (students[i].getAvg() >= 6.0) {
System.out.printf("%s%total", students[i].getName());
System.out.printf("%.2f%total", students[i].getAvg());
}
}
}
private static Student[] readStudents(int total, Scanner scan) {
Student[] students = new Student[total];
for (int i = 0; i < students.length; i++)
students[i] = readStudent(i + 1, scan);
return students;
}
private static Student readStudent(int num, Scanner scan) {
System.out.format("Student %d:\n", num);
System.out.print("Name: ");
String name = scan.next();
System.out.print("1st grade: ");
double grade1 = scan.nextDouble();
System.out.print("2nd grade: ");
double grade2 = scan.nextDouble();
double avg = (grade1 + grade2) / 2;
return new Student(name, grade1, grade2, avg);
}
}