提问人:Laura 提问时间:11/13/2023 最后编辑:Diego BorbaLaura 更新时间:11/14/2023 访问量:56
为什么我的矢量不打印字符串?[复制]
Why is my vector not printing the String? [duplicate]
问:
我正在研究向量,在练习一些练习时,我被困在这个特定的练习中,我的代码似乎没有打印名称字符串,我不知道为什么。在最终项目中,我只能打印平均数为 >=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
答:
0赞
Oleg Cherednik
11/13/2023
#1
- 如果总是一个词,没有空格,那么你可以使用 .
name
scan.next()
- 如果可以有空间,那么你应该使用(就像你所做的那样),然后在获取之前你应该添加(跳过返回值)。
name
scan.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);
}
}
下一个:字符串中的字符组 [已关闭]
评论
Vector