从主方法重新运行主方法

时间:2013-10-27 04:22:45

标签: java netbeans

基本上我需要为这个数学测验做最后一件事我必须编程,我必须问用户他们是否想要回答更多问题,如果是,请在主方法中重新运行所有内容。如果不是,请打印再见。不容易,但我不确定如果他们说是的话,如何告诉它重新运行主要方法。这是我的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!");

}

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

if“Y”.equals(moreProblems)){ 复制并通过主要方法 }

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

5 个答案:

答案 0 :(得分:3)

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

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

答案 1 :(得分:2)

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

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

答案 2 :(得分:1)

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

答案 3 :(得分:1)

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

boolean more = false;

do {

    // all your code

    more = "Y".equals(moreProblems);

} while (more);

或者提供的扫描仪是在循环外宣布的:

do {

    // all your code

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

答案 4 :(得分:0)

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

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;
    }
}