-3
這是我的錯誤信息異常在線程 「主」 java.lang.IllegalArgumentException異常
Exception in thread "main" java.lang.IllegalArgumentException
at TestGrades.setRaceTime(TestGrade.java:49)
at GradeDriver.getGradeData(GradeDriver.java:40)
at Grade Driver.main(GradeDriver.java:26)
Java的類:
import java.util.*;
import java.io.*;
public class GradeDriver{
public static void main(String[] args){
double[] averageInfo = new double[3];
Scanner console = new Scanner(System.in);
String[] fileName = new String[2];
for(int i = 0; i < args.length; i++){
fileName[i] = args[i];
}
Scanner input = getInputFile(console, fileName);
PrintStream output = getOutputFile(console, fileName);
TestGrades[] grades = getGradeData(input);
showGradeInfo(output, grades);
getAverageInfo(averageInfo, grades);
showAverageInfo(output, averageInfo, grades[0]);
}
public static TestGrades[] getGradeData(Scanner input) {
int numberOfGrades = input.nextInt();
TestGrades[] grades = new TestGrades[numberOfGrades];
for(int driver = 0; driver < numberOfGrades && input.hasNext(); driver++) {
grades[driver] = new TestGrades(input.next(), input.next());
for (int grade = 1; grade <= 4; grade++) {
grades[driver].setRaceTime(grade , input.nextDouble());
}
}
return grades;
}
public static void showGradeInfo (PrintStream output, TestGrades[] grades) {
for (int i = 0; i < grades.length; i++) {
output.print((i + 1) + ". " + grades[i] + "\n");
}
}
public static void getAverageInfo (double[] info, TestGrades[] grades) {
double low = 20.0;
double high = 0.0;
double average = 0.0;
double totalAverage = 0.0;
for (int i = 0; i < grades.length; i++) {
average = grades[i].getGradeAverage();
if (average > high) {
high = average;
}
if (average < low) {
low = average;
}
totalAverage = totalAverage + average;
}
info[0] = low;
info[1] = high;
info[2] = totalAverage/grades[0].getCounter();
}
public static void showAverageInfo(PrintStream o, double[] info, TestGrades d) {
String[] s = {"Total number of students: "};
o.printf("\n%d. %s %d\n", 1, s[0], d.getCounter());
for (int i = 1; i <info.length; i++) {
o.printf("%d. %s %.2f/n", i + i, s[i], info[i - 1]);
}
}
public static Scanner getInputFile(Scanner console, String[] inFileName) {
boolean fileFound = false;
Scanner s = null;
if (inFileName[0] == null) {
System.out.print("Please enter the input file name: ");
inFileName[0] = console.nextLine();
}
do {
try {
s = new Scanner(new File(inFileName[0]));
fileFound = true;
}
catch (FileNotFoundException e) {
System.out.println("Cannot read file: " + e) ;
System.out.println("Enter the correct input file name: ");
inFileName[0] = console.nextLine();
}
}
while (!fileFound);
return s;
}
public static PrintStream getOutputFile (Scanner console, String[] outFileName) {
boolean fileFound = false;
PrintStream pS = null;
if (outFileName[1] == null) {
System.out.print("Please enter output file name: ");
outFileName[1] = console.nextLine();
}
do {
try {
pS = new PrintStream (new File (outFileName[1]));
fileFound = true;
}
catch (FileNotFoundException e2) {
System.out.println("Cannot read file: " + e2);
System.out.println("Enter a correct output file name: ");
outFileName[1] = console.nextLine();
}
}
while(!fileFound);
return pS;
}
}
import java.io.*;
import java.util.*;
public class TestGrades {
private static int gradeCount = 0;
private String firstName;
private String lastName;
private double[] raceTime = new double[4];
public TestGrades(String fName, String lName) {
firstName = fName;
lastName = lName;
gradeCount++;
}
private String getFirstName() {
return firstName;
}
private String getLastName() {
return lastName;
}
public double getGradeAverage() {
double highest = 0.0;
double sum = 0.0;
for (int i = 0; i < raceTime.length; i++) {
sum += raceTime[i];
if (raceTime[i] > highest) {
highest = raceTime[i];
}
}
sum = sum - highest;
sum = sum/3;
return sum;
}
public void setRaceTime(int grade, double time) {
if (grade < 1 || grade > 4 || time < 0.0 || time > 15.0) {
throw new IllegalArgumentException();
}
raceTime[grade - 1 ] = time;
}
public int getCounter() {
return gradeCount;
}
public String toString() {
return (firstName + " " + lastName);
}
}
任何幫助將真正理解!
對我們來說,這並不是真的很容易......你在這裏粘貼了什麼文件(名稱)?堆棧跟蹤中的這些行是什麼? (我不會算)。你有什麼嘗試?你可以刪除不相關的代碼嗎? –
@ZachL它是Java,所以文件名必須是「GradeDriver.java」和「TestGrades.java」,以便發佈的兩個類。在觀察堆棧跟蹤時也非常明顯。但我同意我在這裏也錯過了OP的一些努力。 –
年,意識到發佈評論後不久;足夠我不是一個本地的Java揚聲器;-) –