Results 1 to 10 of 10
  1. #1
    Puff26 is offline Member
    Join Date
    Apr 2009
    Posts
    5
    Rep Power
    0

    Default java help please

    Ugh havin homework problemz

    Write a program to simulate rolling dices as follows:
    Use the method Math.random( ) to generate random integers bewteen 1 and 6.
    Then generate ten-thousand numbers between 1 and 6. Then calculate the number of 1 that has appeared, the number of 2 that has appeared, and so on. The final output must be on a dialog box (using JOptionPane). The process must be looped until the user decides to quit. The output format (not the actual numbers) should like the following:

    *****************************************
    The dice has been rolled ten-thousand times, here is the statitics

    The number of 1s appeared is 2000
    The number of 2s appeared is 2000
    The number of 3s appeared is 2000
    The number of 4s appeared is 2000
    The number of 5s appeared is 1000
    The number of 6s appeared is 1000

    Do you want to try again (Y/N)?:

    *********************************************


    All i can do is make a program that asks you how many dice you want to roll, and how many times you want to roll it, them shows the added result of the rolls.

    import javax.swing.JOptionPane;

    public class Dice {


    public static void main (String[] args) {
    String temp1, temp2, temp3, temp4;
    int dice = 0;
    int roll = 0;

    while (true) {

    temp1= JOptionPane.showInputDialog ("How Many Dice Do You Want To Roll? ");
    dice = Integer.parseInt(temp1);
    if (dice > 0) break;
    System.out.println ("Must Be Positive!");

    }

    while (true) {
    temp2 = JOptionPane.showInputDialog ("How Many Times Do You Want To Roll? ");
    roll = Integer.parseInt(temp2);
    if (roll > 0) break;
    System.out.println ("Must Be Positive!");

    }

    int dicetotal = Dicecount (dice, roll);
    JOptionPane.showMessageDialog(null,
    "Dice total is " +dicetotal);

    }

    public static int Dicecount (int dice, int roll) {

    int dicetotal = 0;

    for (int i = 0; i <roll; i++) {

    for (int x = 0; x < dice; x++) {

    int rollcount =(int)(1+6*(Math.random()));
    dicetotal+=rollcount; }

    }

    return dicetotal;

    }

    }





    I need to make a program that automatically rolls the dice 10,000 times and shows how many times each number was rolled and then asks if you want to do it again (and does if you type "Y")

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Thanks for posting your code.

    Note though that most who want help here actually post a question that can be answered. Where's your question?

  3. #3
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    7

    Default

    Java Code:
    int dicetotal = Dicecount (dice, roll);
    JOptionPane.showMessageDialog(null,
    "Dice total is " +dicetotal);
    this part of code is want you want in your original code

  4. #4
    Puff26 is offline Member
    Join Date
    Apr 2009
    Posts
    5
    Rep Power
    0

    Default

    My question is how do i make a code to roll a six-sided dice 10,000 times and then show how many times each number (1-6) occured...I think i have to make 6 different arrays (1 for each number) and then have the program add 1 to the array with the corresponding number each time said number occurs, but i have no idea how to do that.
    Last edited by Puff26; 04-06-2009 at 08:09 AM.

  5. #5
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    7

    Default

    i think one array with 6 int is enough...
    if dice is 6 then dieResult[5]++

  6. #6
    Puff26 is offline Member
    Join Date
    Apr 2009
    Posts
    5
    Rep Power
    0

    Default

    Ugh...don't....understand...arrays, lol. The only way I'd learn 'em is if someone typed up the whole code using them...and i not asking anyone to do that. I got this far using a do/while loop...thing is the program only rolls the dice 6 times instead of 10,000....

    import javax.swing.JOptionPane;
    public class Dice1

    {
    public static void main(String args[])
    {
    int num1=1, num2=1, num3=1, num4=1, num5=1, num6=1;
    String choice="";
    int rollcount = (int)(1+6*(Math.random()));

    do
    {

    for (int counter = 0; counter >= 10000; counter ++)



    if (rollcount == 1)
    num1 ++;
    if (rollcount == 2)
    num2 ++;
    if (rollcount == 3)
    num3 ++;
    if (rollcount == 4)
    num4 ++;
    if (rollcount == 5)
    num5 ++;
    if (rollcount == 6)
    num6 ++;

    System.out.println("The number of 1s rolled was "+num1);
    System.out.println("The number of 2s rolled was "+num2);
    System.out.println("The number of 3s rolled was "+num3);
    System.out.println("The number of 4s rolled was "+num4);
    System.out.println("The number of 5s rolled was "+num5);
    System.out.println("The number of 6s rolled was "+num6);

    choice = JOptionPane.showInputDialog("Do you want to try again (Y/N)?:");
    } while (choice.equals("Yes") | choice.equals("yes") | choice.equals("Y") | choice.equals("y"));
    }

    }

  7. #7
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    7

    Default

    may i ask why num1 etc, initialize to 1?

    rollcount is the value of Dice... then why you dice once only?

    for loop only effect on
    Java Code:
    if (rollcount == 1)
    num1 ++;

  8. #8
    Puff26 is offline Member
    Join Date
    Apr 2009
    Posts
    5
    Rep Power
    0

    Default

    Quote Originally Posted by mtyoung View Post
    may i ask why num1 etc, initialize to 1?

    rollcount is the value of Dice... then why you dice once only?

    for loop only effect on
    Java Code:
    if (rollcount == 1)
    num1 ++;
    okay i see. so what should i initialize the variables as?

  9. #9
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    7

  10. #10
    Puff26 is offline Member
    Join Date
    Apr 2009
    Posts
    5
    Rep Power
    0

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •