The exercise is to simulate the roll of a dice and produce output in the number of launches that were needed to lure the same face for 5 consecutive launches.
The simulation of the launch, you get the class using Math.random (), which generates a random number (if I remember correctly a double) between 0 and 1. multiply the value for the number of possibilities we want to achieve (in our case the number of sides of the dice, 6 in all) and carry out the casting for " (int) " in order to approximate the value to an integer.
At this point the results will be: 0 1 2 3 4 5
Since we are interested in the values \u200b\u200bfrom 1 to 6, we add 1 to the value obtained.
for the rest of the code is as follows:
- / *
- This program simulates the roll of a dice and count the number of extractions
- necessary so that 'bait 5 times the same number. The result obviously varies from time to time!
- * /
- public class Fiverand
- {
- {
- int c = 0 ;
- int temp = 0 ;
- int n = 0 ;
- do
- {
- c + + ;
-
- if ( temp == lancio ) / / count how many times you come out the same number
- {
- n + +;
- }
- else / / reset the counter
- {
- n = 1 ;
- temp = launch ;
- }
- }
- while (n ! = 5) ; / / the cycle is repeated until the count and 'other than 5 ;
-
- }
- }
it will produce output in all walks of life and finally made the string that indicates to us the number of extractions that occurred and the number of "lucky"
soon!
Potete trovare tutti i miei programmi quì .
0 comments:
Post a Comment