【发布时间】:2017-03-01 04:18:37
【问题描述】:
所以这就是我到目前为止所拥有的,getDayInMonth 类必须保持不变,我希望它计算它匹配的次数。请问你是怎么做到的?
public class Real {
public static void main(String[] args) {
int realmonth = 5;
int realday= 11;
int count = 0;
for(int iteration = 1; iteration <=100; iteration ++) {
Random Month = new Random();
int month = Month.nextInt(12) + 1;
if(realmonth == month) count++;
System.out.println("The correct birthday was found " + count + " times during the 100 iterations.");
}
public static int getDayInMonth(int month) {
final int JANUARY = 1; final int JULY = 7;
final int FEBRUARY = 2; final int AUGUST = 8;
final int MARCH = 3; final int SEPTEMBER = 9;
final int APRIL = 4; final int OCTOBER = 10;
final int MAY = 5; final int NOVEMBER = 11;
final int JUNE = 6; final int DECEMBER = 12;
Random dayGenerator = new Random();
switch (month) {
case JANUARY: case MARCH: case MAY: case JULY:
case AUGUST: case OCTOBER: case DECEMBER:
return dayGenerator.nextInt(31) + 1;
case APRIL: case JUNE: case SEPTEMBER: case NOVEMBER:
return dayGenerator.nextInt(30) + 1;
case FEBRUARY:
return dayGenerator.nextInt(28) + 1;
default:
return -1;
}
}
}
【问题讨论】:
标签: java date random count repeat