I have two problems with my code. One being I can't get it to repeat the simple game until the user has no money in his or her bankroll, the game continues even when the bankroll equals 0. The other is I can't get my program to repeat asking the user how much he or she wishes to bet, even if his or her bankroll or bet is too low. Any suggestions?
import java.util.Random;
public class PlayingCard
{
private int face;
private int suit;
//-------------------------------------------------------------------
// Sets up the playing card by drawing it initially.
//-------------------------------------------------------------------
public PlayingCard ()
{
drawCard();
}
//-------------------------------------------------------------------
// Draws the playing card by randomly choosing face and suit values.
//-------------------------------------------------------------------
public void drawCard ()
{
face = (int) (Math.random() * 13) + 2;
suit = (int) (Math.random() * 4);
}
//-------------------------------------------------------------------
// Returns true if the current playing card is exactly the same as
// the card passed in as a parameter.
//-------------------------------------------------------------------
public boolean isEquals (PlayingCard c)
{
return (face == c.face && suit == c.suit);
}
//-------------------------------------------------------------------
// Returns the face value of the current playing card.
//-------------------------------------------------------------------
public int getFace()
{
return face;
}
//-------------------------------------------------------------------
// Returns the suit value of the current playing card.
//-------------------------------------------------------------------
public int getSuit()
{
return suit;
}
//-------------------------------------------------------------------
// Returns the current playing card as a string.
//-------------------------------------------------------------------
public String toString()
{
String cardName = null;
switch (face)
{
case 2: cardName = "Two";
break;
case 3: cardName = "Three";
break;
case 4: cardName = "Four";
break;
case 5: cardName = "Five";
break;
case 6: cardName = "Six";
break;
case 7: cardName = "Seven";
break;
case 8: cardName = "Eight";
break;
case 9: cardName = "Nine";
break;
case 10: cardName = "Ten";
break;
case 11: cardName = "Jack";
break;
case 12: cardName = "Queen";
break;
case 13: cardName = "King";
break;
case 14: cardName = "Ace";
}
switch (suit)
{
case 0: cardName += " of Clubs";
break;
case 1: cardName += " of Spades";
break;
case 2: cardName += " of Hearts";
break;
case 3: cardName += " of Diamonds";
break;
}
return cardName;
}
}
public class HiLoGame
{
public static void main (String[] args)
{
PlayingCard cardOne = new PlayingCard();
PlayingCard cardTwo = new PlayingCard();
double bankroll, bet;
int lostGames = 0, tiedGames = 0, totalGames = 0, wonGames = 0;
Scanner Scan = new Scanner (System.in);
System.out.println("\n\nThis program implements a Hi-Lo game using");
System.out.println("a standard deck of 52 playing cards. You are");
System.out.println("required to enter your bankroll as well as the");
System.out.println("amount of money you wish to bet.");
System.out.print("\nHow much is in your bank? ");
bankroll = Scan.nextDouble();
do
{
System.out.print("\nHow much would you like to bet? ");
bet = Scan.nextDouble();
if (bet >= 100)
{
System.out.print("\nSorry, your bet was too high.");
}
if (bet > bankroll)
{
System.out.print("\nYou don't have enough money.");
}
System.out.println("\nThe first card you drew is the " + cardOne + ".");
System.out.print("\nDo you think the second card will be higher or lower? ");
Scanner scan = new Scanner (System.in);
String guess = scan.nextLine();
guess = guess.toLowerCase();
System.out.println("\nThe second card you drew is the " + cardTwo + ".");
if (cardOne.getFace() < cardTwo.getFace() && guess.equals("higher"))
{
System.out.println("\nCongratulations! You win!");
bankroll = bankroll + bet;
wonGames++;
totalGames++;
System.out.println("\nThe amount left in your bank is " + bankroll + ".");
}
else
{
System.out.println("\nSorry, you lose.");
bankroll = bankroll - bet;
lostGames++;
totalGames++;
System.out.println("\nThe amount left in your bank is " + bankroll + ".");
}
System.out.print("\nWould you like to play again? ");
String choice = scan.nextLine();
choice = choice.toLowerCase();
if (choice.equals("yes"))
{
cardOne.drawCard();
cardTwo.drawCard();
}
else
{
System.out.println("\nYou played " + totalGames + " games total.");
System.out.println("You won " + wonGames + " times, lost " + lostGames + " times, and tied " + tiedGames + " times.");
System.out.println("The amount left in your bank is " + bankroll + ".");
break;
}
}
while (bankroll <= 0);
}
}