线程“main”中的异常java.lang.NoClassDefFoundError:

发布于 2024-12-29 20:55:14 字数 3521 浏览 1 评论 0原文

我已经浏览了代码并一遍又一遍地尝试新事物,但似乎无法摆脱这个错误。我正在编写一个程序,我扫描一个文件作为输入,它会遍历并找到平均值、最大值和最小值。但我在代码末尾遇到了一个问题,我必须为每个分数给出一个分数,然后计算每个分数有多少个。它编译得很好,但是当我去运行它时, 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

述情 2025-01-05 20:55:14

您的堆栈跟踪显示错误 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 to Exam.java from Exam2.java

时常饿 2025-01-05 20:55:14

这里您创建了一个名为 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文