类 System 中的方法退出不能应用于给定类型

Posted

技术标签:

【中文标题】类 System 中的方法退出不能应用于给定类型【英文标题】:Method exit in class System cannot be applied to given types 【发布时间】:2014-03-14 09:01:51 【问题描述】:

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

我有 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

【问题讨论】:

它需要一个int 参数。请阅读 javadoc。 docs.oracle.com/javase/7/docs/api/java/lang/… 是的,这个问题的答案是非常熟悉 Java API,因为它是学习使用这个工具的必要技能,经常使用它,并且好好利用。 【参考方案1】:

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)

【讨论】:

【参考方案2】:

System.exit(int status),有一个 int 参数。 而是使用:。

System.exit(0);

【讨论】:

以上是关于类 System 中的方法退出不能应用于给定类型的主要内容,如果未能解决你的问题,请参考以下文章

错误:类 SpotsDialog 中的构造函数 SpotsDialog 不能应用于给定类型;

firebasex 错误:类 Builder 中的构造函数 Builder 不能应用于给定类型

android:如何修复类 CardsActivity.CardPagerAdapter 中的错误构造函数 CardPagerAdapter 不能应用于给定类型

如何在Java中修复“类Rectangle中的构造函数不能应用于给定类型”错误?

接口 java.util.stream.Stream<T> 中的方法映射不能应用于给定类型;

方法不能应用于给定类型。希望得到帮助