0
This is what I have so far:



     //class to calculate grades

    import java.util.StringTokenizer;
    public class Grades {

//variables
private String studentName;
private int studentId;
private double[] quizGrade;
private double[] homeworkGrade;
private double[] labGrade;
private double[] testGrade;
private double projectGrade;

//no args constructor
public Grades(){
    studentName = "";
    studentId = 0;
    quizGrade[16] = 0.0;
    homeworkGrade[10] = 0.0;
    labGrade[7] = 0.0;
    testGrade[3] = 0.0;
    projectGrade = 0.0;     
}

//parameter constructor
public Grades(String name, int id, double[] quiz, double[] homework, 
        double[] lab, double[] test, double project){
    studentName = name;
    studentId = id;
    quizGrade = quiz;
    homeworkGrade = homework;
    labGrade = lab;
    testGrade = test;
    projectGrade = project;
}

//mutator methods
public void setName(String name){
    studentName = name;
}

public void setId(int id){
    studentId = id;
}

public void setQuiz(double[] quiz){
    for(int i = 0; i < quizGrade.length; i++){
        quizGrade[i] = quiz[i];
    }
}

public void setHomework(double[] homework){
    for(int i = 0; i < homeworkGrade.length; i++){
        homeworkGrade[i] = homework[i];
    }
}

public void setLab(double[] lab){
    for(int i = 0; i < labGrade.length; i++){
        labGrade[i] = lab[i];
    }
}

public void setTest(double[] test){
    for(int i = 0; i < testGrade.length; i++){
        testGrade[i] = test[i];
    }
}

public void setProject(double project){
    projectGrade = project;
}

//accessor methods
public String getStudentName(){
    return studentName;
}

public int getStudentId(){
    return studentId;
}

public double getQuiz(){
    return quizGrade[15];
}

public double getHomework(){
    return homeworkGrade[10];
}

public double getLab(){
    return labGrade[7];
}

public double getTest(){
    return testGrade[3];
}

public double getProject(){
    return projectGrade;
}

但是,这是我不理解的项目的一部分:注意:从键盘输入的分数输入应作为字符串读取,然后将该字符串传递给构造函数,并在构造函数中使用 split (字符串的方法)或StringTokenizer 将分数分割出来,将这些值放置到测验数组的元素中。对其他作业做同样的事情。

所有输入都将由控制类处理,我只是不明白如何设置字符串标记器构造函数或如何让它将字符串转换为双 []。

如果我能得到一些帮助来设置其中一个变量来做这件事,比如 quizGrade,我相信我可以弄清楚如何设置其余的变量。

4

1 回答 1

0

我认为这会有所帮助。如果没有,请发表评论,我们可以从那里开始。

String scores = "96.54 45.4 100";
String[] split = scores.split(" ");
double[] dblScores = new double[split.length];
for(int i = 0; i < split.length; i++){
    dblScores[i] = Double.valueOf(split[i]);
}

用户输入(分数)根据空格拆分为字符串数组。初始化一个双精度数组,因为现在我们知道有多少。然后使用 Double.valueOf(String) 将每个解析为新数组。注意:如果它不是一个有效的双精度数,这将抛出一个 NumberFormatException。

于 2013-04-27T01:50:36.983 回答