• June 15th, 2013, 03:21 PM
dbs360
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);

}
}
• June 15th, 2013, 04:05 PM
johnFuller001
Something like this?

Code :

```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
• June 15th, 2013, 04:34 PM
Zaphod_b
Quote:

Originally Posted by dbs360
Code :

` 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
Code :

` 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
• June 15th, 2013, 05:14 PM
johnFuller001
I get an error when I use the printf to output things...it is:

Code :

```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.
• June 16th, 2013, 02:32 PM
dbs360
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);
}
}