2016-11-10 64 views
0

我目前正在做一個類似老虎機的項目。我遇到了需要循環IF語句的問題,但只有在用戶輸入爲YES的情況下才需要循環?這是我正在嘗試做的一些示例代碼。卡住循環if語句本身

int num1 = 0; 
int num2 = 0; 
int num3 = 0; 

Scanner scan = new Scanner(System.in); 
Random numRand = new Random(); 

num1 = numRand.nextInt((9 - 0) + 1); 
num2 = numRand.nextInt((9 - 0) + 1); 
num3 = numRand.nextInt((9 - 0) + 1); 

System.out.println(num1 + " " + num2 + " " + num3); 

if(num1 == num2 && num1 == num3 && num2 == num3) { 
    System.out.println("All three match - jackpot"); 
    System.out.printf("Would you like to play again? "); 
    String Yes = scan.nextLine(); 
    if(Yes.equals("y")) { 

    } 
    String No = scan.nextLine(); 
    if(No.equals("n")) { 

    } 
} 
else if(num1 == num2 || num2 == num3 || num1 == num3) { 
    System.out.println("Two number match"); 
    System.out.printf("Would you like to play again? "); 
    String Yes = scan.nextLine(); 
    if(Yes.equals("y")) { 

    } 
    String No = scan.nextLine(); 
    if(No.equals("n")) { 

    } 
} 
else { 
    System.out.println("No numbers match"); 
} 
scan.close(); 

從我的代碼,你可以看到,在IF語句中,我想如果語句運行另一個用於當用戶輸入= Y(代表是)這是這樣,如果用戶要輸入y當提示時,if語句會循環。

成果都表示,如果所有的3號匹配,則輸出: 如果2號匹配,輸出: 如果沒有號碼匹配,則輸出:

我希望你明白

+1

直視'while'循環:https://docs.oracle.com/javase/tutorial/java/nutsandbolts/while.html – rafid059

+1

此外,考慮'break'聲明。 http://stackoverflow.com/questions/7951690/how-do-i-exit-a-while-loop-in-java – rafid059

+1

爲了進一步澄清@RafiduzzamanSonnet評論,請了解一下do ... while循環。舉個簡單的例子,試着提示用戶輸入一個數字,直到他們輸入9.另外,想想你的if語句之外,是否真的需要,或者你可以以某種方式將它用作循環條件?祝你好運! – Stefan

回答

1

我建議一個do while構造。

Scanner scan = new Scanner(System.in); 
Random numRand = new Random(); 

boolean keep_playing = true; 

do 
{ 
    num1 = numRand.nextInt((9 - 0) + 1); 
    num2 = numRand.nextInt((9 - 0) + 1); 
    num3 = numRand.nextInt((9 - 0) + 1); 

    System.out.println(num1 + " " + num2 + " " + num3); 

    if(num1 == num2 && num1 == num3) { // && num2 == num3 - unnecessary 
     System.out.println("All three match - jackpot"); 
    } 
    else if(num1 == num2 || num2 == num3 || num1 == num3) { 
     System.out.println("Two number match"); 
    } 
    else { 
     System.out.println("No numbers match"); 
    } 

    System.out.printf("Would you like to play again? "); 

    String input = scan.nextLine(); 
    keep_playing = input.equals("y"); 

} while (keep_playing) 

scan.close(); 
+0

謝謝你這麼多!這是做這件事的正確方法,你幫助我度過了美好時光! – SeanHub

+0

@SeanHub沒問題,很樂意幫忙。 – Michael

1

1.遞歸

有兩種方法可以做到這一點,一件事是通過recursion來處理它。例如。如果所有這些代碼都存在於具有此簽名的功能中,那麼您只需向其添加一個調用即可。

if(Yes.equals("y")) { 
     main(new String[0]); // String[0] is just to satisfy the arguments of the main function, if yours requires other arguments, put them there 
    } 

下行的,這是你的代碼將創建掃描儀等。另外在某些時候,你會看到異常,因爲你會用完棧。

2. While循環

另一種選擇是把所有的代碼爲while建設和使用continue執行所有的代碼裏面一次,並break離開循環:

while (true) { 
    // S: loop start 
    num1 = numRand.nextInt((9 - 0) + 1); 
    . . . 
    if(Yes.equals("y")) { 
     continue; // this will cause us to go to S 
    } else { 
     break; // this will cause us to go to A 
    } 
} 
// A: after loop 
+1

您也可以使用布爾變量而不是無限循環,有些人發現更容易閱讀。 – jthort

+0

使用你的遞歸方法確實幫助了很大的時間,但我仍然遇到了一些問題,以後的循環工作就像我想要的那樣!謝謝你,雖然:) – SeanHub