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 5 of 5

Thread: Please Help: Random Dice Roll:

  1. #1
    Junior Member
    Join Date
    Jun 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Please Help: Random Dice Roll:

    Hello,

    I am a absolute beginner. I am trying to create a program that will randomly roll a die 600 times and display a table with the frequency and percent of each face number. Please help me understand what I need to do to make the following code work:


    public class diceroll
    {

    /**
    *
    */
    public static void main(String[] args)
    {
    int toRoll = 600, x,i=0, c1 = 0, c2 = 0, c3 = 0, c4 = 0, c5 = 0,
    c6 = 0;
    double pct1, pct2, pct3, pct4, pct5, pct6;

    for (i=0;i<=toRoll; i++)
    {
    x = (int)(Math.random()*6)+1;
    if (x==1)
    c1++;
    else if (x==2)
    c2++;
    else if (x==3)
    c3++;
    else if (x==4)
    c4++;
    else if (x==5)
    c5++;
    else if (x==6)
    c6++;
    }
    pct1 = (c1 * 100.0) / (double)toRoll;
    pct2 = (c2 * 100.0) / (double)toRoll;
    pct3 = (c3 * 100.0) / (double)toRoll;
    pct4 = (c4 * 100.0) / (double)toRoll;
    pct5 = (c5 * 100.0) / (double)toRoll;
    pct6 = (c6 * 100.0) / (double)toRoll;

    System.out.printf("Face\tFrequency\t%\n");
    System.out.printf("-------------------\n");
    System.out.printf("1\t%d\t%10.1f\n", c1);
    System.out.printf("2\t%d\t%10.1f\n", c2);
    System.out.printf("3\t%d\t%10.1f\n", c3);
    System.out.printf("4\t%d\t%10.1f\n", c4);
    System.out.printf("5\t%d\t%10.1f\n", c5);
    System.out.printf("6\t%d\t%10.1f\n", c6);

    }
    }


  2. #2
    Junior Member
    Join Date
    Jun 2013
    Posts
    4
    My Mood
    Fine
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Please Help: Random Dice Roll:

    Something like this?

    package diceRollExample;
     
    public class diceRollExample {
     
    	public static void main(String[] args) {
    		int i = 0,d1= 0,d2 =0,d3= 0,d4= 0,d5= 0,d6= 0,maxRoll,currentRoll = 0;
    		double p1 = 0,p2 = 0,p3 = 0,p4 = 0,p5 = 0,p6 = 0;
    		maxRoll = 100;
    		System.out.println("Running.");
    		System.out.println(d1 + " " + d2 + " " + d3 + " " + d4 + " " + d5 + " " + d6 + " " + maxRoll + " " + currentRoll);
    		for(i=0;i!=maxRoll;i++){
    			currentRoll = (int)(Math.random()*6)+1;
    			if(currentRoll == 1){
    				d1++;
     
    			}
    			else if(currentRoll == 2){
    				d2++;
     
    			}
    			else if(currentRoll == 3){
    				d3++;
     
    			}
    			else if(currentRoll == 4){
    				d4++;
     
    			}
    			else if(currentRoll == 5){
    				d5++;
     
    			}
    			else if(currentRoll == 6){
    				d6++;
     
    			}
    		}
    		p1 += (d1 * 100.0) / (double)maxRoll;
    		p2 += (d2 * 100.0) / (double)maxRoll;
    		p3 += (d3 * 100.0) / (double)maxRoll;
    		p4 += (d4 * 100.0) / (double)maxRoll;
    		p5 += (d5 * 100.0) / (double)maxRoll;
    		p6 += (d6 * 100.0) / (double)maxRoll;
    		//Output
    		System.out.println("Face\tFrequency\t%\n");
    		System.out.println("-------------------\n");
    		System.out.println("1. " + d1 + " " + p1 + "%" + "\n");
    		System.out.println("2. " + d2 + " " + p2 + "%" + "\n");
    		System.out.println("3. " + d3 + " " + p3 + "%" + "\n");
    		System.out.println("4. " + d4 + " " + p4 + "%" + "\n");
    		System.out.println("5. " + d5 + " " + p5 + "%" + "\n");
    		System.out.println("6. " + d6 + " " + p6 + "%" + "\n");
    		System.out.println(d1 + " " + d2 + " " + d3 + " " + d4 + " " + d5 + " " + d6 + " " + maxRoll + " " + currentRoll);
    	}
    }

    I know there isn't formatting like yours had, I don't know the printf stuff yet. maxRoll is set too 100, but you can obviously modify this value. I would have user input set to allow them to decide the number of times rolled with a case set for values less than or equal to zero.

    Another idea is the user can decide how many sides a die can have.

    Hope this helps a little
    Last edited by johnFuller001; June 15th, 2013 at 04:11 PM. Reason: more info

  3. #3
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Please Help: Random Dice Roll:

    Quote Originally Posted by dbs360 View Post
            System.out.printf("Face\tFrequency\t%\n");
    Since '%' is a format specifier, you have to do something special to print a '%' sign.

    Personally, I would spell out "percent" but if you want to print a percent sign, use "%%" in the format String.


    Quote Originally Posted by dbs360 View Post
            System.out.printf("1\t%d\t%10.1f\n", c1, pct1);
    After you fix the first error, what error message do you get when it gets to this line?


    Cheers!

    Z

  4. #4
    Junior Member
    Join Date
    Jun 2013
    Posts
    4
    My Mood
    Fine
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Please Help: Random Dice Roll:

    I get an error when I use the printf to output things...it is:

    Exception in thread "main" java.util.UnknownFormatConversionException: Conversion = '
    '
    	at java.util.Formatter.checkText(Unknown Source)
    	at java.util.Formatter.parse(Unknown Source)
    	at java.util.Formatter.format(Unknown Source)
    	at java.io.PrintStream.format(Unknown Source)
    	at java.io.PrintStream.printf(Unknown Source)
    	at diceRollExample.diceRollExample.main(diceRollExample.java:41)

    The same applies if I try to format the output of each dice roll.

  5. #5
    Junior Member
    Join Date
    Jun 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Please Help: Random Dice Roll:

    Thanks guys. I figured out my problem just by editing my code. My problem was the first printf line. I didnt assign the f inside the () as I did in the following output lines, so I just simply had to change the first one to println in that format. So the working code for this is as follows. Thanks again for your help



    public class diceroll
    {

    /**
    *
    */
    public static void main(String[] args)
    {
    int toRoll = 600, x,i=0, c1 = 0, c2 = 0, c3 = 0, c4 = 0, c5 = 0,
    c6 = 0, sum1;
    double pct1, pct2, pct3, pct4, pct5, pct6, sum2;

    for (i=0;i<toRoll; i++)
    {
    x = (int)(Math.random()*6)+1;
    if (x==1)
    c1++;
    else if (x==2)
    c2++;
    else if (x==3)
    c3++;
    else if (x==4)
    c4++;
    else if (x==5)
    c5++;
    else if (x==6)
    c6++;
    }
    pct1 = (c1 * 100.0) / (double)toRoll;
    pct2 = (c2 * 100.0) / (double)toRoll;
    pct3 = (c3 * 100.0) / (double)toRoll;
    pct4 = (c4 * 100.0) / (double)toRoll;
    pct5 = (c5 * 100.0) / (double)toRoll;
    pct6 = (c6 * 100.0) / (double)toRoll;

    sum1= c1+c2+c3+c4+c5+c6;
    sum2= pct1+pct2+pct3+pct4+pct5+pct6;

    System.out.println("Face\tFrequency\t%");
    System.out.printf("---------------------------\n");
    System.out.printf("1\t%d\t%10.1f\n", c1, pct1);
    System.out.printf("2\t%d\t%10.1f\n", c2, pct2);
    System.out.printf("3\t%d\t%10.1f\n", c3, pct3);
    System.out.printf("4\t%d\t%10.1f\n", c4, pct4);
    System.out.printf("5\t%d\t%10.1f\n", c5, pct5);
    System.out.printf("6\t%d\t%10.1f\n", c6, pct6);
    System.out.printf("---------------------------\n");
    System.out.printf("Total:\t%d\t%10.1f\n", sum1, sum2);
    }
    }

Similar Threads

  1. Roll Dice Program Button
    By shen_punkz21 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 28th, 2013, 03:51 PM
  2. Replies: 1
    Last Post: January 24th, 2013, 11:55 AM
  3. Can't figure out how to roll individual dice.
    By mortiz492 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 19th, 2012, 02:07 PM
  4. HELP random generator with roll() and print() method
    By disc_dido in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 18th, 2011, 01:50 PM
  5. [SOLVED] Help printing random dice
    By vanDarg in forum Java Theory & Questions
    Replies: 12
    Last Post: February 1st, 2011, 03:09 PM