mardi 28 février 2017

My main method has to repeat this 100 times: randomly generate numbers 1 to 12, use getDayInMonth to randomly generate days?

So this is what i have so far and the class getDayInMonth has to remain unchanged and i would like it to count how many times it matches. How do you did it please?

import java.util.Random;

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;      }   }

}




Aucun commentaire:

Enregistrer un commentaire