public class CountTest { public static int count1 = 0; public static int count2 = 0; public static void main(String[] args) { Random r = new Random(); for (int i=0; i < 10; i++) { int num = r.nextInt(); if (num > 0.5) { method1(); } else { method2(); } }
public class CountTest {
public static int count1 = 0;
public static int count2 = 0;
public static void main(String[] args) {
Random r = new Random();
for (int i=0; i < 10; i++) {
int num = r.nextInt();
if (num > 0.5) {
method1();
} else {
method2();
}
}
System.out.println(count1 + " " + count2);
}
public static void method1() {
count1++;
}
public static void method2() {
count2++;
}
}
每次调用都自加一次
最后输出