943,907 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 13107
  • Java RSS
Jul 24th, 2004
0

write a program that simulates rooling a pair of dice.

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
shantuli is offline Offline
16 posts
since Jul 2004
Jul 29th, 2004
0

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

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!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
rickste_r is offline Offline
16 posts
since Jul 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Calculating and Displaying Distance
Next Thread in Java Forum Timeline: Trying to write a program that you can enter #s and multiply at the end.





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC