Search code examples
javawhile-loopdice

Java Dice game issue


The assignment is to allow a user to input a sum of money as a bet (This number must be greater than 0 bet>=1).

1) Two dice (1-6) are rolled.

2) Their sum is calculated. If this sum is equal to 7, the user wins 4$ and if they lose, the user loses 1$ (Ex: User inputs 5$ as initial bet, wins = 5+4=9$ now–loses = 5-1 =4$ now).

After the user plays, we need to ask them if they would like to play again, and essentially run the program again using a while statement.

I commented out the to print statements in lines 43 and 50 because what would happen is that these two lines would print before the initial question of how much the user would like to enter (Which is why I commented them out).

Now the problem is that after the user inputs the amount he/she would like to bet, the system then calculates the amount they win/lose and prints this. However, it skips over the print statement asking them if they would like to play again. Why is this? I know something is wrong since initially the program was skipping over the first print statement, and going straight to second and third function. Any help?

Thanks!

//Edit: Also if it was not already obvious, I am quite new to program, so please feel free to critique me in any way ^^

  //Dice Game
//10/11/14
import java.util.Random;
import java.util.Scanner;

public class diceGame2
{
    //Instance Variables
    private String play = "yes";
    private int cash;
    private int sum;
    private int winnings;
    Scanner scan = new Scanner(System.in);
    Random rand = new Random();


    //Constructor
    public diceGame2()
    {
        cash=0;
        sum=0;
        winnings=0;
    }
    public void game()
    {
    do
    {
        System.out.println("Please enter the amount of money you would like to bet");
        cash=scan.nextInt();
        if(cash<1)
        {
            System.out.println("Please enter a bet greater than 1$.");
            cash=scan.nextInt();
        }
    }while((play.equals("yes")&&cash<0));
    }
    public int getDice()
    {

        int dice = rand.nextInt(6) + 1;
        System.out.println("The number rolled was a" + " " + dice);
        System.out.println();
        return dice;
    }
    public int getSum(int d1, int d2)
    {
        sum = d1+d2;
        System.out.println("Their sum is" + " " + sum + " " + "so...");
        System.out.println();
        return sum;
    }
    public int getWinnings(int sum)
    {
    if(sum==7)
    {
        cash += (4);
        System.out.println("You have won" + " " + "4$.");
    }
    else
    {
        cash -= (1);
        System.out.println("You have lost" + " " + "1$.");
    }
    System.out.println("You current have" + " " + cash  + " " + "dollars left to bet");
    return winnings;    
    }
    public String getPlay()
    {
        System.out.println("Do you want to play again");
        play = scan.next();
        return play;
    }

}//Class




//Dice Game Runner
//10/11/14
public class diceGameRunner
{
    public static void main(String[]args)
    {
    String play = "yes";
    while((play.equals("yes")))
    {
    diceGame2 d1 = new diceGame2();
    int dice1, dice2, sum;
    double money;

    d1.game();
    dice1 = d1.getDice();
    dice2 = d1.getDice();
    sum = d1.getSum(dice1, dice2);
    money = d1.getWinnings(sum);
    play = d1.getPlay();
    }


    }

}


Solution

  • There are a couple of problems with your code. First: You have said that without the break statement your while loop never breaks. This is because the condition will always be true. You never change the condition in the loop always forcing it to be true.

    Second: You have "cash =- (-1);" There is an extra - sign in there.

    Lastly (More of a side note): You have the loop in your object. This will make it difficult to call any of your methods and make clear code. Try Moving the while loop outside your object and make the object's methods only perform ONE task.

    EDIT: Here is sample code of what you should do. It was done in ~ 10 minutes and should be modified to what you want. It is an example on how you should set up the class and do the loop.

    The main method:

    import java.util.Scanner;
    
    /**
     *
     * @author Tyler Weaver
     */
    public class Test {
        public static void main(String[] args) {
            String play = "yes";
            int cash;
            Scanner scan = new Scanner(System.in);
    
            do {
                System.out.printf("Enter cash amount: ");
                cash = scan.nextInt();
            } while (cash < 1);
    
            DiceGame game = new DiceGame(cash);
    
            while (play.equalsIgnoreCase("yes") && cash > 0) {
                int dice1 = game.roll();
                int dice2 = game.roll();
    
                game.calcCash(dice1 + dice2);
                cash = game.getCash();
                System.out.printf("Your new cash amount is $%,.2f%n", (float) cash);
                System.out.printf("Do you want to play again? ");
    
                play = scan.next();
            }
        }
    }
    

    This is an example of what the class should look like minus other necessary methods:

    import java.util.Random;
    
    /**
     *
     * @author Tyler Weaver
     */
    public class DiceGame {
        private int cash;
        private Random gen;
    
        public DiceGame(final int cash) {
            this.cash = cash;
            gen = new Random();
        }
    
        public int roll() {
            return gen.nextInt(6) + 1;
        }
    
        public void calcCash(final int sum) {
            cash += (sum == 7) ? 4 : -1;
        }
    
        public int getCash() {
            return cash;
        }
    }
    

    This is not the answer. This is example code from your problem. You will have to figure out how to implement exactly what you need.

    Note how the class only performs one operation per method. This is just coding convention. Also notice how i ask for cash outside the loop in the main method and calculate inside. I also ask if they want to play again at the end of the method to update the conditional 'play'. Cash is updated through methods in the class.