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!

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

Member Avatar for coil

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.

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
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.