+ 1

How to count the number of times some print

For example if do a loop to run dice i wanna know how can i count the times it land on a 6

10th Nov 2019, 11:11 PM
Juan Madrigal
Juan Madrigal - avatar
6 Respuestas
+ 8
Inside loop use if condition and counter: if(dice lands on 6) counter++
10th Nov 2019, 11:31 PM
voja
voja - avatar
+ 2
Declare an int e.g "counter" and initialise it to 0 (zero) outside the loop, inside the loop, increment the counter if the dice is 6.
10th Nov 2019, 11:35 PM
rodwynnejones
rodwynnejones - avatar
+ 2
Not sure if mine is any better that your version...but this is what i came up with: int counter = 0; for(int a= 0; a < 1000; a++) { int mynum = (int)(Math.random() * 6 + 1); if(mynum == 6) counter++; } System.out.print("Count of six's is " + counter);
11th Nov 2019, 12:09 AM
rodwynnejones
rodwynnejones - avatar
+ 2
I made a quick example of this were you can pick any number between 1-6 and shows you the results. 👍 https://code.sololearn.com/cj9H9BAaGlQB/?ref=app
11th Nov 2019, 2:14 AM
D_Stark
D_Stark - avatar
+ 1
https://code.sololearn.com/cL0HkTUHyDo2/?ref=app
10th Nov 2019, 11:49 PM
Juan Madrigal
Juan Madrigal - avatar
+ 1
Thanks guys i was able to figure it out. It there anyway to clean up the code ?
10th Nov 2019, 11:50 PM
Juan Madrigal
Juan Madrigal - avatar