0
當以下Java程序被編譯,則出現下列錯誤:類系統方法退出不能被應用到給定的類型的
方法退出不能被應用到給定的類型;
我有2個類,MainClass
和MathQuiz
。
MainClass:
class MainClass {
public static void main(String args[]) {
MathQuiz mq = new MathQuiz(); //whenever "mq" is written, this refers to class "MathQuiz"
mq.initialiseVars(); //initialise variables (from MathQuiz)
byte exitChoice = 0;
//ask user for choice
mq.showOprMenu(); //show operation menu (from MathQuiz)
byte oprChoice = Keyboard.readByte(); //ask for user input
//if input is out of range (smaller than 0 or greater than 4), ask again
while(oprChoice < 0 || oprChoice > 4) {
System.out.print("Invalid choice! Enter your choice again (1, 2, 3, 4 or 0): ");
oprChoice = Keyboard.readByte();
}//while
mq.showDiffMenu(); //show difficulty menu (from MathQuiz)
byte diffChoice = Keyboard.readByte(); //ask for user input
//if input is out of range (smaller than 0 or greater than 3), ask again
while(diffChoice < 0 || diffChoice > 3) {
System.out.print("Invalid choice! Enter your choice again (1, 2, 3 or 0): ");
diffChoice = Keyboard.readByte(); //ask for user input
}//while
mq.generateRandNums(); //generate random numbers (from MathQuiz)
switch (exitChoice) {
case 0: {
while (exitChoice != 1 || exitChoice != 2); {
System.out.print("Are you sure you want to exit? (1 = Yes, 2 = No) ");
exitChoice = Keyboard.readByte(); //ask for user input
if (exitChoice == 1) {
System.out.println("Exiting program.");
**System.exit(); //quit program**
}//if exitChoice is 1
else if (exitChoice == 2) {
System.out.println("Returning to menu.");
mq.showOprMenu();
}//if exitChoice is 2
}//while exitChoice is neither 1 nor 2
}//case 0 ("Exit" chosen)
case 1: {
System.out.println("You will be tested on addition.");
mq.showAddQuestion(); //show addition question (from MathQuiz)
mq.checkAns(); //check answer (from MathQuiz)
break;
}//case 1 ("Addition" chosen)
case 2: {
System.out.println("You will be tested on subtraction.");
mq.showSubQuestion(); //show subtraction question (from MathQuiz)
mq.checkAns(); //check answer (from MathQuiz)
break;
}//case 2 ("Subtraction" chosen)
case 3: {
System.out.println("You will be tested on multiplication.");
mq.showMultQuestion(); //show multiplication question (from MathQuiz)
mq.checkAns(); //check answer (from MathQuiz)
break;
}//case 3 ("Multiplication" chosen)
case 4: {
System.out.println("You will be tested on division.");
mq.showDivQuestion(); //show division question (from MathQuiz)
mq.checkDivAns(); //check answer and remainder (from MathQuiz)
break;
}//case 4 ("Division" chosen)
}//switch (exitChoice)
}//main
}//class
MathQuiz:
class MathQuiz {
//properties
byte diffChoice, oprChoice, exitChoice;
int num1, num2, userAns, correctAns, userRem, correctRem, score, numOfQuestions;
//methods
public void showOprMenu() {
System.out.println("-------------------");
System.out.println("Mathematical Quiz");
System.out.println(); //skip a line
System.out.println("1. Addition");
System.out.println("2. Subtraction");
System.out.println("3. Multiplication");
System.out.println("4. Division");
System.out.println("0. Exit");
System.out.println("-------------------");
System.out.print("Enter your choice (1, 2, 3, 4 or 0): ");
}//show operation menu
public void showDiffMenu() {
System.out.println("-------------------");
System.out.println("Difficulty Settings");
System.out.println();
System.out.println("1. Easy");
System.out.println("2. Medium");
System.out.println("3. Hard");
System.out.println("0. Go back to Operation Menu");
System.out.println("-------------------");
System.out.print("Enter your choice (1, 2, 3 or 0): ");
}//showDiffMenu();
//initialise variables
public void initialiseVars() {
userAns = 0;
correctAns = 0;
userRem = 0;
correctRem = 0;
score = 0;
numOfQuestions = 0;
diffChoice = 0;
exitChoice = 0;
oprChoice = 0;
}//initialiseVars();
//generate random values for num1 and num2
public void generateRandNums() {
switch (diffChoice) {
case 0: showOprMenu(); break; //case 0 (back to operation menu)
case 1: {
//generate random values for num1 and num2 (between 1 and 10)
num1 = (int) (Math.random() * (10));
num2 = (int) (Math.random() * (10));
break;
}//case 1 (easy)
case 2: {
//generate random values for num1 and num2 (between 1 and 100)
num1 = (int) (Math.random() * (100));
num2 = (int) (Math.random() * (100));
break;
}//case 2 (medium)
case 3: {
//generate random values for num1 and num2 (between 1 and 1000)
num1 = (int) (Math.random() * (1000));
num2 = (int) (Math.random() * (1000));
break;
}//case 3 (hard)
}//switch (diffChoice)
}//generateRandNums();
//show addition question
public void showAddQuestion() {
System.out.print(num1 + " + " + num2 + " = ");
userAns = Keyboard.readInt();
correctAns = num1 + num2;
}//showAddQuestion();
//show subtraction question
public void showSubQuestion() {
System.out.print(num1 + " - " + num2 + " = ");
userAns = Keyboard.readInt();
correctAns = num1 - num2;
}//showSubQuestion();
//show multiplication question
public void showMultQuestion() {
System.out.print(num1 + " * " + num2 + " = ");
userAns = Keyboard.readInt();
correctAns = num1 * num2;
}//showMultQuestion();
//show division question
public void showDivQuestion() {
System.out.print(num1 + "/" + num2 + " = ");
userAns = Keyboard.readInt();
correctAns = num1/num2;
System.out.print("Remainder: ");
userRem = Keyboard.readInt();
correctRem = num1 % num2;
}//showDivQuestion();
//check if inputted answer is correct (addition/subtraction/multiplication)
public void checkAns() {
if(userAns == correctAns) {
System.out.println("Correct!");
score++;
}//if answer is correct
else System.out.println("Incorrect."); //if answer is incorrect
numOfQuestions++; //increases the question "counter" by 1 (both if correct or incorrect)
}//checkAns();
//check if inputted answer and remainder is correct (division)
public void checkDivAns() {
if(userAns == correctAns) System.out.println("Correct!"); //if answer is correct
else System.out.println("Incorrect."); //if answer is incorrect
if(userRem == correctRem) System.out.println("Correct!"); //if remainder is correct
else System.out.println("Incorrect."); //if remainder is incorrect
if((userAns == correctAns) && (userRem == correctRem)) score++; //if both answer and remainder are correct
numOfQuestions++; //increases the question "counter" by 1 (both if correct or incorrect)
}//checkDivAns();
}//class
它需要一個'int'參數。請閱讀javadoc。 –
http://docs.oracle.com/javase/7/docs/api/java/lang/System.html#exit(int) –
是的,這個問題的答案是**非常熟悉** Java API,因爲它是學習使用此工具的必要技能,經常使用它並使用它。 –