2016-03-04 57 views
-2

我的老師要求我們在Java 8中創建一個魔術8球程序。我們必須使用3個方法,一個主要的,一個處理和一個輸出,我們需要在這些方法之間傳遞參數。輸出需要使用switch語句,我們需要在那裏有一個while語句,並且需要隨機生成答案。我有所需的一切,但是當我嘗試運行該程序時,它被卡在while循環中,我不知道我做錯了什麼。 這是我有:我有一個完整的循環,不知道如何使它停止

import java.util.*; 
public class Magic8Ball { 
    public static void main(String[]args) { 
     Scanner input = new Scanner(System.in); 
     System.out.print("Would you like to ask a question? Y or N: "); 
     char answer = input.next().charAt(0); 
     char Y = Character.toUpperCase(answer); 
     process(answer, Y); 
    } 
    public static void process(char a, char Yes) { 
     if (a != Yes) { 
      System.out.println("Thank you, goodbye."); 
     } else { 
      while(a==Yes) { 
       System.out.print("Ask your question: "); 
       Random random = new Random(); 
       int ran = random.nextInt(8-1+1)+1; 
       output(ran); 
      } 
     } 
    } 
    Public static int output(int r) { 
     switch (r) { 
     case 1: System.out.println("Out of memory, try again later); break; 
     case 2: System.out.println("The probability matrix supports you."); break; 
     case 3: System.out.println("That does not compute."); break; 
     case 4: System.out.println("System error, try again later"); break; 
     case 5: System.out.println("Siri says yes."); break; 
     case 6: System.out.println("The asnwer could not be found on the internet."); break; 
     case 7: System.out.println("Wikilinks claims it is true."); break; 
     case 8: System.out.println("Siri says no."); break; 
     default: System.out.println("The system is not responding, try again later"); break; 
     } 
     return r; 
    } 
} 
+0

你沒有更新循環既不'了''也不Yes'合適的解決方案。 – MikeCAT

+0

當你想退出時,你必須更新循環內'a'的值,否則將會是一個無限循環 –

+0

條件是'while(a == Yes)',你永遠不會改變'a'或「是」。 (順便說一句,我不確定你爲什麼要做這個測試,它會檢查輸入是否等於輸入的大寫版本。) – usr2564301

回答

0

首先。有很多錯字在你的示例代碼,你必須關閉「在你的方法output()第一個print語句的結束。二錯誤,Java是區分大小寫,因此Public是不一樣的public

現在,讓我們試着給你的這個問題簡單的解決方案可能是這一個簡單的解決方案:在while循環的末尾添加回報,你可以在下面的代碼中看到

while(a == Yes) { 
    System.out.print("Ask your question: "); 
    Random random = new Random(); 
    int ran = random.nextInt(8-1+1)+1; 
    output(ran); 
    return; 
} 

這樣你停止循環打印第一個輸出後

請記住,return是一個「魔法世界」,你可以用它來告訴你的方法停下來並返回一個結果(在這種情況下,你沒有結果,因爲該方法返回一個void)。

它爲我,讓我知道,如果這是你的問題;-)

+0

爲什麼要使用return和while循環?擁有正確的布爾語句更有意義。在這種情況下,它只運行一次並返回。那麼while語句有什麼用? – Blaatz0r

+0

是的,你是對的,但我試圖離開結構,沒有修改代碼太多。我在Lisa S的另一篇文章中看到,如果他/她使用不同的方法,老師會刪除點。 –

+0

這是一件好事,他扣分。你應該學會正確,而不是不正確。 – Blaatz0r

1
import java.util.*; 
public class Magic8Ball { 

    public static void main(String[]args) { 

     System.out.print("Would you like to ask a question? Y or N: "); 
     Scanner infeedScanner = new Scanner(System.in); 
     char input = infeedScanner.next().charAt(0); 
     process(input, 'Y'); 
    } 

    public static void process(char a, char Yes) 
    { 
     if (a != Yes) 
     { 
     System.out.println("Thank you, goodbye."); 
     } 
     else 
     { 
      boolean bContinue = true; 

      while(bContinue) 
      { 
       System.out.print("Ask your question: "); 
       Random random = new Random(); 
       int ran = random.nextInt(8-1+1)+1; 
       output(ran); 

       System.out.print("Would you like to ask another question? Y or N: "); 
       Scanner infeedScanner = new Scanner(System.in); 

       if (infeedScanner.next().equalsIgnoreCase("n")) 
       { 
        bContinue = false; 
       } 
      } 
     } 
    } 

    public static int output(int r) 
    {  
     switch (r) { 
      case 1: System.out.println("Out of memory, try again later"); break; 
      case 2: System.out.println("The probability matrix supports you."); break; 
      case 3: System.out.println("That does not compute."); break; 
      case 4: System.out.println("System error, try again later"); break; 
      case 5: System.out.println("Siri says yes."); break; 
      case 6: System.out.println("The answer could not be found on the internet."); break; 
      case 7: System.out.println("Wikilinks claims it is true."); break; 
      case 8: System.out.println("Siri says no."); break; 
      default: System.out.println("The system is not responding, try again later"); break; 
     } 

     return r; 
    } 
} 
+0

我喜歡這個解決方案,它是一個很好的重構;-) –

相關問題