基本上我需要为这个数学测验做的最后一件事是我必须编程,我必须询问用户他们是否想回答更多问题,如果是,重新运行 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!");
}
现在我尝试了类似的东西,
if "Y".equals(moreProblems)){ 复制并过去 main 方法}
但这有一个需要无限循环的错误,因为您必须在每个“是”的情况下都有更多的问题陈述,这意味着它永远不会以明智的方式结束编码,您将永远保持复制和粘贴。