开发者

While Question in Blackjack

开发者 https://www.devze.com 2023-01-20 20:27 出处:网络
Making a simple Java code for Blackjack and need some advice. OK I am trying to make this loop and stop when I say \"stay\"

Making a simple Java code for Blackjack and need some advice.

OK I am trying to make this loop and stop when I say "stay" any suggestions??

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 WI开发者_如何学CN!" );
     }

}


Simply add a !.

Your while loop expression in plain English: While name4 is not equal to "stay", continue looping.

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!" );
     }

}
0

精彩评论

暂无评论...
验证码 换一张
取 消