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