线程“main”中的异常java.lang.NoClassDefFoundError:
我已经浏览了代码并一遍又一遍地尝试新事物,但似乎无法摆脱这个错误。我正在编写一个程序,我扫描一个文件作为输入,它会遍历并找到平均值、最大值和最小值。但我在代码末尾遇到了一个问题,我必须为每个分数给出一个分数,然后计算每个分数有多少个。它编译得很好,但是当我去运行它时, 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;
}
}
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.
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;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的堆栈跟踪显示错误
Exception in thread "main" java.lang.NoClassDefFoundError: Exam2
。但您的班级名为
Exam
。因此,将文件名从Exam2.java
更改为Exam.java
Your stacktrace shows error
Exception in thread "main" java.lang.NoClassDefFoundError: Exam2
.But your class is named
Exam
. So change your filename toExam.java
fromExam2.java
这里您创建了一个名为 Exam2.java 的 Java 文件。根据 Java 的文件命名约定,您的文件名必须与包含 Main 方法的类名相同。但根据您的 Main Method 类,它是 Exam,所以我建议您将其更改为 Exam2 而不是 Exam 类。
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.