0

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);  
}
}
2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by emilo35
1

Ouch my eyes hurt just by looking at your code. Please indent it and repost! Well, by going through your code once, I managed to extract some things that might be causing your problems.

You are trying to use a do .. while ... loop to make your program loop. This means that it will loop for as long as the condition after while equals true. Assuming all your brackets are in the right places and all, what your do-while statement says is

do {
    // Lots of stuff here
} while (bankroll <= 0);

Take a look at that. You tell your program to repeat for as long as your bankroll has less than zero credits in it. That's not what you want, is it?

Cheers
Emil Olofsson

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.