而在二十一点的问题

时间:2010-10-20 15:46:26

标签: java while-loop

为Blackjack制作一个简单的Java代码,需要一些建议。

好的我正试图制作这个循环,当我说“停留”时停止 任何建议??

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 个答案:

答案 0 :(得分: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!" );
     }

}