I am making a simple program for class where the user starts with a 100 dollars and they make bets, and they either lose or win that amount based on whether they roll the same number on two dice. All of my code works and meets the requirements, but I can’t seem to handle invalid input for the bets. For example, if a word is entered, it still gives me the Input Mismatch Exception error. I have tried to do a try and catch, as well as the hasNextDouble as shown in my current code. Please help me figure this out!
<code>import java.util.Scanner;
public class DoubleDice {
public static void main(String[] args) {
double playerMoney;
double playerBet;
boolean word = false;
Scanner scnr = new Scanner(System.in);
//player is given $100 to start
playerMoney = 100.00;
Die die1 = new Die();
Die die2 = new Die();
while (!word) {
//program loops until player is out of money
while ((int)playerMoney > 0) {
//player is told their current amount and asked to bet
System.out.print("You have $");
System.out.printf("%.2f", playerMoney);
System.out.print("nHow much would you like to bet (Enter 0 to quit)? ");
//this is where the issue is
if (scnr.hasNextDouble()) {
playerBet = scnr.nextDouble();
word = true;
} else {
System.out.println("Invalid input. Please enter a valid number.");
scnr.nextLine(); // Consume the invalid input
continue; // Go back to the beginning of the loop
}
//quits the game if player enters 0
if ((int)playerBet == 0) {
System.out.print("See you around, winner!");
break;
}
//prompts for a valid bet if negative bet is placed
else if ((int)playerBet < 0) {
System.out.println("Please enter a positive amount to bet!");
System.out.println("n");
continue;
}
//prompts for a valid bet if player bets more money than they have
else if (playerBet > playerMoney) {
System.out.println("Please enter a bet that is less than or equal to your current amount!");
System.out.println("n");
continue;
}
else {
//both dice are rolled and printed
die1.roll();
die2.roll();
System.out.println("You rolled a " + die1.toString() + " and " + die2.toString());
//checks whether dice are equal and prints amount won or lost
if (die1.equals(die2)) {
System.out.print("You win $");
System.out.printf("%.2f", playerBet);
playerMoney += playerBet;
System.out.println("n");
}
else {
System.out.print("You lose $");
System.out.printf("%.2f", playerBet);
playerMoney -= playerBet;
System.out.println("n");
}
}
}
}
//losing message printed if player runs out of money
if ((int)playerMoney <= 0) {
System.out.println("You are out of money!");
System.out.println("Better luck next time");
}
}
}
</code>
<code>import java.util.Scanner;
public class DoubleDice {
public static void main(String[] args) {
double playerMoney;
double playerBet;
boolean word = false;
Scanner scnr = new Scanner(System.in);
//player is given $100 to start
playerMoney = 100.00;
Die die1 = new Die();
Die die2 = new Die();
while (!word) {
//program loops until player is out of money
while ((int)playerMoney > 0) {
//player is told their current amount and asked to bet
System.out.print("You have $");
System.out.printf("%.2f", playerMoney);
System.out.print("nHow much would you like to bet (Enter 0 to quit)? ");
//this is where the issue is
if (scnr.hasNextDouble()) {
playerBet = scnr.nextDouble();
word = true;
} else {
System.out.println("Invalid input. Please enter a valid number.");
scnr.nextLine(); // Consume the invalid input
continue; // Go back to the beginning of the loop
}
//quits the game if player enters 0
if ((int)playerBet == 0) {
System.out.print("See you around, winner!");
break;
}
//prompts for a valid bet if negative bet is placed
else if ((int)playerBet < 0) {
System.out.println("Please enter a positive amount to bet!");
System.out.println("n");
continue;
}
//prompts for a valid bet if player bets more money than they have
else if (playerBet > playerMoney) {
System.out.println("Please enter a bet that is less than or equal to your current amount!");
System.out.println("n");
continue;
}
else {
//both dice are rolled and printed
die1.roll();
die2.roll();
System.out.println("You rolled a " + die1.toString() + " and " + die2.toString());
//checks whether dice are equal and prints amount won or lost
if (die1.equals(die2)) {
System.out.print("You win $");
System.out.printf("%.2f", playerBet);
playerMoney += playerBet;
System.out.println("n");
}
else {
System.out.print("You lose $");
System.out.printf("%.2f", playerBet);
playerMoney -= playerBet;
System.out.println("n");
}
}
}
}
//losing message printed if player runs out of money
if ((int)playerMoney <= 0) {
System.out.println("You are out of money!");
System.out.println("Better luck next time");
}
}
}
</code>
import java.util.Scanner;
public class DoubleDice {
public static void main(String[] args) {
double playerMoney;
double playerBet;
boolean word = false;
Scanner scnr = new Scanner(System.in);
//player is given $100 to start
playerMoney = 100.00;
Die die1 = new Die();
Die die2 = new Die();
while (!word) {
//program loops until player is out of money
while ((int)playerMoney > 0) {
//player is told their current amount and asked to bet
System.out.print("You have $");
System.out.printf("%.2f", playerMoney);
System.out.print("nHow much would you like to bet (Enter 0 to quit)? ");
//this is where the issue is
if (scnr.hasNextDouble()) {
playerBet = scnr.nextDouble();
word = true;
} else {
System.out.println("Invalid input. Please enter a valid number.");
scnr.nextLine(); // Consume the invalid input
continue; // Go back to the beginning of the loop
}
//quits the game if player enters 0
if ((int)playerBet == 0) {
System.out.print("See you around, winner!");
break;
}
//prompts for a valid bet if negative bet is placed
else if ((int)playerBet < 0) {
System.out.println("Please enter a positive amount to bet!");
System.out.println("n");
continue;
}
//prompts for a valid bet if player bets more money than they have
else if (playerBet > playerMoney) {
System.out.println("Please enter a bet that is less than or equal to your current amount!");
System.out.println("n");
continue;
}
else {
//both dice are rolled and printed
die1.roll();
die2.roll();
System.out.println("You rolled a " + die1.toString() + " and " + die2.toString());
//checks whether dice are equal and prints amount won or lost
if (die1.equals(die2)) {
System.out.print("You win $");
System.out.printf("%.2f", playerBet);
playerMoney += playerBet;
System.out.println("n");
}
else {
System.out.print("You lose $");
System.out.printf("%.2f", playerBet);
playerMoney -= playerBet;
System.out.println("n");
}
}
}
}
//losing message printed if player runs out of money
if ((int)playerMoney <= 0) {
System.out.println("You are out of money!");
System.out.println("Better luck next time");
}
}
}
4