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;
}
但是,这是我不理解的项目的一部分: 注意:从键盘输入的分数输入应该作为String读取,然后将该字符串传递给构造函数,并在构造函数中,使用split(String方法)或StringTokenizer将分数分开,将这些值分配给元素测验数组。对其他作业也这样做。
所有输入都将由控制类处理,我只是不明白如何设置字符串标记生成器构造函数或如何使它将字符串转换为double []。
如果我能得到一些帮助来设置其中一个变量来做,请说quizGrade,我确定我可以弄清楚如何设置其余的变量。
答案 0 :(得分: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)将每个解析为新数组。注意:如果它不是有效的double,则抛出NumberFormatException。