Results 1 to 10 of 10
Thread: java help please
- 04-06-2009, 06:06 AM #1
Member
- Join Date
- Apr 2009
- Posts
- 5
- Rep Power
- 0
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")
-
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?
- 04-06-2009, 06:26 AM #3
Senior Member
- Join Date
- Dec 2008
- Location
- Hong Kong
- Posts
- 473
- Rep Power
- 13
Java Code:int dicetotal = Dicecount (dice, roll); JOptionPane.showMessageDialog(null, "Dice total is " +dicetotal);
- 04-06-2009, 07:03 AM #4
Member
- Join Date
- Apr 2009
- Posts
- 5
- Rep Power
- 0
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 07:09 AM.
- 04-06-2009, 10:43 AM #5
Senior Member
- Join Date
- Dec 2008
- Location
- Hong Kong
- Posts
- 473
- Rep Power
- 13
i think one array with 6 int is enough...
if dice is 6 then dieResult[5]++
- 04-07-2009, 05:16 AM #6
Member
- Join Date
- Apr 2009
- Posts
- 5
- Rep Power
- 0
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"));
}
}
- 04-07-2009, 11:06 AM #7
Senior Member
- Join Date
- Dec 2008
- Location
- Hong Kong
- Posts
- 473
- Rep Power
- 13
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 ++;
- 04-08-2009, 06:39 AM #8
Member
- Join Date
- Apr 2009
- Posts
- 5
- Rep Power
- 0
- 04-08-2009, 10:53 AM #9
Senior Member
- Join Date
- Dec 2008
- Location
- Hong Kong
- Posts
- 473
- Rep Power
- 13
i think it should initialize to 0 as 0 times rolled
- 04-09-2009, 06:13 AM #10
Member
- Join Date
- Apr 2009
- Posts
- 5
- Rep Power
- 0
Bookmarks