gpt4 book ai didi

java - 无法解决 ArithmeticException?/按零?

转载 作者:行者123 更新时间:2023-12-01 16:16:07 26 4
gpt4 key购买 nike

我在解决从 Student 类中的 getAverageScore() 方法中获取的 ArithmeticException 时遇到问题。我正在尝试编写一个程序来读取以下文本文件 scores.txt:

34     c081 c082 c083 c084
S2023 99 75 85 62
S2025 -1 92 67 52
S1909 100 83 45 -1

c数字(例如c081)是学校的类(class)代码,s数字(例如s2023)是学生编号。中间的数字代表他们的分数,-1也意味着他们没有被该单位录取。

我希望能够编写一个程序,在该程序中,我可以输出类(class)平均分最高的学生的输出,以及该平均分和学生人数。我希望能够确保 -1 值不包含在该类(class)平均分中,这样,如果学生有其中之一,则仅计算他们实际参加的类(class)的平均分。

这是Student 类(class):

import java.util.ArrayList;

public class Student {

private String studentNumber;
private ArrayList<Integer> scores = new ArrayList<Integer>();

public Student(String studentText) {
String[] parts = studentText.split(studentText, ' ');
this.studentNumber = parts[0];

for (int i = 1; i < parts.length - 1; i++) {
scores.add(Integer.parseInt(parts[i + 1]));
}
}

public String getStudentNumber() {
return this.studentNumber;
}

public float getAverageScore() {
int sum = 0;
int numberOfCoursesNotTaken = 0;

for (int i = 0; i <this.scores.size(); i++) {
if (i != -1) {
sum += this.scores.get(i);
System.out.println(sum);
} else {
numberOfCoursesNotTaken++ ;
System.out.println(numberOfCoursesNotTaken);
}
}
return sum / (this.scores.size() - numberOfCoursesNotTaken);
}


}


我从以下类中调用此方法:

import java.io.File; 
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;

public class MySchool {
public static void main(String[] args) {
int numberOfStudents;
ArrayList<Student> allStudents = new ArrayList<Student>() ;

try {

File scoresFile = new File("Scores.txt");
Scanner scoresFileReader = new Scanner(scoresFile);


String headerRow = scoresFileReader.nextLine();

char c = headerRow.charAt(0);
numberOfStudents = Character.getNumericValue(c);


for (int studentI = 0; studentI < numberOfStudents; studentI++) {
String studentText = scoresFileReader.nextLine();
System.out.println(studentText);
Student student = new Student(studentText);
allStudents.add(student);
}

scoresFileReader.close();

} catch (FileNotFoundException e) {
System.out.println("An error occurred");
e.printStackTrace();
}

float highestAverage = 0;
String highestScoringStudentNumber = null;


for (Student student : allStudents) {
if (student.getAverageScore() > highestAverage) {
highestAverage = student.getAverageScore();
highestScoringStudentNumber = student.getStudentNumber();
}

}
System.out.println("Highest scoring student: " + highestScoringStudentNumber);
}
}

但是,StudentgetAverageScore() 的返回行给出以下错误: Exception in thread "main"java.lang.ArithmeticException:/by零
在 Student.getAverageScore(Student.java:34)
在 MySchool.main(MySchool.java:42)

我以为我已经使用 numberOfCoursesNotTaken 变量解决了这个问题?有人可以帮助我吗?

最佳答案

我强烈建议您在发现无效值后立即将其删除。您可以将 Student 构造函数替换为以下内容:

public Student(String studentText) {
String[] parts = studentText.split(" "); // previously (studentText, ' ')
this.studentNumber = parts[0];

for (int i = 1; i < parts.length - 1; i++) {
int score = Integer.parseInt(parts[i]); // NOT i+1
if (score >=0) this.scores.add(score);
}
}

当您这样做时,您可以大大简化平均分计算(现在没有 -1):

public float averageScore() {
if (scores.isEmpty()) {
return 0; // avoids divide-by-zero
} else {
int total = 0;
for (int score : scores) total += score;
return total / (float) scores.size();
}
}

关于java - 无法解决 ArithmeticException?/按零?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62386849/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com