Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Simple Do Loop Problem

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    6
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default 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.

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


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default 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?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Oct 2012
    Posts
    6
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default 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. 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.

  4. #4
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default 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.

  5. The Following User Says Thank You to jps For This Useful Post:

    inflames098 (October 10th, 2012)

Similar Threads

  1. One of the most simple loop awnsers
    By Firestar912 in forum Loops & Control Statements
    Replies: 8
    Last Post: May 7th, 2012, 07:13 PM
  2. [SOLVED] WHILE LOOP USING || (or) SIMPLE PROBLEM
    By SPACE MONKEY in forum Loops & Control Statements
    Replies: 3
    Last Post: May 7th, 2012, 12:23 AM
  3. Simple Nested Do/While Loop
    By Farmer in forum Loops & Control Statements
    Replies: 2
    Last Post: July 25th, 2011, 08:31 AM
  4. Replies: 4
    Last Post: May 15th, 2011, 06:03 AM
  5. boolean value in a simple loop (do-while)
    By chronoz13 in forum Loops & Control Statements
    Replies: 5
    Last Post: October 18th, 2009, 12:05 AM