Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: How to make the output side by side. I need HELP!

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    11
    My Mood
    Worried
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default How to make the output side by side. I need HELP!

    Okay so I have to find the number of times 2 die land on 2-12 /and the percentage of getting a 1 a 2 and so on rolling it 10million times.

    So far I have this:
    import java.util.Random;
    public class MonteCarlo {
     
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
     
     
     
    		    {  
    		        Random randomNumbers = new Random(); // Generates random numbers  
    		        int[] array = new int[ 13 ]; // Declares the array  
    		        int dice1;  
    		        int dice2;  
     
     
    		        //Roll the die 36,000 times  
    		        for ( int roll = 1; roll <=10000000; roll++ )  
    		        {     
    		            int die1 =  randomNumbers.nextInt ( 6 )+ 1;  
    		            int die2 =  randomNumbers.nextInt ( 6 )+ 1;  
    		            int total = die1 + die2;  
    		            ++array[total];  
     
    		        }  
     
     
    		        // outputs array values  
     
     
     
     
     
     
     
    		        int nRolls = 100, nDice = 6; // default values
     
     
     
    		         nRolls = 10000000;
    		        //10,000,000 rolls of the dice
    		         nDice = 6;
    		         // six sides of the dice
    		        int minSum = 2, maxSum = 2 * nDice;
    		        //lowest sum of dice is two. Max sum is 2 times the number of sides. minSum= 2, maxSum=12
    		        int[] Prop = new int[maxSum - minSum + 1];
     
    		        Random rand = new Random();
    		        //random
    		        for (int iter = 1; iter <= nRolls; iter++) {
    		            int throw1 = 1 + rand.nextInt(nDice), throw2 = 1 + rand.nextInt(nDice);
    		            int sum = throw1 + throw2;
    		            Prop[sum - minSum]++;
    		        }
     
    		        System.out.println("Number of rolls: " + nRolls + " on a six sided dice");
    		        //  Using int nRolls
    		        System.out.println("Number of sides of the dice: " + nDice);
    		        //Usin int nDice
    		        System.out.println("Sum of Dice         Percentage      Number Of Times Rolled");
    		        // created space so it creates space in the output
    		        for (int i = 0; i < Prop.length; i++) 
    		        {
    		        	System.out.println(String.format("   %2d                 %5.2f%%", i + minSum, Prop[i] * 100.0 / nRolls));
    		            // System.out.println("   " + (i+minSum) + "             " + (hist[i]*100.0/nRolls);
    		        }{
    		        for ( int face = 2; face < array.length; face++ )  
    		            System.out.printf( "%4d%10d\n",        face, array[ face ] );  
    		        	}
    		    } // end main  
     
    		    }} // end class DiceRollin


    The output is:
    Number of rolls: 10000000 on a six sided dice
    Number of sides of the dice: 6
    Sum of Dice         Percentage      Number Of Times Rolled
        2                  2.78%                      
        3                  5.56%
        4                  8.32%
        5                 11.11%
        6                 13.90%
        7                 16.67%
        8                 13.88%
        9                 11.11%
       10                  8.33%
       11                  5.55%
       12                  2.78%
     
           555185
         832554
          1111462
          1389278
          1667420
          1389082
         1111436
          832328
          554695
        277347


    As you can see i do not want the numbers 2-12 again. I need the out put to look like THIS:
     
     
    Number of rolls: 10000000 on a six sided dice
    Number of sides of the dice: 6
    Sum of Dice         Percentage      Number Of Times Rolled
        2                  2.78%                          279213
        3                  5.56%                          555185
        4                  8.32%                          832554
        5                 11.11%                          1111462
        6                 13.90%                          1389278
        7                 16.67%                          1667420
        8                 13.88%                          1389082
        9                 11.11%                          1111436
       10                  8.33%                          832328
       11                  5.55%                           554695
       12                  2.78%                           277347


    Please any advice would help.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: How to make the output side by side. I need HELP!

    Is the problem with the output that the Number Of Times Rolled is not on the same row with the other two values?

    Where do you format the line for printing? Why don't you add that third item to the data that is on the row?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Mar 2013
    Posts
    11
    My Mood
    Worried
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: How to make the output side by side. I need HELP!

    Sorry i made a mistake in copying my output...It looks like this and i want it changed to what is above.


    Output so far:
     
    Number of rolls: 10000000 on a six sided dice
    Number of sides of the dice: 6
    Sum of Dice         Percentage      Number Of Times Rolled
        2                  2.77%
        3                  5.56%
        4                  8.35%
        5                 11.13%
        6                 13.89%
        7                 16.67%
        8                 13.89%
        9                 11.10%
       10                  8.32%
       11                  5.55%
       12                  2.77%
       2    278192
       3    556866
       4    832608
       5   1111222
       6   1389046
       7   1666643
       8   1388537
       9   1110083
      10    832644
      11    555531
      12    278628


    --- Update ---

    Why don't you add that third item to the data that is on the row?
    I tried doing that but I get errors. I am probably doing it wrong. Please help. Thanks Norm

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: How to make the output side by side. I need HELP!

    but I get errors
    Please copy the full text of the error messages and paste it here.

    What are the two arrays: (array and Prop) used for? Why do you need two?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Feb 2013
    Posts
    45
    Thanks
    0
    Thanked 5 Times in 5 Posts

    Default Re: How to make the output side by side. I need HELP!

    you made mistake on your loop. Because you used double for loop
    So process completed the first for loop and print the statement after that that will be go to another one.

    So print your output using single for loop like below...

    for (int i = 0; i < Prop.length; i++) 
    		        {
    		        	System.out.println(String.format("   %2d                 %5.2f%%              %10d  ", i + minSum, Prop[i] * 100.0 / nRolls,array[ i+2 ]));
     
    		        }
    Regards
    Android developer
    Trinay Technology Solutions
    http://www.trinaytech.com
    5705750475

  6. The Following User Says Thank You to Tamilarasi For This Useful Post:

    JAVAHELPP (March 18th, 2013)

  7. #6
    Junior Member
    Join Date
    Mar 2013
    Posts
    11
    My Mood
    Worried
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: How to make the output side by side. I need HELP!

    Thank you!! I got it now. I don't know why i used a double loop. I'm new to all of this but i get it now.

Similar Threads

  1. Creating an EOF and Acknowledgement on client side
    By Jess17 in forum What's Wrong With My Code?
    Replies: 11
    Last Post: February 6th, 2013, 08:03 AM
  2. File transfer from Client to Server side
    By highlander141 in forum Java Networking
    Replies: 1
    Last Post: August 29th, 2012, 08:53 AM
  3. InputStream Problem at Client Side
    By pavan in forum Web Frameworks
    Replies: 1
    Last Post: March 26th, 2010, 03:21 AM
  4. Validate in server side..
    By Ganezan in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 27th, 2009, 06:36 AM
  5. Java program to open jsp page on client side instead of server side
    By khanshakil in forum JavaServer Pages: JSP & JSTL
    Replies: 1
    Last Post: July 8th, 2009, 06:26 AM

Tags for this Thread