gpt4 book ai didi

java - System 类中的方法导出不能应用于给定类型

转载 作者:行者123 更新时间:2023-11-29 07:04:09 25 4
gpt4 key购买 nike

编译以下Java程序时,出现如下错误:

method exit in class System cannot be applied to given types;

我有 2 个类,MainClassMathQuiz

主类:

    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

数学测验:

   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

最佳答案

System.exit接受一个参数,您应该将退出状态传递给它,例如:

System.exit(0); // zero exit status usually means that program did its job without errors.

或者你可以简单地做:

return; // in the main method return basically has the same effect as System.exit(0)

关于java - System 类中的方法导出不能应用于给定类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21788883/

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