GradeBook Java 程序。帮忙打电话上课?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26895921/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
GradeBook Java Program. Help calling class?
提问by Kenny Bowen
The assignment goes as follows.
分配如下。
A teacher has five students who have taken four tests. The teacher uses the following grade scale to assign a letter grade to a student, based on the average of his or her four test scores.
一位老师有五个学生,他们参加了四次考试。教师使用以下等级量表根据学生四次考试成绩的平均值为学生分配字母等级。
90-100: A
80-79: B
70-79: C
60-69: D
0-59: F
Write a class that uses a String array on an arraylist object to hold the five students' names, an array of five characters to hold the five students' letter grades, and five arrays of four doubles each to hold each student's set of test scores. The class should have methods that return a student's name, the average test score, and a letter grade based on the average.
编写一个类,在 arraylist 对象上使用 String 数组来保存五个学生的姓名,一个包含五个字符的数组来保存五个学生的字母成绩,以及五个包含四个双打的数组来保存每个学生的测试分数集。该类应该具有返回学生姓名、平均考试成绩和基于平均成绩的字母等级的方法。
Demonstrate the class in a program that allows the user to enter each student's name and his or her four test scores. It should then display each student's average test score and letter grade.
在允许用户输入每个学生的姓名和他或她的四项考试成绩的程序中演示课程。然后它应该显示每个学生的平均考试成绩和字母等级。
Program should not accept a score lower than 0 or higher than 100
程序不应接受低于 0 或高于 100 的分数
The following code is here:
以下代码在这里:
Student class:
学生班:
public class Student{
private String name;
private double[] test = new double[4];
public Student(){
name = " ";
}
public Student(String n, double[] t){
name = n;
test = t;
}
public void setName(String n)
{
name = n;
}
public String getName(){
return name;
}
public void setTest(double t,int i)
{
test[i] = t;
}
public double getTest(int i)
{
return test[i];
}
public double getTestAvg(){
double sum = 0;
double avg;
for(int i = 0; i < test.length; i++)
{
sum += test[i];
}
avg = sum / test.length;
return avg;
}
public char getLetterGrade(){
double average = getTestAvg();
char grade=0;
if(average >= 90)
grade = 'A';
else if (average >= 80)
grade = 'B';
else if (average >= 70)
grade = 'C';
else if (average >=60)
grade = 'D';
else if (average < 60)
grade = 'F';
return grade;
}
public String toString(){
String str = "";
str += "\nName of student: " + name;
str += "\nAverage test score: " + getTestAvg();
str += "\nLetter grade: " + getLetterGrade();
return str;
}
}
The Main Program:
主要程序:
import java.util.Scanner;
import java.io.*;
public class GradeBook {
public static void main(String[] args) throws IOException {
Student[] students = new Student[5];
getStudentData(students);
}
public static double getStudentData(Student[] array) {
Scanner scan = new Scanner(System.in);
String[] student = new String[5];
double[] test = new double[4];
for (int i = 0; i < student.length; i++) {
System.out.println("Enter the name of the student : ");
student[i] = scan.nextLine();
for (int j = 0; j < test.length; j++) {
System.out.println("Enter score " + (j + 1) + " for the student");
test[j] = scan.nextDouble();
scan.nextLine();
}
array[i] = new Student(student[i], test);
}
return 0;
}
}
The problem is, is that I can enter all the information, but nothing will display after all the numbers have been entered in. Any help would be greatly appreciated!
问题是,我可以输入所有信息,但输入所有数字后什么也不会显示。任何帮助将不胜感激!
回答by Ben Minton
You're not seeing anything displayed because you never make the call to display anything. You are populating the "array", so you need to iterate over it now.
您没有看到任何显示的内容,因为您从未调用过显示任何内容。您正在填充“数组”,因此您现在需要对其进行迭代。
After the outer for loop (the first one), you'll want to iterate over the populated array and print out the student objects. This will work since you already have a toString() method.
在外部 for 循环(第一个)之后,您需要遍历填充的数组并打印出学生对象。这将起作用,因为您已经有一个 toString() 方法。
ex:
前任:
for(Student student : array)
{
System.out.println(student);
}