tags:

views:

26

answers:

1

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

}
+1  A: 

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

}
Jacob Relkin