Java 从 Main Method 重新运行 Main 方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19614599/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 18:46:35  来源:igfitidea点击:

Rerun Main method from Main Method

javanetbeans

提问by Jarod Wood

Basically the last thing I need to do for this Math Quiz I have to program, I have to ask the user if they would like to answer more problems, if yes, rerun everything in the Main Method. If no, print goodbye. The no is easy, but I'm unsure how to tell it to rerun the main method if they say yes. Here is the code in my main method.

基本上我需要为这个数学测验做的最后一件事我必须编程,我必须问用户他们是否想要回答更多的问题,如果是,请重新运行 Main Method 中的所有内容。如果没有,请打印再见。否很容易,但如果他们说是,我不确定如何告诉它重新运行 main 方法。这是我的主要方法中的代码。

 public static void main(String[] args) {
    Scanner in = new Scanner(System.in);

    int digit = 0;

    String result1 = getUserChoice("");

    digit = getNumberofDigit1(digit);

    int numberOfProblems = amountOfProblems();

    for (int i = 0; i < numberOfProblems; i++) {
    int number1 = getRandomNumber1(digit);
    int number2 = getRandomNumber2(digit);

    System.out.println("Enter your answer to the following problem: \n" + 
            number1 + result1 + number2);
    int correctAnswer = getCorrectAnswer(number1, result1, number2);

    int userAnswer = getUserAnswer();

    CheckandDisplayResult(correctAnswer, userAnswer);
    }
        System.out.println("Would you like to solve more probelms(Y/N)? ");
        String moreProblems = in.next();

        if ("Y".equals(moreProblems)){
            digit = 0;

    result1 = getUserChoice("");

    digit = getNumberofDigit1(digit);

    numberOfProblems = amountOfProblems();

    for (int i = 0; i < numberOfProblems; i++) {
    int number1 = getRandomNumber1(digit);
    int number2 = getRandomNumber2(digit);

    System.out.println("Enter your answer to the following problem: \n" + 
            number1 + result1 + number2);
    int correctAnswer = getCorrectAnswer(number1, result1, number2);

    int userAnswer = getUserAnswer();

    CheckandDisplayResult(correctAnswer, userAnswer);
    }
        System.out.println("Would you like to solve more probelms(Y/N)? ");
        moreProblems = in.next();

          if ("Y".equals(moreProblems)){

        }
        System.out.println("Thank you for taking this quiz, Goodbye!");

}

Now I have tried something like,

现在我尝试过类似的东西,

if "Y".equals(moreProblems)){ copy and past the main method }

if "Y".equals(moreProblems)){ 复制并传递 main 方法 }

But that has the error of requiring an infinite loops as you'd have to have the more problems statement in every if of yes, meaning it would never end coding wise, you would keep copying and pasting forever.

但这有一个需要无限循环的错误,因为您必须在每个 if 的 if 中都有更多的问题语句,这意味着它永远不会明智地结束编码,您将永远复制和粘贴。

回答by Ernest Friedman-Hill

What you can do is move everything in main()into another static method, call it interact(). Then in main(), just have logic which calls interact()as long as the user wants to interact with your program. In other words, put the math quiz into one method, and the business of presenting the quiz into main(). Your program will be easier to read and easier to modify further, if needed.

您可以做的是将所有内容移动main()到另一个静态方法中,称为interact(). 然后在 中main(),只要interact()用户想要与您的程序交互,就需要调用逻辑。换句话说,将数学测验纳入一种方法,并将测验呈现为main(). 如果需要,您的程序将更易于阅读且更易于进一步修改。

回答by Christian

You could enclose all the code you want to "re-run" in a while loop:

您可以在 while 循环中包含要“重新运行”的所有代码:

boolean run = true;
while (run) {
    // Here your code
    // Here input if user want to re-run
    if (getUserChoice("").equals("NO"))
        run = false;
}

回答by Radiodef

Put all of it in a big do-while loop:

将所有内容放在一个大的 do-while 循环中:

boolean more = false;

do {

    // all your code

    more = "Y".equals(moreProblems);

} while (more);

Or provided your Scanner is declared outside the loop you can just:

或者,如果您的 Scanner 是在循环之外声明的,您可以:

do {

    // all your code

} while ("Y".equals(in.next()));

回答by nhgrif

Alternative to what others have suggested, this is the method I prefer:

替代其他人的建议,这是我更喜欢的方法:

while(true) {
    //do all your stuff
    if(/*some exit condition*/) { break; }
}

回答by Vikram

I guess this prototype might help you

我想这个原型可能对你有帮助

import java.util.Scanner;
 public class MathsClass {
    public static void main(String[] args){
        MathsClass object = new MathsClass();

        while(object.response())
            object.mathsQuiz();

    }

    public void mathsQuiz(){
        //your quiz functionalities
        System.out.println("Add two nos");
    }

    public boolean response(){
        System.out.println("Would u like to solve more problems? ");
        Scanner scanner = new Scanner(System.in);
        boolean response = scanner.nextBoolean();

        return response;
    }
}