为什么我的矢量不打印字符串?[复制]

Why is my vector not printing the String? [duplicate]

提问人:Laura 提问时间:11/13/2023 最后编辑:Diego BorbaLaura 更新时间:11/14/2023 访问量:56

问:

这个问题在这里已经有答案了:
7天前关闭。

社群在 7 天前审查了是否重新打开这个问题,并关闭了这个问题:

原始关闭原因未解决

我正在研究向量,在练习一些练习时,我被困在这个特定的练习中,我的代码似乎没有打印名称字符串,我不知道为什么。在最终项目中,我只能打印平均数为 >=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
java 字符串 向量 printf

评论

0赞 0x150 11/13/2023
臭名昭著的 stackoverflow.com/questions/13102045/ 问题
0赞 Basil Bourque 11/13/2023
它在 Java 中被称为“数组”,而不是“向量”。 是一个过时的集合类。Vector

答:

0赞 Oleg Cherednik 11/13/2023 #1
  • 如果总是一个词,没有空格,那么你可以使用 .namescan.next()
  • 如果可以有空间,那么你应该使用(就像你所做的那样),然后在获取之前你应该添加(跳过返回值)。namescan.nextLine()scan.nextInt()scan.nextLine()
@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) {
        final Locale locale = Locale.US;
        Scanner scan = new Scanner(System.in);
        scan.useLocale(locale);

        System.out.print("How many students will you enter? ");
        int total = scan.nextInt();
        Student[] students = readStudents(total, scan);

        System.out.print("Enter min avg grade: ");
        double minAvg = scan.nextDouble();

        for (Student student : students) {
            if (student.getAvg() >= minAvg)
                System.out.format(locale, "%s %.1f\n", student.getName(), student.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);
    }

}