Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8699869
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T02:03:04+00:00 2026-06-13T02:03:04+00:00

My problem is that whenever I try to compile and run my program, it

  • 0

My problem is that whenever I try to compile and run my program, it says one of my arithmetic problems closer to the end of my code is dividing by zero. Now there is another problem. Whenever the user is prompted to enter the number of rolls, you can input a number and hit enter, but it just skips to the next line and nothing happens. Nothing else in the code happens.

* NOTE *

I can’t use arrays in this assignment because it is not covered until the next section.

Here is my assignment here. This is what i’m supposed to be doing. I can’t figure out what is going wrong here. My math seems to be correct but something is going wrong.

In short my assignment wants me to find the probability of two 11 sided dice being “rolled” the amount of times the user inputs. For example:

If the user says the dice it to be rolled 100 times it would output it something like this
2s: (insert Probability of having the sum of the 2 dice being 2 after 100 rolls)
3s: (insert Probability of having the sum of the 2 dice being 3 after 100 rolls)
4s: (insert Probability of having the sum of the 2 dice being 4 after 100 rolls)
5s: (insert Probability of having the sum of the 2 dice being 5 after 100 rolls)

and so on.

Here is my code so far:

public static void main(String[] args)

{

    //Declare and initialize variables and objects

    Scanner in = new Scanner(System.in);

    Random randNum = new Random();




    int match = 0; //Number of times sum of dice matches the current sum

    int die1 = 0; //Random generated numbers
    int die2 = 0;
    int diceTotal2 = 0;
    int diceTotal3 = 0;
    int diceTotal4 = 0;
    int diceTotal5 = 0;
    int diceTotal6 = 0;
    int diceTotal7 = 0;
    int diceTotal8 = 0;
    int diceTotal9 = 0;
    int diceTotal10 = 0;
    int diceTotal11 = 0;
    int diceTotal12 = 0;

    int sumOfDice = 0;
    double probability2 = 0.0;
    double probability3 = 0.0;
    double probability4 = 0.0;
    double probability5 = 0.0;
    double probability6 = 0.0;
    double probability7 = 0.0;
    double probability8 = 0.0;
    double probability9 = 0.0;
    double probability10 = 0.0;
    double probability11 = 0.0;
    double probability12 = 0.0;


    //Input: ask user for number of rolls and number of sides on a die

    System.out.println("Number of Rolls: ");

    int rolls = in.nextInt();

    //***************************************************************************************

    //Using nested loops, cycle through the possible sums of the dice.

    //Roll the dice the given number of times for each sum.

    //Count how many times the sum of the dice match the current sum being looked for.

    //***************************************************************************************


    //Loop to increment through the possible sums of the dice

        //Loop to throw dice given number of times

        for( int numberOfRolls = 1; numberOfRolls < rolls; numberOfRolls++)

        {
              die1 = randNum.nextInt(6);
              die2 = randNum.nextInt(6);
              sumOfDice = die1 + die2;
              for( ; ; )
              {

            //Check if the sum of dice is equal to the given sum
                if(sumOfDice == 2)

                {
                    diceTotal2++;
                    probability2 = diceTotal2 / numberOfRolls;
                }

                else if(sumOfDice ==3)
                {
                    diceTotal3++;
                    probability3 = diceTotal3 / numberOfRolls;
                }

                else if(sumOfDice ==4)
                {
                    diceTotal4++;
                    probability4 = diceTotal4 / numberOfRolls;
                }

                else if(sumOfDice ==5)
                {
                    diceTotal5++;
                    probability5 = diceTotal5 / numberOfRolls;
                }

                else if(sumOfDice ==6)
                {
                    diceTotal6++;
                    probability6 = diceTotal6 / numberOfRolls;
                }

                else if(sumOfDice ==7)
                {
                    diceTotal7++;
                    probability7 = diceTotal7 / numberOfRolls;
                }

                else if(sumOfDice ==8)
                {
                    diceTotal8++;
                    probability8 = diceTotal8 / numberOfRolls;
                }

                else if(sumOfDice ==9)
                {
                    diceTotal9++;
                    probability9 = diceTotal9 / numberOfRolls;
                }

                else if(sumOfDice ==10)
                {
                    diceTotal10++;
                    probability10 = diceTotal10 / numberOfRolls;
                }

                else if(sumOfDice ==11)
                {
                    diceTotal11++;
                    probability11 = diceTotal11 / numberOfRolls;
                }

                else if(sumOfDice ==12)
                {
                    diceTotal12++;
                    probability12 = diceTotal12 / numberOfRolls;
                }

            }                

        }



       System.out.println("Sum of Dice" + "         " + "Probability");
       System.out.println("2s: \t\t" + probability2 + "%");
       System.out.println("3s: \t\t" + probability3 + "%");
       System.out.println("4s: \t\t" + probability4 + "%");
       System.out.println("5s: \t\t" + probability5 + "%");
       System.out.println("6s: \t\t" + probability6 + "%");
       System.out.println("7s: \t\t" + probability7 + "%");
       System.out.println("8s: \t\t" + probability8 + "%");
       System.out.println("9s: \t\t" + probability9 + "%");
       System.out.println("10s: \t\t" + probability10 + "%");
       System.out.println("11s: \t\t" + probability11 + "%");
       System.out.println("12s: \t\t" + probability12 + "%");


        //After all throws, calculate percentage of throws that resulted in the given sum



} //end main
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T02:03:05+00:00Added an answer on June 13, 2026 at 2:03 am

    As you already have a solution, I would present you with another one:

    import java.util.Random;
    import java.util.Scanner;
    
    public class Main {
        public static void main(String[] args) {
            int nRolls = 100, nDice = 6; // default values
    
            Scanner in = new Scanner(System.in);
            System.out.print("Number of throws: ");
            nRolls = in.nextInt();
            System.out.print("Number of sides on the dices: ");
            nDice = in.nextInt();
    
            int minSum = 2, maxSum = 2 * nDice;
            int[] hist = new int[maxSum - minSum + 1];
    
            Random rand = new Random();
            for (int iter = 1; iter <= nRolls; iter++) {
                int throw1 = 1 + rand.nextInt(nDice), throw2 = 1 + rand.nextInt(nDice);
                int sum = throw1 + throw2;
                hist[sum - minSum]++;
            }
    
            System.out.println("Number of rolls: " + nRolls);
            System.out.println("Number of sides of the dice: " + nDice);
            System.out.println("Sum of Dice         Percentage");
            for (int i = 0; i < hist.length; i++) {
                System.out.println(String.format("   %2d                 %5.2f%%", i + minSum, hist[i] * 100.0 / nRolls));
                // System.out.println("   " + (i+minSum) + "             " + (hist[i]*100.0/nRolls);
            }
    
            in.close();
        }
    }
    

    It shows you how to use arrays to solve this task. Each entry in the array holds the number of throws that came up with the corresponding sum. You always have 2*nDice - 1 possible sums (you can not reach 1 with two dices), so the size of the array is dependent on the number of sides on the dice.

    Then you just iterate through all throws and add 1 to the corresponding histogram entry (note that I offset the histogram, so hist[0] corresponds to a sum of 2, hist[1] to a sum of 3, etc).

    At the end, you can just calculate the percentage. (It’s not a probability, it’s the percentage that this sum occured in our simulation. If you make the number of rolls larger, this percentage will be an approximation of the probability.)

    At the end, you just print it out. The String.format stuff is just for alignment of the values. If you are confused about it, just use

    System.out.println("   " + (i+minSum) + "             " + (hist[i]*100.0/nRolls);
    

    instead.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My problem is that in WPF, whenever I try and change the colour of
The problem I have is that whenever I try to drag and drop with
I've been working on an Entity, oneToMany relationship, the problem is that whenever I
problem is , that whenever the grid's row is right clicked the selected item
I'm running into a problem that I can't seem to get past. Whenever I
Im currently facing the problem that when i try to set focus on some
I am facing problem that whenever I turn off security settings in IE8, in
The problem is that whenever I input a big-ass text in the form of
I'm creating a presentation using deck.js, the problem that I'm having is that whenever
Simple problem that I can't figure out... How can I print a '%' character

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.