Q BgQuestion:

      
Apprentice
Karma Points: 134
Respect (100%):
posted by  CollegeMath on 11/3/2009 5:04:25 PM  |  status: Closed  |  Earned Karma: 134

Java, Dice Tossing

Course Textbook Chapter Problem Needs by
Software Design N/A N/A N/A 11/3/2009 at 10:00:00 PM
Question Details:

I want to write a program that would do this in java. 

Simulate tossing a dice 1000 times.

Keep count of how often each integer gets roled.

List the resulting frequency for each integer as a count and as percentage. 


Bonus Point Alert! Earn +7 additional karma points for helping this gold member.

AAnswers:

Answer Question Ask for clarification
Oracle
Karma Points: 18,830
posted by Marth on 11/3/2009 6:39:12 PM  |  status: Live
Asker's Rating: Helpful   
Response Details:
Let us first write a method that rolls a die once.

public static int rollDie()
{
      return (int)(Math.random()*6+1);
}




public static void main(String[] args)
{
      // create an array to store the number of times each integer is rolled
     
      // for simplicity, I will use array size 7, with [1 - 6] as the possible outputs
     
      int[] output = new int[7];
     
      // roll die 1000 times
      for(int x = 0; x < 1000; x++)
      {
            output[rollDie()]++;
      }

      // print results
      for(int i = 1; i<=6; i++)
      {
            // print count and percentage
            System.out.println(i+": "+output[i]+" rolls; "+String.format("%.1f", (output[i]/10.0))+"%");
      }
}


Thank you for rating my response. Feel free to PM me if you have further questions.

Java Genius

"Lois Lane is falling, accelerating at an initial rate of 32ft per second per second. Superman swoops down to save her by reaching out two arms of steel. Ms. Lane, who is now traveling at approximately 120 miles per hour, hits them, and is immediately sliced into three equal pieces." - Sheldon
Apprentice
Karma Points: 134
posted by CollegeMath on 11/3/2009 7:42:29 PM  |  status: Live
Asker's Rating: N/A-Posted by Person Asking Question   
Response Details:

I get this error messsage when running it. «Ï 


----jGRASP exec: java RollingDice
ÏϧÏException in thread "main" java.util.IllegalFormatConversionException: f != java.lang.Integer
ÏϧϠ       at java.util.Formatter$FormatSpecifier.failConversion(Formatter.java:3999)
ÏϧϠ       at java.util.Formatter$FormatSpecifier.printFloat(Formatter.java:2722)
ÏϧϠ       at java.util.Formatter$FormatSpecifier.print(Formatter.java:2667)
ÏϧϠ       at java.util.Formatter.format(Formatter.java:2433)
ÏϧϠ       at java.util.Formatter.format(Formatter.java:2367)
ÏϧϠ       at java.lang.String.format(String.java:2769)
ÏϧϠ       at RollingDice.main(RollingDice.java:19)
ÏϧÏ
ÏÏ§Ï ----jGRASP wedge: exit code for process is 1.
ÏÏ©Ï ----jGRASP: operation complete.
¼¼ÏÏ

Oracle
Karma Points: 31,937
posted by rapunzel on 11/3/2009 8:07:10 PM  |  status: Live
Asker's Rating: Lifesaver   
Response Details:
please rate - thanks

worked fine for me.

I just moved the method after the main.  Used jGrasp, which I think you used. 
This is my output

 ----jGRASP exec: java untitled2

1: 176 rolls; 17.6%
2: 166 rolls; 16.6%
3: 153 rolls; 15.3%
4: 149 rolls; 14.9%
5: 190 rolls; 19.0%
6: 166 rolls; 16.6%

 ----jGRASP: operation complete





public class untitled2
{
public static void main(String[] args)
{
      // create an array to store the number of times each integer is rolled
     
      // for simplicity, I will use array size 7, with [1 - 6] as the possible outputs
     
      int[] output = new int[7];
     
      // roll die 1000 times
      for(int x = 0; x < 1000; x++)
      {
            output[rollDie()]++;
      }

      // print results
      for(int i = 1; i<=6; i++)
      {
            // print count and percentage
            System.out.println(i+": "+output[i]+" rolls; "+String.format("%.1f", (output[i]/10.0))+"%");
      }
}

public static int rollDie()
{
      return (int)(Math.random()*6+1);
}

}

Note to all members 1. Please 1 question per post 2. Show respect to your fellow members by rating all answers. 3. When rating remember that the first answer is not necessarily the best answer. 4. When answering questions, explain what you are doing, so that the asker will learn, don't just give a meaningless number. 5. Your answers should be your work. Don't copy from another member, this is Karma abuse and possible disciplinary actions against you. and GOOD LUCK to you all!!
Answer Question Ask for clarificarion

Join Cramster's Community

Cramster.com brings together students, educators and subject enthusiasts in an online study community. With around-the-clock expert help and a community of over 100,000 knowledgeable members, you can find the help you need, whenever you need it. Join for free today » How Cramster is different from tutoring »