基本上我需要爲這個數學測驗做最後一件事我必須編程,我必須問用戶他們是否想回答更多問題,如果是的話,重新運行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!");
}
現在我想是這樣,
如果 「Y」 .equals(moreProblems)){ 副本和以往的主要方法 }
但是,這需要有一個的錯誤無限循環,因爲如果是的話,你必須有更多的問題陳述,這意味着它永遠不會結束編碼明智的,你會永遠複製和粘貼。
我想過,但是分配規定,所有方法都必須從主方法調用,所以測驗必須留在主要方法部分。 –
那麼,按照其他人的建議,與while循環一起去吧。 –