无法解决 ArithmeticException? / 零?
Cannot work around ArithmeticException? / by zero?
我无法解决从 Student
class 中的 getAverageScore()
方法获取的 ArithmeticException。我正在尝试编写一个程序来读取以下文本文件 scores.txt
:
34 c081 c082 c083 c084
S2023 99 75 85 62
S2025 -1 92 67 52
S1909 100 83 45 -1
c081等c号是学校的课程代码,s2023等s号是学号。中间的数字代表他们的分数,-1也代表他们没有被录取。
我希望能够编写一个程序,在该程序中,我可以输出 class 平均分最高的学生,以及该平均分和他们的学号。我希望能够确保 -1 值不包含在 class 平均值中,这样如果学生有其中之一,他们的平均值只针对他们所做的 classes 计算实际服用。
这是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);
}
}
我从以下 class:
调用此方法
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);
}
}
然而,Student
中getAverageScore()
的return行给出了以下错误:Exception in thread "main" java.lang.ArithmeticException: / by zero
at Student.getAverageScore(Student.java:34)
at MySchool.main(MySchool.java:42)
我以为我已经使用 numberOfCoursesNotTaken
变量解决了这个问题?谁能帮帮我?
Student class -> getAverageScore :在 for 循环中,您检查的是 if i != -1,而不是 if scores.get(i) != -1.
我真的建议一旦发现无效值就将其删除。您可以将 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);
}
}
当你这样做的时候,你可以大大简化你的平均分计算(现在没有 -1s):
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();
}
}
我无法解决从 Student
class 中的 getAverageScore()
方法获取的 ArithmeticException。我正在尝试编写一个程序来读取以下文本文件 scores.txt
:
34 c081 c082 c083 c084
S2023 99 75 85 62
S2025 -1 92 67 52
S1909 100 83 45 -1
c081等c号是学校的课程代码,s2023等s号是学号。中间的数字代表他们的分数,-1也代表他们没有被录取。
我希望能够编写一个程序,在该程序中,我可以输出 class 平均分最高的学生,以及该平均分和他们的学号。我希望能够确保 -1 值不包含在 class 平均值中,这样如果学生有其中之一,他们的平均值只针对他们所做的 classes 计算实际服用。
这是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);
}
}
我从以下 class:
调用此方法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);
}
}
然而,Student
中getAverageScore()
的return行给出了以下错误:Exception in thread "main" java.lang.ArithmeticException: / by zero
at Student.getAverageScore(Student.java:34)
at MySchool.main(MySchool.java:42)
我以为我已经使用 numberOfCoursesNotTaken
变量解决了这个问题?谁能帮帮我?
Student class -> getAverageScore :在 for 循环中,您检查的是 if i != -1,而不是 if scores.get(i) != -1.
我真的建议一旦发现无效值就将其删除。您可以将 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);
}
}
当你这样做的时候,你可以大大简化你的平均分计算(现在没有 -1s):
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();
}
}