alright, i have been working on this code for two days. it is an assignment for my ap comp science class. i have tried several experiments that have all not worked. im supposed to make a proram that simulates an 11 sided dice being rolled, and it has to use nested loops. the user has to input a number of rolls and the program is supposed to continue that many times. also i have to display the probability of each side of the die being rolled. if anyone has any help it is GREATLY appreciated. this is the way i went about doing it:

/**
 * 
 * 
 * Nathan Gibson 
 * 
 */
import java.util.Scanner;
import java.util.Random;

public class DiceProbability
{
    public static void main(String [] args)
    {
        Scanner in;  
        in = new Scanner(System.in);
        
        int counterTrials = 1;
        int counter1 = 1;
        int counter2 = 1;
        int counter3 = 1;
        int counter4 = 1;
        int counter5 = 1;
        int counter6 = 1;
        int counter7 = 1;
        int counter8 = 1;
        int counter9 = 1;
        int counter10 = 1;
        int counter11 = 1;
        
        
        
        System.out.println("Number of Rolls: ");
        int numberOfRolls = in.nextInt();
        
        int randomNumber = ((int)(0+ Math.random()* 11));
        
        
        
         
        
        
        for(int side1 = 0; counterTrials <= numberOfRolls; numberOfRolls++) 
        {
            randomNumber = ((int)(0+ Math.random()* 11));
            if (randomNumber == 1)
                {
                    counter1++;
                    counterTrials++;
                }
            }
            
            
        
            for(int side2 = 0; counterTrials <= numberOfRolls; numberOfRolls++) 
            {
                randomNumber = ((int)(0+ Math.random()* 11));
                if (randomNumber == 2)
                {
                
                    counter2++;
                    counterTrials++;
                }
            }
            
            
                for(int side3 = 0; counterTrials <= numberOfRolls; numberOfRolls++)
                {
                    randomNumber = ((int)(0+ Math.random()* 11));
                    if (randomNumber == 3)
                    {
                        counter3++;
                        counterTrials++;
                    
                    }
                }
                
                
                
                
                        for(int side4= 0; counterTrials <= numberOfRolls; numberOfRolls++)
                        {
                        randomNumber = ((int)(0+ Math.random()* 11));
                            if (randomNumber == 4)
                            {
                                counter4++;
                                counterTrials++;
                            }
                        }
                        
                            
                        
                            for(int side5 = 0; counterTrials <= numberOfRolls; numberOfRolls++) 
                            {
                                randomNumber = ((int)(0+ Math.random()* 11));
                                if (randomNumber == 5)
                                {
                                    counter5++;
                                    counterTrials++;
                                }
                            }
                                
                            
                                for(int side6 = 0; counterTrials <= numberOfRolls; numberOfRolls++) 
                                {
                                    randomNumber = ((int)(0+ Math.random()* 11));
                                    if (randomNumber == 6)
                                    {
                                        counter6++;
                                        counterTrials++;
                                    }
                                }
                               
                                
                                    
                                
                                    for(int side7 = 0; counterTrials <= numberOfRolls; numberOfRolls++) 
                                    {
                                        randomNumber = ((int)(0+ Math.random()* 11));
                                        if (randomNumber == 7)
                                        {
                                            counter7++;
                                            counterTrials++;
                                        }
                                    }
                                    
                                        
                                    
                                        for(int side8= 0; counterTrials <= numberOfRolls; numberOfRolls++)
                                        {
                                            randomNumber = ((int)(0+ Math.random()* 11));
                                            if (randomNumber == 8)
                                            {
                                                counter8++;
                                                counterTrials++;
                                            }
                                        
                                        }   
                                        
                                            for(int side9 = 0; counterTrials <= numberOfRolls; numberOfRolls++) 
                                            {
                                                randomNumber = ((int)(0+ Math.random()* 11));
                                                if (randomNumber == 9)
                                                {
                                                    counter9++;
                                                    counterTrials++;
                                                }
                                            }
                                            
                                                
                                            
                                                for(int side10 = 0; counterTrials <= numberOfRolls; numberOfRolls++) 
                                                {
                                                    randomNumber = ((int)(0+ Math.random()* 11));
                                                    if (randomNumber == 10)
                                                    {
                                                        counter10++;
                                                        counterTrials++;
                                                    }
                                                }
                                                    
                                                
                                                    for(int side11= 0; counterTrials <= numberOfRolls; numberOfRolls++)
                                                    {
                                                        randomNumber = ((int)(0+ Math.random()* 11));
                                                        if (randomNumber == 11)
                                                        {
                                                            counter11++;
                                                            counterTrials++;
                                                        }
                                                    }
                                                        
                                                    
                                                    
                                                
        
        System.out.print("Sum Of Dice              Probability");
        System.out.print("\n1s " + "                      " + (double) (counter1/numberOfRolls * 100) + "%");
        System.out.print("\n2s " + "                      " + (double) (counter2/numberOfRolls * 100) + "%");
        System.out.print("\n3s " + "                      " + (double) (counter3/numberOfRolls * 100) + "%");
        System.out.print("\n4s " + "                      " + (double) (counter4/numberOfRolls * 100) + "%");
        System.out.print("\n5s " + "                      " + (double) (counter5/numberOfRolls * 100) + "%");
        System.out.print("\n6s " + "                      " + (double) (counter6/numberOfRolls * 100) + "%");
        System.out.print("\n7s " + "                      " + (double) (counter7/numberOfRolls * 100) + "%");
        System.out.print("\n8s " + "                      " + (double) (counter8/numberOfRolls * 100) + "%");
        System.out.print("\n9s " + "                      " + (double) (counter9/numberOfRolls * 100) + "%");
        System.out.print("\n10s " + "                      " + (double) (counter10/numberOfRolls * 100) + "%");
        System.out.print("\n11s " + "                      " + (double) (counter11/numberOfRolls * 100) + "%");
        
    
        }
        
}

