Java 如何获得列表中所有数字的平均值
我需要从用户提交的数字中获得平均测试分数,并保持无穷大。我需要知道考试的平均分数。 这是学生班:Java 如何获得列表中所有数字的平均值,java,arraylist,while-loop,runtime-error,user-input,Java,Arraylist,While Loop,Runtime Error,User Input,我需要从用户提交的数字中获得平均测试分数,并保持无穷大。我需要知道考试的平均分数。 这是学生班: public class Student { private String name; private int numOfQuizzes; private double totalScore; private ArrayList<Integer> scores; public Student(String name){ this.
public class Student {
private String name;
private int numOfQuizzes;
private double totalScore;
private ArrayList<Integer> scores;
public Student(String name){
this.name = name;
scores = new ArrayList<Integer>();
}
public String getName() {
return name;
} public void addQuiz(int score){
scores.add(score);
}
public double getTotalScore() {
for(int score : scores){
totalScore += score;
}
return totalScore;
}
public double getAverageScore(){
return totalScore/(double)numOfQuizzes;
}
}
正如你所看到的,即使将分数除以大小,我也会得到无穷大。我想你已经把自己搞糊涂了,因为你只持有两个列表 删除主方法中的分数列表。同时从学生班级中删除
totalScore
;这就是所谓的“计算字段”
更新方法
public double getTotalScore() {
double totalScore = 0;
for(int score : scores){
totalScore += score;
}
return totalScore;
}
然后,您实际上需要这样一个循环来更准确地处理输入验证的顺序
String grade;
do {
grade = inp.nextLine();
if (grade.equals("q")) break;
// Convert the grade to an integer and pass it
student.addQuiz(Integer.parseInt(grade));
} while (true);
然后,从学生课堂上删除numOfQuizzes
在平均法中,这就是你使用的方法
return getTotalScore()/scores.size();
你现在得到无穷大两次,因为
this.numOfQuizzes
您需要对代码进行一些更改,如下所示: 学生班:
public class Student {
private String name;
private double totalScore;
private ArrayList<Integer> scores;
public Student(String name){
this.name = name;
scores = new ArrayList<Integer>();
}
public String getName() {
return name;
} public void addQuiz(int score){
scores.add(score);
}
public double getTotalScore() {
totalScore = 0;
for(int score : scores){
totalScore += score;
}
return totalScore;
}
public double getAverageScore(){
return getTotalScore()/scores.size();
}
}
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
// Request the name
System.out.print("What is your name?: ");
String name = input.nextLine();
// Create the student object
Student student = new Student(name);
// Ask for the grades
System.out.print("Please enter your scores (q to quit): ");
String grade = input.nextLine();
while (!grade.equals("q")) {
// Convert the grade to an integer and pass it
student.addQuiz(Integer.parseInt(grade));
// Request a new grade
grade = input.nextLine();
}
// Report the results
System.out.println("Students Name: " + student.getName());
System.out.println("Total Quiz Scores: " + student.getTotalScore());
System.out.println("Average Quiz Score: " + student.getAverageScore());
}
}
尝试使用此代码,它将正常工作并显示平均分数,而不是无限
已完成的更改:
public class Student {
private String name;
private double totalScore;
private ArrayList<Integer> scores;
public Student(String name){
this.name = name;
scores = new ArrayList<Integer>();
}
public String getName() {
return name;
} public void addQuiz(int score){
scores.add(score);
}
public double getTotalScore() {
totalScore = 0;
for(int score : scores){
totalScore += score;
}
return totalScore;
}
public double getAverageScore(){
return getTotalScore()/scores.size();
}
}
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
// Request the name
System.out.print("What is your name?: ");
String name = input.nextLine();
// Create the student object
Student student = new Student(name);
// Ask for the grades
System.out.print("Please enter your scores (q to quit): ");
String grade = input.nextLine();
while (!grade.equals("q")) {
// Convert the grade to an integer and pass it
student.addQuiz(Integer.parseInt(grade));
// Request a new grade
grade = input.nextLine();
}
// Report the results
System.out.println("Students Name: " + student.getName());
System.out.println("Total Quiz Scores: " + student.getTotalScore());
System.out.println("Average Quiz Score: " + student.getAverageScore());
}
}
私人智力测验代码>来自学生班
totalScore=0学生类中的getTotalScore()
方法中的code>
return getTotalScore()/scores.size()getAverageScore()
方法中的code>inStudent class。此处getTotalScore()
返回总分和scores.size()
将返回总分。(以你的例子来说是3)
ArrayList分数=新建ArrayList()代码>来自主类。因为它在主类中不需要
double average=student.getTotalScore()/scores.size()因为下一行中的student.getAverageScore()
将计算并返回平均值
什么“运行时错误”?这
虽然(!grade.equals('q')){
永远不应该是真的,但它只需要在java.lang.NumberFormatException.forInputString(未知源)中输入“q”?或在java.lang.Integer.parseInt(未知源)中输入字符串:在finalReview.finalReview.main中输入“q”(FinalReview.java:73)numfquizes是并且仍然是0,所以当calc average.where添加do{grade=inp.nextLine()时,总是除以0;?代替当前的while循环。它被称为dowhile。我已经从我的类文件中删除了numofquizzes,但我仍然使用它获得无穷大。我建议您在添加数字时打印出来,并使用调试器遍历代码。我不确定您根据我所说的更改了什么。我目前没有运行此代码来检查它我自己