I'm fairly new to Java (and programming in general). I've been getting everything done pretty easily so far but this issue is really frustrating me. I've been through things a thousand different ways and cannot figure out why it's doing this:
Here is my code:
import java.util.Scanner; public class Driver { public static void main (String args []) { // Declare and instantiate a new coin object Dice die1 = new Dice(); Dice die2 = new Dice(); double account; double wager; int total; String userReady; boolean result; // Create keyboard object Scanner keyboard = new Scanner(System.in); // Initial game banner System.out.println("Welcome to the game of Simple Craps"); // Create gambling account for user specified amount System.out.print("How much do you have to gamble? "); account = keyboard.nextDouble(); // Determine if user is ready to play System.out.print("Are you ready to play (Y or N)? "); userReady = keyboard.nextLine(); // Loop while the user wants to continue while (userReady.equalsIgnoreCase("Y")) { // Display totalDollars System.out.println("You have $" + account + " remaining"); // Determine how much the user wants to gamble System.out.print("How much would you like to bet? "); wager = keyboard.nextDouble(); if (wager <= account && account > 0) { // Roll the dice die1.roll(); die2.roll(); // Retrieve and display results System.out.println("You rolled a " + die1.getDiceValue() + " and a " + die2.getDiceValue() + " for a total of " + die1.add(die2)); if (die1.add(die2) == 7 || die1.add(die2) == 11) { account += wager; System.out.println("You win $" + wager); System.out.println(); } else if (die1.add(die2) == 2 || die1.add(die2) == 3 || die1.add(die2) == 12) { account -= wager; System.out.println("You lose $" + wager); System.out.println(); } else System.out.println("Draw"); } //End if loop // Ask user if they want to try again System.out.println(); System.out.print("Try again (Y or N)? "); userReady = keyboard.nextLine(); }//End while loop }//End main }//End class
And the problem is that the "Are you ready to play?" question does not seem to work
Here is sample output with the above code:
----jGRASP exec: java Driver
Welcome to the game of Simple Craps
How much do you have to gamble? 123
Are you ready to play (Y or N)?
----jGRASP: operation complete.
The very weird thing (at least to me), is that if I initialize account to 9.0 and remove the lines:// Create gambling account for user specified amount System.out.print("How much do you have to gamble? "); account = keyboard.nextDouble();
The program runs and outputs the following:
Both are issues with the validation not working, but I'm at a loss as to why. Any ideas? Big thanks in advance, and let me know if you need any more information.Welcome to the game of Simple Craps
Are you ready to play (Y or N)? y
You have $9.0 remaining
How much would you like to bet? 1
You rolled a 1 and a 1 for a total of 2
You lose $1.0
Try again (Y or N)?
----jGRASP: operation complete.