简体   繁体   English

有多少尝试猜测

[英]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. 类型,因为我认为代码可以正常工作,但是由于数组是6个数字,因此我认为这将花费一些时间。

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...!" 出于这个原因,我将数组更改为仅1个数字,直到返回“您在...中猜到了它!”仍然花了很长时间。 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: 要生成介于1到59(含)之间(但不是60)的随机整数,请在以下3个地方使用以下代码段:

     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. 最后一对for循环(连续3次打印之前的循环)看起来很奇怪。 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. PS:从59个元素中选择6个已订购商品有324亿种可能性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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