0

This is my code I am suppose to be stimulating the game of craps: I am getting an error

Exception in thread "main" java.lang.ArithmeticException: / by zero at Dice.main(Dice.java:71)

please help the instructions are: In the game of craps, a pass line bet proceeds as follows. Two six-sided dice are rolled; the first roll of the dice in a craps round is called the “come out roll.” A come out roll of 7 or 11 automatically wins, and a come out roll of 2, 3, or 12 automatically loses. If 4, 5, 6, 8, 9, or 10 is rolled on the come out roll, that number becomes the point. The player keeps rolling the dice until either 7 or the point is rolled. If the point is rolled first, then the player wins the bet. If a 7 is rolled first, then the player loses. Write a program that simulates a game of craps using these rules without human input. Instead of asking for a wager, the program should calculate whether the player would win or lose.

The program should simulate rolling the two dice and calculate the sum. Add a loop so that the program plays 10,000 games.Add counters that count how many times the player wins, and how many times the player loses. At the end of the 10,000 games, compute the probability of winning [i.e., Wins / (Wins + Losses)] and output this value. Over the long run, who is going to win the most games, you or the house? Note: To generate a random number x, where 0 x ≤< 1, use x = Math.random(); . For example, multiplying by 6 and converting to an integer results in an integer that is between 0 and 5.

public class Dice 
{
    public static void main(String[]args)
    {
        //declaring variables
        int comeOutRoll1, comeOutRoll2;
        int roll1, roll2;
        int numW, numL;
        int sum, sum2 = 0;
        int thePoint = 0;
        double probability; 


        //initializing variables
        comeOutRoll1 = (int)(Math.random()*6) +1;
        comeOutRoll2 = (int)(Math.random()*6) +1;
        sum = comeOutRoll1 + comeOutRoll2;
        numW = 0;
        numL = 0;


        for(int timesPlayed = 0; timesPlayed <= 10000; timesPlayed++)
        {

            switch(sum)
            {
            //adds how many wins and losses
            case 2:
            case 3:
            case 12:
                numL = numL + 1;
            break;
            case 7:
            case 11:
                numW = numW +1;
            break;
            case 4:
                thePoint = sum;
            case 5:
                thePoint = sum;
            case 6:
                thePoint = sum;
            case 8:
                thePoint = sum;
            case 9:
                thePoint = sum;
            case 10:
                thePoint = sum;
            break;
            //if not any of these cases roll again
            default:

                roll1 = (int)(Math.random()*6) +1;
                roll2 = (int)(Math.random()*6) +1;
                sum2 = roll1 + roll2;
                break;
            }

            if(sum2 == thePoint)
            {
                do
                numW = numW +1;
            }
            else if(sum2 == 7)
            {
                numL = numL +1;
            }
        }


            probability = (numW/(numW+numL));

            System.out.println("Number of Wins: " + numW);
            System.out.println("Number of Losses: " + numL);
            System.out.println("The probability of winning is: " + probability + " percent");   


    }

}
Olivia
  • 11
  • 1
  • 6
  • you never change `sum` in your loop, so you never increment`numL` and `numW`. Also you miss `break` in a lot of cases. – Sergey Grinev Nov 12 '16 at 20:57
  • check line 71, you divide by zero there – P.J.Meisch Nov 12 '16 at 20:58
  • The value of `comeOutRoll1` is 1-6, right? And the value of `comeOutRoll2` is also 1-6, right? So the value of `sum` is 2-12? So the `default` block is never executed and `sum2` is always 0. And if `sum` is 4, 5, 6, 8, 9, or 10, then `thePoint = sum`, so `numW` and `numL` will stay 0, and `numW/(numW+numL)` will then cause "/ by zero" exception. – Andreas Nov 12 '16 at 21:01
  • 1
    Welcome to Stack Overflow! It looks like you need to learn to use a debugger. Please help yourself to some [complementary debugging techniques](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). If you still have issues afterwards, please feel free to come back with more details. – Joe C Nov 12 '16 at 21:32
  • How would you suggest I do that? – Olivia Nov 12 '16 at 22:05
  • 1
    Possible duplicate of [A simulation the game of craps java](http://stackoverflow.com/questions/40568421/a-simulation-the-game-of-craps-java) – janos Nov 14 '16 at 12:08

0 Answers0