0

Hi i've been set this assignment to design a Craps game for Java, i know this has been done lots before but i seem to be going round and round with this code....very frustrating!

This is the code i've written so far:

public class CrapsGame {

    public static void main(String[] args) {

        int die1 = (int) (Math.random()*6 + 1);
        int die2 = (int) (Math.random()*6 + 1);
        int dice = die1 + die2;

        System.out.println("You rolled " + dice);

        if (dice == 7 || dice == 11){
            System.out.println("We have a winner");
        }
        else if (dice == 2 || dice == 3 || dice == 12){
            System.out.println("CRAPS!");
        }

        else {

            System.out.println("Marker is "+dice);

            int die3 = (int) (Math.random()*6 + 1);
            int die4 = (int) (Math.random()*6 + 1);
            int dice2 = die3 + die4;

            while(dice2 != dice || dice2 != 7){
                System.out.println("You rolled "+dice2);
            }

        } //Else
        
    } //Main

} //Class

I'm very aware this isnt the finished article at all but any help would be greatly appreciated!

3
Contributors
3
Replies
4
Views
6 Years
Discussion Span
Last Post by benhowdle89
0

Whats the problem? Do you have errors? Which part do you need help with? Logic or language?

0

In lines 26 and 27, if the player doesn't win, you don't re-roll the dice, so it's an infinite loop. Also, even if the player does win, the program just abruptly terminates and doesn't tell the player that s/he won.

0

This is the way i got it working in case anyone is interested!

package crapsgame;

public class CrapsGame {
    static int dice2;

    public static void main(String[] args) {

        int die1 = (int) (Math.random()*6 + 1);
        int die2 = (int) (Math.random()*6 + 1);
        int dice = die1 + die2;

        System.out.println("You rolled " + dice);

        if (dice == 7 || dice == 11){
            System.out.println("We have a winner");
        }
        else if (dice == 2 || dice == 3 || dice == 12){
            System.out.println("CRAPS!");
        }

        else {

            System.out.println("Marker is "+dice);

            while (dice != 0){
                int die3 = (int) (Math.random()*6 + 1);
                int die4 = (int) (Math.random()*6 + 1);
                int dice2 = die3 + die4;
                System.out.println("You rolled "+dice2);
                    if (dice2 == dice){
                        System.out.println("We have a winner, you matched your marker");
                        dice = 0;
                    }
                    else if (dice2 == 7){
                        System.out.println("We have a loser, you got a 7");
                        dice = 0;
                    }
            }

     
        } //Else
        
    } //Main

} //Class
This question has already been answered. 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.