2010-10-20 42 views
0

爲Blackjack製作一個簡單的Java代碼並需要一些建議。同時在Blackjack中提問

好吧我正在嘗試使這個循環,並停止當我說「留」 任何建議?

while (name4.equals("stay")) 
{ 
    System.out.print("Would you like to hit or stay? "); 
    String name4 = keyboard.next(); 

    total2 = total+number4; 
    number4 = random.nextInt(9)+2; 

    System.out.println("You drew a " + number4 + ". "); 
    System.out.println("Your total is " + (total + number4) + ". "); 

    if (total == 21) 
    { 
      System.out.print("YOU WIN!"); 
    } 

} 

回答

1

只需添加一個!

你的while循環表達爲純英文:雖然name4不等於「停留」,繼續循環。

while (!name4.equals("stay")) 
{ 
    System.out.print("Would you like to hit or stay? "); 
    String name4 = keyboard.next(); 

    total2 = total+number4; 
    number4 = random.nextInt(9)+2; 

    System.out.println("You drew a " + number4 + ". "); 
    System.out.println("Your total is " + (total + number4) + ". "); 

    if (total == 21) 
    { 
      System.out.print("YOU WIN!"); 
    } 

}