2013-10-27 119 views
0

基本上我需要爲這個數學測驗做最後一件事我必須編程,我必須問用戶他們是否想回答更多問題,如果是的話,重新運行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)){ 副本和以往的主要方法 }

但是,這需要有一個的錯誤無限循環,因爲如果是的話,你必須有更多的問題陳述,這意味着它永遠不會結束編碼明智的,你會永遠複製和粘貼。

回答

1

你可以做的是將main()中的所有東西都轉換爲另一種靜態方法,稱之爲interact()。然後在main()中,只要用戶想要與您的程序進行交互,就可以調用interact()。換句話說,將數學測驗放入一種方法中,並將測驗呈現爲main()。如果需要,您的程序將更易於閱讀並且更容易進一步修改。

+0

我想過,但是分配規定,所有方法都必須從主方法調用,所以測驗必須留在主要方法部分。 –

+0

那麼,按照其他人的建議,與while循環一起去吧。 –

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

將所有東西放入一個大do-while循環:

boolean more = false; 

do { 

    // all your code 

    more = "Y".equals(moreProblems); 

} while (more); 

或者只要您的掃描儀在循環外聲明,您可以:

do { 

    // all your code 

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

替代別人的建議,這是方法d我更喜歡:

while(true) { 
    //do all your stuff 
    if(/*some exit condition*/) { break; } 
} 
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; 
    } 
} 
+0

有人能告訴我爲什麼我收到了投票嗎? – Vikram