# Simple Do Loop Problem

• October 10th, 2012, 11:16 AM
inflames098
Simple Do Loop Problem
I created a simple Blackjack game. Works great, but I am trying to have the user play the game multiple times. So, I set up a do loop giving them a choice to press Y or N to play the game, but Eclipse is not liking a variable that I am assigning. Either I am misplacing the while loop that I am declaring to make the statement true or I totally fudging up the code. The portion is at the end of the code. Thanks for any advice.

Code :

```import java.util.Random; import java.util.Scanner;   public class BlackJack {   public static void main(String[] args) {   Scanner scan = new Scanner(System.in); // Allows users to input information   BlackJack2 black = new BlackJack2();   Random r = new Random();   System.out.println("Welcome to Ultimate Blackjack!!!"); System.out.println("~ Would you like to play Ultimate Blackjack!?!? ~ Yes (y) or No (n)"); String input = scan.next(); if (input.equalsIgnoreCase("y")) { black.play(true); } else return; // Program quits when no is pressed   int computer = 14 + r.nextInt(9); // Create Random Card Total for Computer black.setcomputer(computer); // Stores Card Total for Computer   int firstCard = 1 + r.nextInt(11); // 1st Card: Random Number is picked. Set range from 1 to 11. Aces are 11 or 1, but thats not defined here. black.setfcard(firstCard); // 1st Card: Number is stored   int secondCard = 1 + r.nextInt(10); // Creates Random Number for 2nd Card. Range is from 1 to 10 because in order to make the game fair and not let the user // go over 21 to begin with, need to have maximum of 10. Again, aces are 11 or 1, but thats not defined here. black.setscard(secondCard); // Stores Card Number for Second Card integer   int player = secondCard + firstCard; // Adds the two values together to generate a beginning score. black.setplayer(player); System.out.println("Your first card was " + black.getfcard() + " and your second card was " + black.getscard());   while (black.play()) { // while loop begins for the first two cards. System.out.println("Your total is " + black.getplayer()); if (black.getplayer() == 21) { System.out.println("You have 21! You hit Blackjack!"); } else System.out.println("Would you like another card? Hit (h) or Stand (s)"); // if player does not hit 21, then they get to decide on h or s. input = scan.next(); // allows user input   while (input.equalsIgnoreCase("h")) { // if player hits h, this while loop executes   int ncard = 1 + r.nextInt(10); // Creates Random Number for New Card   black.setncard(ncard); black.setplayer(ncard);   System.out.println("Your new card is " + black.getncard()); // New card generated System.out.println("Your new total is " + black.getplayer()); // New total calculated   black.resetncard();   if (black.getplayer() <= 21); { System.out.println("Would you like another card? Hit (h) or Stand (s)?"); input = scan.next(); } } do { if (input.equalsIgnoreCase("s")) { // if player selects s, these statements execute, depending on result if ( black.getplayer() > 21 ) { System.out.println("You had " + black.getplayer() + " and the computer had " + black.getcomputer() + ". The computer wins!!!");   } if ( black.getplayer() > black.getcomputer() ) { System.out.println("You had " + black.getplayer() + " and the computer had " + black.getcomputer() + ". You Are The Champion of the World!!!");   } if ( black.getplayer() < black.getcomputer() ) { System.out.println("You had " + black.getplayer() + " and the computer had " + black.getcomputer() + ". Epic fail bro!!!");   } if ( black.getplayer() == black.getcomputer() ) { System.out.println("You had " + black.getplayer() + " and the computer had " + black.getcomputer() + ". You both drew!!!"); } boolean play; String playAgain; System.out.println("Play again?"); playAgain = scan.nextLine(); if(playAgain.equals("yes")){ play = true; } if(playAgain.equals("no")){ play=false; } } }while(play==true); } } }```
• October 10th, 2012, 11:35 AM
KevinWorkman
Re: Simple Do Loop Problem
Yikes, is that really how you indent your code? I'd recommend fixing that before you do anything (should be pretty easy in eclipse).

What's the actual problem? Do you see an error? Some strange behavior? Does your program gain sentience and send a Terminator back in time to kill Sarah Connor?
• October 10th, 2012, 02:11 PM
inflames098
Re: Simple Do Loop Problem
yeah, i know organization is pretty bad for the moment, sorry for that. that would be cool if i could send arnold back in time to end the world, but, i'm not at to that level yet. :o i am using eclipse (excellent program) but the game is not executing my if statement at all.

What I mean is that one of goals is to allow the user to play the game multiple times. I set up a do loop, with an if statement and boolean statement, however, the game is not executing at all. Eclipse is telling me that 'play' cannot be resolved to a variable. sorry again for the horrible indentation, i am a noob at Java at the moment.
• October 10th, 2012, 03:43 PM
jps
Re: Simple Do Loop Problem
The error message also shows a line number. That line is where the variable is no longer in scope. Correct your indentation in the code and you can more easily see the scope of the variable in question. From there you can determine why the variable is not in scope and do something to correct the issue.