+ 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
6 ответов
+ 8
Inside loop use if condition and counter:
if(dice lands on 6)
counter++
+ 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.
+ 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);
+ 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
+ 1
https://code.sololearn.com/cL0HkTUHyDo2/?ref=app
+ 1
Thanks guys i was able to figure it out. It there anyway to clean up the code ?