Java项目:字符串标记器有问题


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 (String的方法)或StringTokenizer将分数拆分,并将这些值放入quiz数组的元素中。

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

如果我能得到一些帮助来设置其中一个变量,比如quizGrade,我肯定我能弄清楚如何设置其余的。

我想这会有帮助。如果没有,请留下评论,我们可以从那里开始。

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。

相关内容

  • 没有找到相关文章

最新更新