随机生成的数字有问题 - Java

Having An Issue With Randomly Generated Numbers - Java

我正在根据乔伊斯·法雷尔 (Joyce Farrell) 的 Java 编程书籍从事此项目,但我遇到随机生成数字和用户猜测未被正确检查的问题。例如,用户有 3 个猜测,假设他们的第一个猜测是 2,第一个随机生成的数字是 2,程序将打印出你输了。当猜测确实正确时。请帮我。我已经添加了程序的详细信息以及到目前为止所做的事情。


创建彩票游戏应用程序。生成三个随机数(见附录 D 中的帮助) 这样做),每个都在 0 和 9 之间。允许用户猜测三个数字。比较每一个 用户对三个随机数的猜测并显示一条消息,其中包括用户的 猜测,随机确定的三位数,以及用户拥有的金额 获奖情况如下

匹配号码奖($)

任何一个匹配10 两个配对 100 三个匹配,不按顺序 1000 三个匹配,顺序为1,000,000

不匹配 0

确保您的应用程序能够容纳重复数字。例如,如果用户 猜1、2、3,随机生成的数字是1、1、1,不给用户 三个正确猜测的功劳 - 只有一个。将文件另存为彩票。


我的源代码

// Filename: Lottery.java
// Written by: Andy A
// Written on: 14 January 2015

import java.util.Scanner;
import java.util.Random;

public class Lottery {

    public static void main(String[] args) {

        Scanner userInput = new Scanner(System.in);
        Random ranNum = new Random();

        // LIMIT Contains The Numbers From 0 - 9
        // TIMES Contains The Number of Time ranNum Should Run
        final int LIMIT = 9;
        final int TIMES = 3;

        // Users Guesses
        int usersFirstGuess;
        int usersSecondGuess;
        int usersThirdGuess;

        // Randomly Generated Numbers
        final int GenFirst = ranNum.nextInt(LIMIT);
        final int GenSecond = ranNum.nextInt(LIMIT);
        final int GenThird = ranNum.nextInt(LIMIT);

        // User is asked for 3 guesses
        System.out.println("Please enter your first guess: ");
        usersFirstGuess = userInput.nextInt();
        System.out.println("Please enter your second guess: ");
        usersSecondGuess = userInput.nextInt();
        System.out.println("Please enter your third and final guess: ");
        usersThirdGuess = userInput.nextInt();

        // Winning Amounts
        final double WinTen = 10;
        final double WinHun = 100;
        final double WinThund = 1000;
        final double WinMillion = 1000000;
        final int WinZero = 0;

        // Shows the randomly generated numbers
        for(int x = 0; x < TIMES; ++x)
            System.out.print(ranNum.nextInt(LIMIT) + " ");
        System.out.println();

        // First Generated
        if(GenFirst == usersFirstGuess ) {
            System.out.println("You have won: $" + WinTen);
        }
            else if(GenSecond == usersSecondGuess) {
                    System.out.println("You have won: $" + WinTen);
            }
            else if(GenThird == usersThirdGuess) {
                System.out.println("You have won: $" + WinTen);
            }
    }
}

您正在使用 ranNum.nextInt(LIMIT) 打印新生成的数字,但是您正在将用户输入与存储在 GenXXX 变量中的数字进行比较。

解决方案:改为打印变量。

System.out.println(GenFirst + " " + GenSecond + " " + GenThird);

如果您仍想使用循环进行打印,您可以将数字存储在一个数组中。

// generate
final int[] generated = new int[TIMES];
for (int x = 0; x < TIMES; x++)
    generated[x] = ranNum.nextInt(LIMIT);

// print
for (int x = 0; x < TIMES; x++)
    System.out.print(generated[x] + " ");

这不是随机生成数字的问题,而是您向用户显示它们的方式。

在您的 if / else if 语句之前,您在 for 循环中生成 new 随机数。这意味着,与用户输入 (genFirst) 相比的数字可以是 3,但是在 for 循环中显示给用户的数字是一个新的随机数,例如2.

要解决这个问题,您应该像这样显示生成的数字:

for (int ranInt : new int[] { GenFirst, GenSecond, GenThird}) {
    System.out.println(ranInt);
}

这段代码创建了一个生成的数字数组,并循环打印它们。显然,你也可以print GenFirst,然后print GenSecond,然后print GenThird

希望对您有所帮助!

没错, 你为什么打印

System.out.print(ranNum.nextInt(LIMIT) + " ");

什么时候你应该打印

System.out.print(GenThird + " ");
System.out.print(GenSecond + " ");
System.out.print(GenFirst + " ");

这应该可以解决问题。

// Filename: Lottery.java
// Written by: Andy A
// Written on: 14 January 2015

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.Random;

public class Lottery {

    public static void main(String[] args) {

        Scanner userInput = new Scanner(System.in);
        Random ranNum = new Random();

        // LIMIT Contains The Numbers From 0 - 9
        // TIMES Contains The Number of Time ranNum Should Run
        final int LIMIT = 9;
        final int TIMES = 3;

        // Users Guesses
        int usersFirstGuess;
        int usersSecondGuess;
        int usersThirdGuess;
        List<Integer> guesses = new ArrayList<>();

        // Randomly Generated Numbers
        final int GenFirst = ranNum.nextInt(LIMIT);
        final int GenSecond = ranNum.nextInt(LIMIT);
        final int GenThird = ranNum.nextInt(LIMIT);

        // User is asked for 3 guesses
        System.out.println("Please enter your first guess: ");
        usersFirstGuess = userInput.nextInt();
        guesses.add(usersFirstGuess);
        System.out.println("Please enter your second guess: ");
        usersSecondGuess = userInput.nextInt();
        guesses.add(usersSecondGuess);
        System.out.println("Please enter your third and final guess: ");
        usersThirdGuess = userInput.nextInt();
        guesses.add(usersThirdGuess);

        // Winning Amounts
        final double WinTen = 10;
        final double WinHun = 100;
        final double WinThund = 1000;
        final double WinMillion = 1000000;
        final int WinZero = 0;

        // Shows the randomly generated numbers
        System.out.println(GenFirst + " " + GenSecond + " " + GenThird);
        List<Integer> lottery = new ArrayList<>();
        lottery.add(GenFirst);
        lottery.add(GenSecond);
        lottery.add(GenThird);

        if (guesses.equals(lottery)) {
            System.out.println("You have won: $" + WinMillion);
        } else {
            int matchCount = 0;
            for (Integer guessValue : guesses) {
                if (lottery.contains(guessValue)) {
                    matchCount++;
                    lottery.remove(guessValue);
                }
            }

            switch (matchCount) {
                case 0:
                    System.out.println("You have won: $" + WinZero);
                    break;
                case 1:
                    System.out.println("You have won: $" + WinTen);
                    break;
                case 2:
                    System.out.println("You have won: $" + WinHun);
                    break;
                case 3:
                    System.out.println("You have won: $" + WinThund);
                    break;
            }
        }
    }
}

也许这会有所帮助!

import java.util.Scanner;
import java.util.Random;
public class Qellonumrat {

    
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        Random rand=new Random();
        int random_integer=(int) rand.nextInt(10);
        System.out.println("Guess the number: ");
        int number=sc.nextInt();
        while(true){
            if(number == random_integer){
                random_integer++;
                System.out.println("Congrats you won!!!");
                break;
            }
            else{
                System.out.println("Try again");
                break;
            }
        }
    }   
}