write a program that simulates rooling a pair of dice.

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jul 2004
Posts: 16
Reputation: shantuli is an unknown quantity at this point 
Solved Threads: 0
shantuli shantuli is offline Offline
Newbie Poster

write a program that simulates rooling a pair of dice.

 
0
  #1
Jul 24th, 2004
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
Reply With Quote Quick reply to this message  
Join Date: Jul 2004
Posts: 16
Reputation: rickste_r is an unknown quantity at this point 
Solved Threads: 0
rickste_r's Avatar
rickste_r rickste_r is offline Offline
Newbie Poster

Re: write a program that simulates rooling a pair of dice.

 
0
  #2
Jul 29th, 2004
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!
Only lemmings should jump to conclusions! :rolleyes: Please rate me!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC