【问题标题】:My main method has to repeat this 100 times: randomly generate numbers 1 to 12, use getDayInMonth to randomly generate days?我的主要方法必须重复100次:随机生成数字1到12,使用getDayInMonth随机生成天数?
【发布时间】: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


    【解决方案1】:
        for (int iteration = 1; iteration <= 10000; iteration++) {
            Random Month = new Random();
            int month = Month.nextInt(12) + 1;
            // Added call to getDayInMonth
            if (realmonth == month && getDayInMonth(month) == realday) {
                count++;
            }
        }
        // Moved this outside for loop
        System.out.println("The correct birthday was found " + count + " times during the 100 iterations.");
    

    为了简化您的代码,您可以随机选择一年中的一天,即 365,因为您似乎没有考虑闰年

        Random r = new Random();
        int realDate = r.nextInt(365) + 1; 
        // Or assign one yourself with 
        // LocalDate.of(2013, 4, 31).getDayOfYear();
    
        int count = 0;
        for (int iteration = 1; iteration <= 10000; iteration++) {
            int randDate = r.nextInt(365) + 1;
            if (randDate == realDate) {
                count++;
            }
        }
    

    【讨论】:

    • Quest 说 getDayInMonth 方法无法更改。必须是家庭作业挑战。
    • 后来看到了,但是第一部分仍然按照要求:)
    【解决方案2】:

    你很接近。问题是如何调用getDayInMonth 方法。这是一种方法,而不是您在问题中错误陈述的类。

    这个方法是static,意思是它独立于这个类的实例。换句话说,不是面向对象的。所以我们用类名来称呼它。

    int dayOfMonth = Real.getDayOfMonth( month ) ;
    

    扩展您的if 以测试month 和dayOfMonth。

    boolean monthMatch = ( month == realMonth ) ;
    boolean dayOfMonthMatch = ( dayOfMonth == realDay ) ;
    if ( monthMatch && dayOfMonthMatch ) { count++ ; }
    

    请注意,按照惯例,Java 程序员通常在 if 语句中包含大括号,即使是单行也是如此。

    命名约定

    在 Java 中,实例以小写首字母命名。所以你的Random Month 应该是Random month。但是你已经有了这样的变量名,所以改成Random monthRandom。

    另外,我会将其替换为:

    int month = ThreadLocalRandom.current().nextInt( 1 , 13 ) ;
    

    我们在那里使用 13,因为边界是排他性的,所以 12 + 1 = 13。请参阅 class documentation。

    java.time

    顺便说一句,在实际工作而不是学校作业中,我们会使用 java.time 类来解决这个问题,例如 Month 和 MonthDay。

    【讨论】:

    • 当这段代码,“int dayOfMonth = Real.getDayOfMonth(month);”添加它指出该月份无法解决为我首先遇到的问题的变量,我该如何解决这个问题以便识别它?
    • @Dave 您有两个名称相同的变量 - 非常糟糕。修正您的 var 名称,并重新阅读我的答案中的“命名约定”部分。
    • 它仍然不起作用,我不知道为什么,没有变量具有相同的名称,并且在使用此代码时仍然有消息说“月份”无法识别......
    • @Dave 如有疑问,请重新开始,一次一行,随时编译。
    猜你喜欢
    • 2017-07-25
    • 1970-01-01
    • 2017-12-24
    • 2016-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-28
    • 1970-01-01
    相关资源
    最近更新 更多