my problem was that i couldnt get the counters to work right therefore my probabilitys were wrong, so i changed that, but now when i run i am prompted to enter a number and then nothing happens.

Just to start I notice that in your for loops you are incrementing the numberOfRolls variable. Basically you are making it so that the for loops go on forever because counterTrials can never catch up to numberOfRolls . Also do you have sample output for this app. To what extent are you supposed to use nested loops. This looks like over kill.

for(int pos4 = 0; pos4 <= 9; pos4++) //loop in 1000s position
for(int pos3 = 0; pos3 <= 9; pos3++) //loop in 100s position
for(int pos2 = 0; pos2 <= 9; pos2++) //loop in 10s position

this was our example of a nested loop. im in highschool taking a distance learning ap computer science class. i only have an instructor via email, hence my trouble.

i attached the expected output. im going to try to fix the counter problem you pointed out.

Attachments assignment.jpg 34.88 KB

from your first reply, i think this may have been what you meant, did this fix the counter problem?

for(int side1 = 0; counterTrials <= numberOfRolls; counterTrials++) 
        {
            randomNumber = ((int)(0+ Math.random()* 11));
            if (randomNumber == 1)
                {
                    counter1++;
                    
                }
            }

from your first reply, i think this may have been what you meant, did this fix the counter problem?

for(int side1 = 0; counterTrials <= numberOfRolls; counterTrials++)

That's better. The whole program can be redesigned significantly. One, use arrays.

Two,

i have to display the probability of each side of the die being rolled

The probability of any given side of the die being face-up after a single roll is one divided by the number of sides. That's a known mathematical fact. Often, one simulates this using a computer program to see how close the results are to this mathematical fact, but from a terminology point of view, the probability is known ahead of time and unchanging. You can roll the dice x number of times, keep track of what comes up, and display that, which I assume is the assignment here, but if you display those results, you aren't displaying the probability. That's from a terminology point of view. That said, why you would use a nested loop for this is unclear to me. I am assuming you don't know how to or aren't allowed to use arrays. If you are, the code is extremely short. Regardless I have to believe you have way too many loops. Seems to me you only need one loop.

for (int i = 0; i < numRolls; i++)
{
   int roll = /* generate a random number from 1 to 11 */

   switch (roll)
   {
      case 1: /* increment appropriate variable */ break;
      case 2: /* increment appropriate variable */ break;
      case 3: /* increment appropriate variable */ break;
      // etc.
   }
}

