gpt4 book ai didi

java.lang.NoClassDefFoundError 如何解决这个问题?

转载 作者:行者123 更新时间:2023-12-01 14:46:06 25 4
gpt4 key购买 nike

运行此代码时出现以下错误:

这是完整的 Stackflow,如有任何意见,我们将不胜感激;

Exception in thread "main" java.lang.NoClassDefFoundError: ExecuteQuiz
Caused by: java.lang.ClassNotFoundException: ExecuteQuiz
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)

知道为什么吗?我还有其他 7 个类参与该项目,但从未见过此错误。

    import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

public class ExecuteQuiz {

static Scanner input = new Scanner(System.in);

public static void main(String[] args) throws IOException {
// ask the user for the filename
Scanner scan = new Scanner(System.in);

System.out.print("Which quiz are you taking? ");
String theFile = scan.nextLine(); // file may contain more than one word
File fileIn = new File(theFile);

// ask the user for another filename if the given file doesn't exist
// exists() method in File class - checks whether file
// exists and is readable
while (!fileIn.exists()) {
System.out.print("Invalid file name! Try again: ");

theFile = scan.nextLine();

fileIn = new File(theFile);
}

// have a valid file name, create a Scanner object
Scanner fileScan = new Scanner(fileIn);

// An arraylist of ALL problems.
ArrayList<Problem> problems = new ArrayList<Problem>();

// process the file
while (fileScan.hasNextLine()) {
String type = scan.nextLine(); // Get the line in a string
String question = scan.nextLine();

switch (type) {
case "W":
String WAnswer = scan.nextLine();
WProblem w = new WProblem(question, WAnswer);
problems.add(w);
break;
case "T":
String TString = scan.nextLine(); // Gets the string
boolean TAnswer = Boolean.parseBoolean(TString); // Converts to
// boolean
TProblem t = new TProblem(question, TAnswer); // Creates the
// object
problems.add(t);
break;
case "N":
String Nanswer = scan.nextLine();
NProblem n = new NProblem(question, Nanswer);
problems.add(n);
break;
case "S":
ArrayList<String> options = new ArrayList<String>();
// Get the answer and add it to the options
String SAnswer = input.nextLine();
options.add(SAnswer);

// add the rest of options
while (input.nextLine() != null) {
String option = input.nextLine();
options.add(option);
}
// Create new objects
SProblem s = new SProblem(question, SAnswer, options);

problems.add(s);
break;
case "M":
ArrayList<String> MAnswer = new ArrayList<String>();
ArrayList<String> MOptions = new ArrayList<String>();

// Find all the answers
while (input.nextLine() != null) {
String answer = input.nextLine();
MAnswer.add(answer);
MOptions.add(answer);
}

// get the rest of the options
while (input.nextLine() != null) {
MOptions.add(input.nextLine());
}

MProblem m = new MProblem(question, MAnswer, MOptions);
problems.add(m);
break;
case "O":
// Adding answers into an arraylist
ArrayList<String> OrderedAnswer = new ArrayList<String>();

// Add the answers in order
while (input.nextLine() != null) {
OrderedAnswer.add(input.nextLine());
}
OProblem o = new OProblem(question, OrderedAnswer);
problems.add(o);
break;
}
// Analyze the type of problem

}
}
}

最佳答案

您的类存在重大编译时间问题。你甚至编译过代码并检查过吗??

switch case 在编译中给出错误:不兼容的类型

该类尚未编译,您正尝试直接运行它..因此没有找到类定义错误。

尝试更改部分:

case "W": 

case 1:

编译然后运行

我使用了以下代码:

import java.io.File;
import java.io.IOException;

import java.util.ArrayList;
import java.util.Scanner;


public class ExecuteQuiz {

static Scanner input = new Scanner(System.in);

public static void main(String[] args) throws IOException {
// ask the user for the filename
Scanner scan = new Scanner(System.in);

System.out.print("Which quiz are you taking? ");
String theFile = scan.nextLine(); // file may contain more than one word
File fileIn = new File(theFile);

// ask the user for another filename if the given file doesn't exist
// exists() method in File class - checks whether file
// exists and is readable
while (!fileIn.exists()) {
System.out.print("Invalid file name! Try again: ");

theFile = scan.nextLine();

fileIn = new File(theFile);
}

// have a valid file name, create a Scanner object
Scanner fileScan = new Scanner(fileIn);

// An arraylist of ALL problems.
ArrayList<Problem> problems = new ArrayList<Problem>();

// process the file
while (fileScan.hasNextLine()) {
int type = Integer.parseInt(scan.nextLine()); // Get the line in a string
String question = scan.nextLine();

switch (type) {
case 1:
String WAnswer = scan.nextLine();
WProblem w = new WProblem(question, WAnswer);
problems.add(w);
break;
case 2:
String TString = scan.nextLine(); // Gets the string
boolean TAnswer = Boolean.parseBoolean(TString); // Converts to
// boolean
TProblem t = new TProblem(question, TAnswer); // Creates the
// object
problems.add(t);
break;
case 3:
String Nanswer = scan.nextLine();
NProblem n = new NProblem(question, Nanswer);
problems.add(n);
break;
case 4:
ArrayList<String> options = new ArrayList<String>();
// Get the answer and add it to the options
String SAnswer = input.nextLine();
options.add(SAnswer);

// add the rest of options
while (input.nextLine() != null) {
String option = input.nextLine();
options.add(option);
}
// Create new objects
SProblem s = new SProblem(question, SAnswer, options);

problems.add(s);
break;
case 5:
ArrayList<String> MAnswer = new ArrayList<String>();
ArrayList<String> MOptions = new ArrayList<String>();

// Find all the answers
while (input.nextLine() != null) {
String answer = input.nextLine();
MAnswer.add(answer);
MOptions.add(answer);
}

// get the rest of the options
while (input.nextLine() != null) {
MOptions.add(input.nextLine());
}

MProblem m = new MProblem(question, MAnswer, MOptions);
problems.add(m);
break;
case 6:
// Adding answers into an arraylist
ArrayList<String> OrderedAnswer = new ArrayList<String>();

// Add the answers in order
while (input.nextLine() != null) {
OrderedAnswer.add(input.nextLine());
}
OProblem o = new OProblem(question, OrderedAnswer);
problems.add(o);
break;
}
// Analyze the type of problem

}
}
}

关于java.lang.NoClassDefFoundError 如何解决这个问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15425039/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com