Question: I have identified a bug that causes the program to terminate unexpectedly when I ' m done with the game. When I click no to

I have identified a bug that causes the program to terminate unexpectedly when I'm done with the game. When I click no to not play again or when I click yes when out of bankroll money, the game crashes. Can you debug this code below and fix it?
import java.util.Scanner;
import java.util.Random;
public class PlayingCard {
private int face;
private String suit;
private static final String[] SUITS ={"Hearts", "Diamonds", "Clubs", "Spades"};
private static final int[] FACES ={2,3,4,5,6,7,8,9,10,11,12,13,14};
public PlayingCard(){
Random rand = new Random();
this.face = FACES[rand.nextInt(FACES.length)];
this.suit = SUITS[rand.nextInt(SUITS.length)];
}
public int getFace(){
return this.face;
}
public boolean isEquals(PlayingCard other){
return this.face == other.face;
}
@Override
public String toString(){
return this.face +" of "+ this.suit;
}
public static void main(String[] args){
Scanner input = new Scanner(System.in);
int totalGames =0;
int totalWins =0;
int totalLosses =0;
int totalTies =0;
double bankroll;
do {
System.out.print("Enter your bankroll: $");
while (!input.hasNextDouble()||(bankroll = input.nextDouble())=0){
System.out.println("Invalid input. Please enter a positive real number.");
input.nextLine();
}
double initialBankroll = bankroll;
int gamesPlayed =0;
int wins =0;
int losses =0;
int ties =0;
while (bankroll >0){
gamesPlayed++;
System.out.print("Enter your bet (between $0.01 and $100.00): $");
double bet;
while (!input.hasNextDouble()||(bet = input.nextDouble())0.01|| bet >100){
System.out.println("Invalid bet amount. Please enter a value between $0.01 and $100.00.");
input.nextLine();
}
if (bet > bankroll){
System.out.println("Insufficient funds. You have $"+ bankroll +" left in your bankroll.");
continue;
}
PlayingCard cardOne = new PlayingCard();
PlayingCard cardTwo = new PlayingCard();
System.out.println("Card drawn: "+ cardOne);
System.out.print("Higher or lower? (h/l): ");
char guess = Character.toLowerCase(input.next().charAt(0));
if ((guess =='h' && cardTwo.getFace()> cardOne.getFace())||
(guess =='l' && cardTwo.getFace() cardOne.getFace())){
bankroll += bet;
System.out.println("Correct! You won $"+ bet);
wins++;
} else if (cardTwo.getFace()== cardOne.getFace()){
System.out.println("It's a tie!");
ties++;
} else {
bankroll -= bet;
System.out.println("Incorrect! You lost $"+ bet);
losses++;
}
System.out.println("Your bankroll: $"+ bankroll);
System.out.print("Do you want to play again? (y/n): ");
input.nextLine();
String playAgain = input.nextLine();
if (!playAgain.equalsIgnoreCase("y")){
break;
}
}
System.out.println("
*** Game Summary ***");
System.out.println("Total games played: "+ gamesPlayed);
System.out.println("Total wins: "+ wins);
System.out.println("Total losses: "+ losses);
System.out.println("Total ties: "+ ties);
System.out.println("Ending amount in bankroll: $"+ bankroll);
System.out.println("Initial bankroll: $"+ initialBankroll);
totalGames += gamesPlayed;
totalWins += wins;
totalLosses += losses;
totalTies += ties;
System.out.println("
*** Overall Summary ***");
System.out.println("Total games played: "+ totalGames);
System.out.println("Total wins: "+ totalWins);
System.out.println("Total losses: "+ totalLosses);
System.out.println("Total ties: "+ totalTies);
input.close();
} while (true);
}
}
 I have identified a bug that causes the program to terminate

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!