我已经完成了代码并反复尝试新事物,似乎无法摆脱这个错误。我正在写一个程序,我扫描一个文件作为输入,它通过并找到平均值,最大值和最小值。但是我已经在代码的末尾遇到了问题,我必须给每个分数评分,然后计算每个分数的数量。它编译得很好,但是当我去运行它时,java.langNoClassDefFoundError:不断出现请尽可能帮助。
import java.io.*;
import java.util.*;
class Exam
{
public static void main(String [] args) throws IOException
{
int A;
int B;
int C;
int D;
int F;
// Greets user and prompts to enter name of the file containing there grades.
System.out.println("***Welcome to the Exam Statistics Program!!***");
Scanner cin = new Scanner(System.in);
System.out.println("Please enter a filename:");
String filename = cin.next();
// Scanner built for new file and puts grades into an array.
Scanner file = new Scanner(new FileReader(filename));
int g;
g=file.nextInt();
int [] grades = new int[g];
for(int i=0; i<grades.length; i++)
{
grades[i] = file.nextInt();
}
System.out.println("Minimum Score: " + getMinValue(grades));
System.out.println("Maximum Score: " + getMaxValue(grades));
System.out.println("Average Score: " + Average(grades));
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println("A: " +A);
System.out.println("B: " +B);
System.out.println("C: " +C);
System.out.println("D: " +D);
System.out.println("F: " +F);
System.out.println("The grade count is: "+ g);
}
// Calculates the average of the array and returns the variable containing that values.
static double Average( int [] grades)
{
double sum=0;
double x;
for(int i=0; i<grades.length; i++)
sum += (double)grades[i];
if(grades.length==0) return 0;
x = sum/grades.length;
return x;
}
// Calculates the lowest grade in the file.
public static int getMinValue(int[] grades)
{
int minValue = grades[0];
for(int i=1;i<grades.length;i++)
{
if(grades[i] < minValue)
{
minValue = grades[i];
}
}return minValue;
}
// Calculates the highest grade in the file.
public static int getMaxValue(int[] grades)
{
int maxValue = grades[0];
for(int i=1;i < grades.length;i++)
{
if(grades[i] > maxValue)
{
maxValue = grades[i];
}
} return maxValue;
}
// Adds up the total number of letter grades you have in the file.
public static int countA(int[] grades) //throws IOException
{
int A=0;
for(int i=1; i<grades.length;i++)
{
if(grades[i]>89||grades[i]<=100)
{
A = A++;
}
}
return A;
}
public static int countB(int[] grades)
{
int B=0;
for(int i=1; i<grades.length;i++)
{
if(grades[i]>79||grades[i]<=89)
{
B = B++;
}
}
return B;
}
public static int countC(int[] grades)
{
int C = 0;
for(int i=1; i<grades.length;i++)
{
if(grades[i]>69||grades[i]<=79)
{
C = C++;
}
}
return C;
}
public static int countD(int[] grades)
{
int D = 0;
for(int i=1; i<grades.length;i++)
{
if(grades[i]>59||grades[i]<=69)
{
D = D++;
}
}
return D;
}
public static int countF(int[] grades)
{
int F = 0;
for(int i=1; i<grades.length;i++)
{
if(grades[i]<=59)
{
F = F++;
}
}
return F;
}
}
答案 0 :(得分:3)
您的堆栈跟踪显示错误Exception in thread "main" java.lang.NoClassDefFoundError: Exam2
。
但是你的班级名为Exam
。因此,请将您的文件名从Exam.java
Exam2.java
答案 1 :(得分:1)
在这里,您创建了一个名为Exam2.java的Java文件。根据Java的文件命名约定,您必须提供与包含Main Method的类名相同的文件名。但根据你的主要方法的课程,它是考试,所以我建议你把它改为Exam2而不是考试。