[英]How to count how many times something occures in a loop, java
I am trying to find out how many times the 6 occurs in this loop.我试图找出 6 在这个循环中出现了多少次。 I tried array,
ArrayList
, sapply and others but I still can't make it work.我尝试了 array、
ArrayList
、 sapply 等,但我仍然无法使其工作。
Here is the code这是代码
public class Numbersinhere {
public static void main(String[] args) {
int Min = 1;
int Max = 6;
for (int y = 0;
y < 6000;
y++) {
int x = Min + (int) (Math.random() * ((Max - Min) + 1));
System.out.println(x);
//how many times the 6 ??????????
}
}
}
It throws a dice 6000 times.它掷骰子 6000 次。
I guess I have to use the variable short
, but nothing behind that works我想我必须使用变量
short
,但背后没有任何作用
Maybe its because of public static void main(String[] args) {
but what else should I use in java?也许是因为
public static void main(String[] args) {
但我还应该在 java 中使用什么?
Try this method试试这个方法
public static void main (String[] args) throws java.lang.Exception {
// your code goes here
int min = 1;
int max = 6;
int reps = 10, x, count = 0;
for (int y = 0;
y < reps;
y++) {
x = min + (int) (Math.random() * ((max - min) + 1));
System.out.println(x);
if(x == 6) {
count++;
}
//how many times the 6 ??????????
}
System.out.println("Count = " + count);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.