title: Finding probability using nested for loops in Java
body: <p>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. </p>
  
  <p><strong>* NOTE *</strong></p>
  
  <p>I can't use arrays in this assignment because it is not covered until the next section.</p>
  
  <p>Here is my assignment <a href="https://access.desire2learn.com/content/enforced/33909-5312_OFF_13_017.001/educator_apcsa_v9/module05/rtfmod05/5.05_AssignmentInstructionsDiceProbability.pdf?_&amp;d2lSessionVal=gtu91phg7xjofH089eMdDdTc6" rel="nofollow">here</a>. 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.</p>
  
  <p>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:</p>
  
  <p>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)</p>
  
  <p>and so on.</p>
  
  <p>Here is my code so far:</p>
  
  <pre><code>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 &lt; 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
  </code></pre>