'How to fix class name already defined in java eclipse. The error is found in my second line "public class Quiz". It says that Quiz aready defined
import javax.swing.JOptionPane;
public class Quiz {
static int nQuestions = 0;
static int nCorrect = 0;
static String ask(String question) {
while (true) {
String answer = JOptionPane.showInputDialog(question);
answer = answer.toUpperCase();
boolean valid = (answer.equals("A") || answer.equals("B") || answer.equals("C") ||
answer.equals("D") || answer.equals("E"));
if (valid) return answer;
JOptionPane.showMessageDialog(null, "Invalid answer. Please enter A, B, C, D, or
E.");
}
}
static void check(String question, String correctAnswer) {
nQuestions++;
String answer = ask(question);
if (answer.equals(correctAnswer)) {
JOptionPane.showMessageDialog(null, "Correct!");
nCorrect++;
} else {
JOptionPane.showMessageDialog(null, "Incorrect. The correct answer is
"+correctAnswer+".");
}
}
public static void main(String[] args) {
String question = "In what year did World War 1 end?\n";
question += "A. 1918\n";
question += "B. 1922\n";
question += "C. 1931\n";
question += "D. 1914\n";
question += "E. 1929\n";
check(question, "A");
question = "When did USA land on the moon?\n";
question += "A. 1978\n";
question += "B. 1950\n";
question += "C. 1989\n";
question += "D. 1969\n";
question += "E. 1972\n";
check(question, "D");
question = "What is not a country?\n";
question += "A. Congo\n";
question += "B. New Zealand\n";
question += "C. Australia\n";
question += "D. Russia\n";
question += "E. Canada\n";
check(question, "C");
JOptionPane.showMessageDialog(null, nCorrect + " correct out of " + nQuestions + "
questions ");
}
}
The problem lies in line two where my class quiz is. When I run the code, it says, "pubic class "Quiz" already defined. Al of this is in my Quiz.java file. Please help if you can.How to fix class name already defined in java eclipse. The error is found in my second line "public class Quiz". It says that Quiz aready defined.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
