该计划将计算学生的最终成绩。每个学生都持有他/她的中期成绩,第二季成绩和期末考试的记录。最终成绩将是中期成绩的25%,第二季成绩的30%和期末考试的45%。该计划将询问10名学生的数据。每个学生的数据包括姓名,部分,期中成绩,第二季度和期末考试。
我如何以表格形式显示学生的成绩?
这是gradesummary.java的代码
public class gradesummary {
public static void main(String args[]){
Scanner cin = new Scanner(System.in);
Vector<Student> list = new Vector<>();
for(int x=0; x<10 ;x++){
Student s = new Student();
System.out.print("Name: ");
s.name = cin.nextLine();
System.out.print("Section: ");
s.section = cin.nextLine();
System.out.print("Midterm Grade: ");
s.midgrade = cin.nextDouble();
System.out.print("Second Quarter: ");
s.secondqtr = cin.nextDouble();
System.out.print("Final Exam: ");
s.finexam = cin.nextDouble();
//save s in data collection
list.add(s);
}
for(int x = 0; x<10; x++){
//what will i do here next?
Student xtt = list.get(x);
}
}
}
这是Student.java的代码
public class Student {
//is this class correct?
String name;
String section;
Double midgrade;
Double secondqtr;
Double finexam;
Double fingrade;
public Student(){
}
public Student (String n, String s, Double mg, Double sq, Double fe, Double fg){
name = n;
section = s;
midgrade = mg;
secondqtr = sq;
finexam = fe;
fingrade = fg;
}
}
我想要这样的输出
Name Midterm Grade Second Quarter Final Exam Final Grade
Student2 80 79.69 85 87.674
Student2 78 77.69 81 79.257
Student3 88 87 89 88.15
Student4 88.67 84 86.78 86.4185
Student5 98 90 94 93.8
Student6 93 91 90 91.05
Student7 84 78 87 83.55
Student8 82 86.4 89 86.47
Student9 72 79 81 78.15
Student10 85.34 81 83 82.985
答案 0 :(得分:0)
我建议您将数据直接读入学生的字段。为此,您只能将Scanner对象传递给Student构造函数。
你只需要:
public Student (Scanner sc){
name = sc.next();
section = sc.next()
.
.
.
}
确保在阅读学生记录后关闭扫描仪。
答案 1 :(得分:0)
你应该这样写:
for(Student student: list){
System.out.printf("%-8s %-8s %-8s %-8s%n", "Name", "Midterm Grade", "Second Quarter", "Final Exam");
System.out.printf("%1d -8s %7.1f %7.1f %7.1f%n", student.get...);
}
最终成绩数学是:
float finalGrade =(student.get(FinalExam)* 45/100)+ (student.get(MidtermGrade)* 20/100 ......)
答案 2 :(得分:0)
您会注意到一些变化,但我对此的看法是:
public class GradeSummary {
public static void main(String args[]) {
Scanner cin = new Scanner(System.in);
List<Student> list = new ArrayList<>();
for (int x = 0; x < 10; x++) {
Student s = new Student();
System.out.print("Name: ");
s.setName(cin.nextLine());
System.out.print("Section: ");
s.setSection(cin.nextLine());
System.out.print("Midterm Grade: ");
s.setMidterm(cin.nextDouble());
System.out.print("Second Quarter: ");
s.setSecondQuarter(cin.nextDouble());
System.out.print("Final Exam: ");
s.setFinalExam(cin.nextDouble());
list.add(s);
}
for (Student student : list) {
System.out.println(String.format("Name: %s, Final Grade: %s", student.getName(), student.getWeightedGrade()));
}
}
}
和
public class Student {
private String name;
private String section;
private Double midterm;
private Double secondQuarter;
private Double finalExam;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSection() {
return section;
}
public void setSection(String section) {
this.section = section;
}
public Double getMidterm() {
return midterm;
}
public void setMidterm(Double midterm) {
this.midterm = midterm;
}
public Double getSecondQuarter() {
return secondQuarter;
}
public void setSecondQuarter(Double secondQuarter) {
this.secondQuarter = secondQuarter;
}
public Double getFinalExam() {
return finalExam;
}
public void setFinalExam(Double finalExam) {
this.finalExam = finalExam;
}
public Double getWeightedGrade()
{
return this.midterm * .25 + this.secondQuarter * .30 + this.finalExam * .45;
}
}