Use arrays and you don't even need the switch statement. As to the example of the nested loop your professor provided, it may be useful in some other program, but I cannot understand what possible reason there would be to use it here. Maybe I'm way off on what you are expected to do.

everyone has been telling me to use arrays, but we havent went over them and the lesson is on nested loops. im all with you guys on the it doesnt make sense factor, thats why it is so hard for me to wrap my mind around it. i could be done easily with if else statements. i still cant get it to work. i have gotten it to output this:

Number of Rolls:
100
Sum Of Dice Probability
1s 0.0%
2s 0.0%
3s 0.0%
4s 0.0%
5s 0.0%
6s 0.0%
7s 0.0%
8s 0.0%
9s 0.0%
10s 0.0%
11s 0.0%

with this code:

/**
 * 
 * 
 * Nathan Gibson 
 * 
 */
import java.util.Scanner;
import java.util.Random;

public class DiceProbability
{
    public static void main(String [] args)
    {
        Scanner in;  
        in = new Scanner(System.in);
        
        int counterTrials = 1;
        int counter1 = 1;
        int counter2 = 1;
        int counter3 = 1;
        int counter4 = 1;
        int counter5 = 1;
        int counter6 = 1;
        int counter7 = 1;
        int counter8 = 1;
        int counter9 = 1;
        int counter10 = 1;
        int counter11 = 1;
        
        
        
        System.out.println("Number of Rolls: ");
        int numberOfRolls = in.nextInt();
        
        int randomNumber = ((int)(0+ Math.random()* 11));
        
        
        
         
        
        
        for(int side1 = 0; counterTrials <= numberOfRolls; counterTrials++) 
        {
            randomNumber = ((int)(0+ Math.random()* 11));
            if (randomNumber == 1)
                {
                    counter1++;
                    
                }
            }
            
            
        
            for(int side2 = 0; counterTrials <= numberOfRolls; counterTrials++) 
            {
                randomNumber = ((int)(0+ Math.random()* 11));
                if (randomNumber == 2)
                {
                
                    counter2++;
                    
                }
            }
            
            
                for(int side3 = 0; counterTrials <= numberOfRolls; counterTrials++)
                {
                    randomNumber = ((int)(0+ Math.random()* 11));
                    if (randomNumber == 3)
                    {
                        counter3++;
                        
                    
                    }
                }
                
                
                
                
                        for(int side4= 0; counterTrials <= numberOfRolls; counterTrials++)
                        {
                        randomNumber = ((int)(0+ Math.random()* 11));
                            if (randomNumber == 4)
                            {
                                counter4++;
                                
                            }
                        }
                        
                            
                        
                            for(int side5 = 0; counterTrials <= numberOfRolls; counterTrials++) 
                            {
                                randomNumber = ((int)(0+ Math.random()* 11));
                                if (randomNumber == 5)
                                {
                                    counter5++;
                                    
                                }
                            }
                                
                            
                                for(int side6 = 0; counterTrials <= numberOfRolls; counterTrials++) 
                                {
                                    randomNumber = ((int)(0+ Math.random()* 11));
                                    if (randomNumber == 6)
                                    {
                                        counter6++;
                                        
                                    }
                                }
                               
                                
                                    
                                
                                    for(int side7 = 0; counterTrials <= numberOfRolls; counterTrials++) 
                                    {
                                        randomNumber = ((int)(0+ Math.random()* 11));
                                        if (randomNumber == 7)
                                        {
                                            counter7++;
                                            
                                        }
                                    }
                                    
                                        
                                    
                                        for(int side8= 0; counterTrials <= numberOfRolls; counterTrials++)
                                        {
                                            randomNumber = ((int)(0+ Math.random()* 11));
                                            if (randomNumber == 8)
                                            {
                                                counter8++;
                                                
                                            }
                                        
                                        }   
                                        
                                            for(int side9 = 0; counterTrials <= numberOfRolls; counterTrials++) 
                                            {
                                                randomNumber = ((int)(0+ Math.random()* 11));
                                                if (randomNumber == 9)
                                                {
                                                    counter9++;
                                                    
                                                }
                                            }
                                            
                                                
                                            
                                                for(int side10 = 0; counterTrials <= numberOfRolls; counterTrials++) 
                                                {
                                                    randomNumber = ((int)(0+ Math.random()* 11));
                                                    if (randomNumber == 10)
                                                    {
                                                        counter10++;
                                                        
                                                    }
                                                }
                                                    
                                                
                                                    for(int side11= 0; counterTrials <= numberOfRolls; counterTrials++)
                                                    {
                                                        randomNumber = ((int)(0+ Math.random()* 11));
                                                        if (randomNumber == 11)
                                                        {
                                                            counter11++;
                                                            
                                                        }
                                                    }
                                                        
                                                    
                                                    
                                                
        
        System.out.print("Sum Of Dice              Probability");
        System.out.print("\n1s " + "                      " + (double) (counter1/numberOfRolls * 100) + "%");
        System.out.print("\n2s " + "                      " + (double) (counter2/numberOfRolls * 100) + "%");
        System.out.print("\n3s " + "                      " + (double) (counter3/numberOfRolls * 100) + "%");
        System.out.print("\n4s " + "                      " + (double) (counter4/numberOfRolls * 100) + "%");
        System.out.print("\n5s " + "                      " + (double) (counter5/numberOfRolls * 100) + "%");
        System.out.print("\n6s " + "                      " + (double) (counter6/numberOfRolls * 100) + "%");
        System.out.print("\n7s " + "                      " + (double) (counter7/numberOfRolls * 100) + "%");
        System.out.print("\n8s " + "                      " + (double) (counter8/numberOfRolls * 100) + "%");
        System.out.print("\n9s " + "                      " + (double) (counter9/numberOfRolls * 100) + "%");
        System.out.print("\n10s " + "                     " + (double) (counter10/numberOfRolls * 100) + "%");
        System.out.print("\n11s " + "                     " + (double) (counter11/numberOfRolls * 100) + "%");

but i still cant figure out how to get my probability to correctly calculate.

The example in my last post doesn't use arrays. You could turn a regular for-loop into a nested for-loop like this:

for (int i = 0; i < 10; i++)
{
  for (int j = 0; j < 10; j++)
  {
    // single roll code.  Will execute 100 times.  See last post.  You'll use neither i nor j in here.
  }
}

It's equivalent to this:

for (int i = 0; i < 100; i++)
{
    // single roll code.  Will execute 100 times.  See last post.  You won't use variable  i in here.
}

So write the program with a hard-coded number like 100 rolls first, get it to work the way you want, then tackle doing it from input from a scanner.

Edited 7 Years Ago by VernonDozier: n/a

i get what your saying. but the only thing is, thats not really my problem anymore. now everything outputs right except all my probabilities are at 0, and i dont know why.

That's better.

I take this quote from post 5 back (can't edit it - darn you, 30 minute limit!). This loop isn't going to work.

for(int side1 = 0; counterTrials <= numberOfRolls; counterTrials++)

You probably want this:

for(int counterTrials = 0; counterTrials < numberOfRolls; counterTrials++)

The same variable should be in all three parts of the for-loop. side1 was in the first, but not the middle or the end. Try changing it to these and see if you get better results.

I also see no nested loops in your code.

FYI: Indenting for loops does not make them nested. In order to be nested, one for loop must be inside another. If you look at VernonDozier's example you see a nested for loop. Here's another example with 3 for loops that are nested. Notice how one for loop is contained inside the other.

//for loop 1
for (int i = 0; i < 10; i++)
{
     //for loop 2
     for (int j=0; j < 10; j++)
     {
          //for loop 3
          for (int k=0; k < 10; k++)
          {

          }//end for loop 3
     }//end for loop 2
}//end for loop 1

To fix the problem of printing 0.0%, you need to change your print statements.

Original:

System.out.print("\n1s " + "                      " + (double) (counter1/numberOfRolls * 100) + "%");

Change to:

System.out.print("\n1s " + "                      " +  (double)counter1/(double)numberOfRolls * 100.0 + "%");

Notice how I cast each variable to double and change "100" to "100.0".
I don't think that it is a good idea to get into a habit of doing math inside print statements though. If you ever try to do addition you are going to run into problems, because "+" is also a String concatenation operator. Instead do the following:

double myValue;
myValue = (double)counter1/(double)numberOfRolls * 100.0;
System.out.print("\n1s " + "                      " +  myValue + "%");

Edited 7 Years Ago by cgeier: n/a

Another problem you have though, is that the only for loop that is ever executed is the first one because counterTrials = numberOfRolls after the first loop is finished.

Check it out:

int counterTrials = 1;

for(int side1 = 0; counterTrials <= numberOfRolls; counterTrials++)
{
     //it doesn't matter what we do in here because counterTrials will still be incremented
}

for(int side2 = 0; counterTrials <= numberOfRolls; counterTrials++)
{
     //it doesn't matter what we do in here because counterTrials will still be incremented
}

Also, "int side1 = 0" is supposed to be an initialization value for the loop. It doesn't really do anything after the first iteration though.

for(int side1 = 0; side1 <= numberOfRolls; side1++)
{
     //it doesn't matter what we do in here because counterTrials will still be incremented
}

In your code, it doesn't do anything except take up memory. You may as well write it as follows:

for(; counterTrials <= numberOfRolls; counterTrials++)
{
     //it doesn't matter what we do in here because counterTrials will still be incremented
}

So if we specify numberOfRolls = 1000, we get the following:
I'm going to substitute in the values for the variables,

For loop 1 (side1):
            counterTrials   numberOfRolls    increment counterTrials
Round 1:    for (1   <=    1000;                     1+1)
Round 2:    for (2   <=    1000;                     2+1)
Round 3:    for (3   <=    1000;                     3+1)

                                 ...
Round 999: for (999    <=    1000;            999+1)
Round 1000: for (1000  <=    1000;        1000+1)

After for loop 1 completes, counterTrials now = 1001, and we exit the for loop. So we continue onto the next piece of code. We find for loop 2 (side2):

For loop 2 (side2):
            counterTrials   numberOfRolls    increment counterTrials
Round 1:    for (1001   <=    1000;          counterTrials++)

counterTrials (which = 1001) > numberOfRolls (which = 1000), so the loop does not execute, and the counter is not incremented. So as you have your code now, for loops (side2 - side11) do not ever execute.

Edited 7 Years Ago by cgeier: n/a

wow. i took you guys advice and chopped some stuff out. i dont care, its just a grade, not worth all this. if any one is interested, here is the finished code:

/**
 * 
 * 
 * Nathan Gibson 
 * 
 */
import java.util.Scanner;
import java.util.Random;

public class DiceProbability
{
    public static void main(String [] args)
    {
        Scanner in;  
        in = new Scanner(System.in);
        
        
        int counter1 = 1;
        int counter2 = 1;
        int counter3 = 1;
        int counter4 = 1;
        int counter5 = 1;
        int counter6 = 1;
        int counter7 = 1;
        int counter8 = 1;
        int counter9 = 1;
        int counter10 = 1;
        int counter11 = 1;
        
        
        
        System.out.println("Number of Rolls: ");
        int numberOfRolls = in.nextInt();
        
        int randomNumber = ((int)(0+ Math.random()* 11));
        
        
          for(int counterTrials = 0; counterTrials < numberOfRolls; counterTrials++) 
         {
            randomNumber = ((int)(0+ Math.random()* 11));
            if (randomNumber == 1)
                {
                    counter1++;
                    
                }
            
            randomNumber = ((int)(0+ Math.random()* 11));
            if (randomNumber == 2)
                {
                    counter2++;
                    
                }    
            
            randomNumber = ((int)(0+ Math.random()* 11));
            if (randomNumber == 3)
                {
                    counter3++;
                    
                }        
            randomNumber = ((int)(0+ Math.random()* 11));
            if (randomNumber == 4)
                {
                    counter4++;
                    
                }    
            randomNumber = ((int)(0+ Math.random()* 11));
            if (randomNumber == 5)
                {
                    counter5++;
                    
                }    
                
            randomNumber = ((int)(0+ Math.random()* 11));
            if (randomNumber == 6)
                {
                    counter6++;
                    
                }    
            randomNumber = ((int)(0+ Math.random()* 11));
            if (randomNumber == 7)
                {
                    counter7++;
                    
                }    
            randomNumber = ((int)(0+ Math.random()* 11));
            if (randomNumber == 8)
                {
                    counter8++;
                    
                }    
           randomNumber = ((int)(0+ Math.random()* 11));
            if (randomNumber == 9)
                {
                    counter9++;
                    
                }    
            randomNumber = ((int)(0+ Math.random()* 11));
            if (randomNumber == 10)
                {
                    counter10++;
                    
                }        
                             
            randomNumber = ((int)(0+ Math.random()* 11));
            if (randomNumber == 11)
                {
                    counter11++;
                    
                }   
            }
                                                    
                                                        
                                                    
                                                    
                                                
        
        System.out.print("Sum Of Dice              Probability");
        System.out.print("\n1s " + "                      " +  (double)counter1/(double)numberOfRolls * 100.0 + "%");
        System.out.print("\n2s " + "                      " +  (double)counter2/(double)numberOfRolls * 100.0 + "%");
        System.out.print("\n3s " + "                      " +  (double)counter3/(double)numberOfRolls * 100.0 + "%");
        System.out.print("\n4s " + "                      " +  (double)counter4/(double)numberOfRolls * 100.0 + "%");
        System.out.print("\n5s " + "                      " +  (double)counter5/(double)numberOfRolls * 100.0 + "%");
        System.out.print("\n6s " + "                      " +  (double)counter6/(double)numberOfRolls * 100.0 + "%");
        System.out.print("\n7s " + "                      " +  (double)counter7/(double)numberOfRolls * 100.0 + "%");
        System.out.print("\n8s " + "                      " +  (double)counter8/(double)numberOfRolls * 100.0 + "%");
        System.out.print("\n9s " + "                      " +  (double)counter9/(double)numberOfRolls * 100.0 + "%");
        System.out.print("\n10s " + "                     " + (double)counter10/(double)numberOfRolls * 100.0 + "%");
        System.out.print("\n11s " + "                     " + (double)counter11/(double)numberOfRolls * 100.0 + "%");
        
        
        
        }
        
    }

this is truly the best forum i have been too. everyone who contributed to helping, thank you. you guys are pretty good.

You almost got it. You just need to get rid of a bunch of the following statements:

randomNumber = ((int)(0+ Math.random()* 11));

It really should only exist in one place. You can get rid of the rest of them.

for(int counterTrials = 0; counterTrials < numberOfRolls; counterTrials++)          
{            
     randomNumber = ((int)(0+ Math.random()* 11));

     if (randomNumber == 1)                
     {                    
          counter1++;
     }             

     if (randomNumber == 2)                
     {                    
          counter2++;                 
     }    

      ....
}

Or better yet, change to "else-if" statements. There's no need to check 11 "if" statements each time. If you use "else if", it only checks until it finds the one that satisfies the expression.

for(int counterTrials = 0; counterTrials < numberOfRolls; counterTrials++)          
{            
     randomNumber = ((int)(0+ Math.random()* 11));

     if (randomNumber == 1)                
     {                    
          counter1++;
     }             

     else if (randomNumber == 2)                
     {                    
          counter2++;                 
     }    

     else if (randomNumber == 3)                
     {                    
          counter3++;                 
     }

      ....
}

I guess this could be considered a nested statement (obviously not a nested for loop though), because you could re-write it as:

for(int counterTrials = 0; counterTrials < numberOfRolls; counterTrials++)          
{            
     randomNumber = ((int)(0+ Math.random()* 11));

     if (randomNumber == 1)                
     {                    
          counter1++;
     }//if             

     else{         
          if (randomNumber == 2)                
          {           
               counter2++;
          }//if                 
          else 
          {
               if (randomNumber == 3)                
               {           
                    counter3++;
               }//if 

                  .....
          }//else
     }//else    

     
}

Edited 7 Years Ago by cgeier: n/a

Comments
Good posts on this thread.
This question has already been answered. Start a new discussion instead.