i have gone through the code and tried new things over and over and can't seem to get rid of this error. i am writing a program where i scan a file as input and it goes through and finds average, max, and min. but i have had aproblem towards the end of the code where i have to give a grade for each score and then count up how many of each grades there are. it compiles fine but when i go to run it the java.langNoClassDefFoundError: keeps coming up please help if you can.
我已经完成了代码并反复尝试了新的东西,似乎无法摆脱这个错误。我正在写一个程序,我扫描一个文件作为输入,它通过并找到平均值,最大值和最小值。但是我已经在代码的末尾遇到了问题,我必须给每个分数评分,然后计算每个分数的数量。它编译得很好但是当我去运行它时,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;
}
}
2 个解决方案
#1
3
Your stacktrace shows error Exception in thread "main" java.lang.NoClassDefFoundError: Exam2
.
您的stacktrace在线程“main”中显示错误异常java.lang.NoClassDefFoundError:Exam2。
But your class is named Exam
. So change your filename to Exam.java
from Exam2.java
但是你的班级名为考试。因此,请将您的文件名从Exam2.java更改为Exam.java
#2
1
Here you have created a Java file with name Exam2.java. As per Java's File naming convention you must give your file name same as the class name which contains Main Method. But as per your Main Method's class it is Exam, So I would suggest you to change it to Exam2 instead of class Exam.
在这里,您创建了一个名为Exam2.java的Java文件。根据Java的文件命名约定,您必须提供与包含Main Method的类名相同的文件名。但根据你的主要方法的课程,它是考试,所以我建议你把它改为Exam2而不是考试。
#1
3
Your stacktrace shows error Exception in thread "main" java.lang.NoClassDefFoundError: Exam2
.
您的stacktrace在线程“main”中显示错误异常java.lang.NoClassDefFoundError:Exam2。
But your class is named Exam
. So change your filename to Exam.java
from Exam2.java
但是你的班级名为考试。因此,请将您的文件名从Exam2.java更改为Exam.java
#2
1
Here you have created a Java file with name Exam2.java. As per Java's File naming convention you must give your file name same as the class name which contains Main Method. But as per your Main Method's class it is Exam, So I would suggest you to change it to Exam2 instead of class Exam.
在这里,您创建了一个名为Exam2.java的Java文件。根据Java的文件命名约定,您必须提供与包含Main Method的类名相同的文件名。但根据你的主要方法的课程,它是考试,所以我建议你把它改为Exam2而不是考试。