0

This is my code..I do not think I used do/while loop correctly...

how can I use do/while properly?

Should I use different method?

I want to know how

   public static void main(String[] args) {
    // TODO code application logic here
    Scanner input = new Scanner(System.in);
    System.out.print("enter minimum of Yahtzee dice 2-9:");
    int min = input.nextInt();
    System.out.print("enter maximum of Yahtzee dice 3-10:");
    int max = input.nextInt();
    String tryAgain = "y";
    do {
    System.out.println("Dice   Rolls\tYahtzee's Percentage  Odds\t");
    for (int i = min; i <= max; i++) {
        int count = 0;
        boolean success = true;
        for (int j = 0; j <= 5000000; j++) {
            Random generator = new Random();
            YahtzeeDice d = new YahtzeeDice(generator, i);
            if (d.IsAYahtzee()) {
                count++;
            }
        }
        System.out.printf("%d     ", i);
        System.out.printf("%d\t", 5000000);
        System.out.printf("%d\t  ", count);
        System.out.printf("%f   ", (double) count / 5000000);
        double per = count / (double) 5000000;
        int odds = (int) (1 / per);
        System.out.printf("1 in %d\n", odds);
    }
        System.out.print("Run another simulation [y/n]? ");
        tryAgain = input.nextLine();
} while(!tryAgain.equals("n"));

1 Answers1

0

immibis nailed it in the first comment, I think. You are VERY close to having it right. try this:

public static void main(String[] args) {
        // TODO code application logic here
        Scanner input = new Scanner(System.in);
        Random generator = new Random();

        String tryAgain = "y";
        do {
            System.out.print("enter minimum of Yahtzee dice 2-9:");
            int min = input.nextInt();
            input.nextLine();
            System.out.print("enter maximum of Yahtzee dice 3-10:");
            int max = input.nextInt();
            input.nextLine();

            System.out.println("Dice   Rolls\tYahtzee's Percentage  Odds\t");

            for (int i = min; i <= max; i++) {
                int count = 0;
                boolean success = true;
                for (int j = 0; j <= 5000; j++) {
                    YahtzeeDice d = new YahtzeeDice(generator, i);
                    if (d.IsAYahtzee()) {
                        count++;
                    }
                }
                System.out.printf("%d     ", i);
                System.out.printf("%d\t", 5000000);
                System.out.printf("%d\t  ", count);
                System.out.printf("%f   ", (double) count / 5000000);
                double per = count / (double) 5000000;
                int odds = (int) (1 / per);
                System.out.printf("1 in %d\n", odds);
            }

            System.out.print("Run another simulation [y/n]? ");
            tryAgain = input.nextLine();

        } while (!tryAgain.equals("n"));

}

ps. while playing around, I moved your inputs without know what you were trying to do. you should move them back out of the loop, if that's what you intended.