| | |
write a program that simulates rooling a pair of dice.
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Jul 2004
Posts: 16
Reputation:
Solved Threads: 0
hello everyone,
i am very new in java.this is why i need some help from you.i hope you will help me by solving the following problem:
write a program that simulates rooling a pair of dice. you can simulate rooling one die by choosing one of the integers 1, 2, 3, 4, 5, or 6 at random. the number you pick represents the number on the die after it is rolled. the expression
(int)(Math.random()*6) + 1
does the computation you need to select a random integer between 1 and 6. you can assign this value to a value to a variable to represent one of the dice that are being rolled. do this twice and add the results together to get the total roll. your program should report the number showing on each die as well as the total roll. for example:
the first die comes up 3
the second die comes up 5
your total roll is 8
regards,
shantuli :o
i am very new in java.this is why i need some help from you.i hope you will help me by solving the following problem:
write a program that simulates rooling a pair of dice. you can simulate rooling one die by choosing one of the integers 1, 2, 3, 4, 5, or 6 at random. the number you pick represents the number on the die after it is rolled. the expression
(int)(Math.random()*6) + 1
does the computation you need to select a random integer between 1 and 6. you can assign this value to a value to a variable to represent one of the dice that are being rolled. do this twice and add the results together to get the total roll. your program should report the number showing on each die as well as the total roll. for example:
the first die comes up 3
the second die comes up 5
your total roll is 8
regards,
shantuli :o
We use the Math.random class to generate a number from 1 to 6.
We do this again to generate the second number.
We have a total variable that gets the total of the 2 random numbers.
--------------------------------------------------------------------
public static void main(String[] args) {
//variables to hold numbers for the die and total
int no1 = (int)(Math.random()*6) + 1;
int no2 = (int)(Math.random()*6) + 1;
int total = no1 + no2;
//print out the result
System.out.println("Your first score: " + no1);
System.out.println("Your second score: " + no2);
System.out.println("The total is: " + total);
}
---------------------------------------------------------------------
That should be it, leave feedback! rickste_r!
We do this again to generate the second number.
We have a total variable that gets the total of the 2 random numbers.
--------------------------------------------------------------------
public static void main(String[] args) {
//variables to hold numbers for the die and total
int no1 = (int)(Math.random()*6) + 1;
int no2 = (int)(Math.random()*6) + 1;
int total = no1 + no2;
//print out the result
System.out.println("Your first score: " + no1);
System.out.println("Your second score: " + no2);
System.out.println("The total is: " + total);
}
---------------------------------------------------------------------
That should be it, leave feedback! rickste_r!
Only lemmings should jump to conclusions! :rolleyes: Please rate me!
![]() |
Similar Threads
- how to write a program for power (C)
- Help with rolling dice problem (C++)
- Clarity... (Java)
- rolling dice (C)
- Trying to write a program that you can enter #s and multiply at the end. (Java)
Other Threads in the Java Forum
- Previous Thread: Calculating and Displaying Distance
- Next Thread: Trying to write a program that you can enter #s and multiply at the end.
| Thread Tools | Search this Thread |
Tag cloud for Java
affinetransform android api apple applet application arc arguments array arrays automation binary bluetooth businessintelligence chat class classes client code component database desktop draw ebook eclipse encode equation error event exception file fractal game givemetehcodez graphics gui helpwithhomework html ide image input integer intersect j2me java javaexcel javaprojects jmf jni jpanel julia linked linux list loop mac main map method methods mobile netbeans newbie number online open-source oracle parameter print problem program programming project properties recursion reference replaysolutions rotatetext scanner score screen scrollbar server set size sms socket sort sql string superclass swing template test threads time tree windows working xstream





