简体   繁体   中英

How many tries to guess

I have a lottery program that I would like to ask me to take a guess at the 'winning' numbers and then will generate said numbers. When it's done, it will print those numbers back to me, and tell me how many tries it took to arrive at the correct answer.

My code is below. I thought I'd gotten the answer from another thread, was so sure that I closed it - and can't find it now. My question is really just a 'What's wrong with my code?' type, as I think that the code is working, but since the array is 6 numbers I figure it will take the computer some time.

For that reason, I changed the array to just 1 number and it was still taking forever to come back with a "You guessed it in...!" Leading me to believe there is something else wrong I'm missing.

package lottery;

import java.util.Scanner;

public class lottery { // Begin lottery class
    public static void main(String[] args) { // Begin MAIN method

        // Define variables
        Scanner keyboard = new Scanner(System.in);
        int[] lottery = new int[6];
        int randomNum = 1 + (int) Math.random() * 59;
        int noTimes = 1;
        int guess = 0;
        // End variable definition

        System.out.println("Generating lottery numbers, what is your guess?");
        while (guess != randomNum) {
            guess = keyboard.nextInt();
            guess++;
        }

        // Input received - generate numbers now
        System.out.println("Thank you. Generating lottery numbers now...");

        for (int i = 0; i < 6; i++) {
            randomNum = (int) Math.ceil(Math.random() * 59); // Random number created here.
            for (int x = 0; x < i; x++) {
                if (lottery[x] == randomNum) // Here, code checks if same random number generated before.
                {
                    randomNum = (int) Math.ceil(Math.random() * 59);// If random number is same, another number generated.
                    x = -1; // restart the loop
                }

            }
            lottery[i] = randomNum;
        }

        for (int j = 0; j < noTimes; j++) {
            for (int i = 0; i < lottery.length; i++) {
                System.out.print("The winning numbers are: " + lottery[i] + " ");
            }
            System.out.println("\n");
            System.out.print("You correctly guessed in " + guess + " tries.");
        }
    } // End MAIN method   
} // End Lottery class

Apparently, I don't know how to do the regular code tag? colors?

There are various problems in your code, which are explained below.

  1. To generate a random integer between 1 and 59 inclusive (but not 60), please use the following code snippet in your 3 places:

     int randomNum = 1 + (int)(Math.random() * 59); 
  2. The guess-check logic needs to be fixed (as outlined below):

     int noTimes = 0; // Corrected ... while (guess != randomNum) { guess = keyboard.nextInt(); noTimes++; // Corrected } 
  3. The last pair of for-loops (the ones just before the 3 consecutive prints) look weird. Here is a more sensible replacement:

     System.out.print("The winning numbers are:"); for (int i = 0; i < lottery.length; i++) { System.out.print(" " + lottery[i]); } System.out.println(); System.out.println("You correctly guessed in " + noTimes+ " tries."); 

Other than these, your code looks okay to me so far.

PS: Choosing 6 ordered items from 59 elements has 32.4 billion possibilities.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM