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

时间:2015-02-07 20:20:15

标签: java oop

我正在使用Joyce Farrell撰写的Java Programming book中的这个项目,我遇到了随机生成的数字和用户未正确检查的猜测的问题。例如,用户有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);
            }
    }
}

4 个答案:

答案 0 :(得分:4)

您正在使用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] + " ");

答案 1 :(得分:0)

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

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

要解决此问题,您应该显示生成的数字:

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

这段代码创建了一个生成数字的数组,并通过它们打印它们。显然,您还可以print GenFirst,然后print GenSecond,然后print GenThird

我希望这有帮助!

答案 2 :(得分:0)

完全, 你为什么打印

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

何时应该只是打印

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

答案 3 :(得分:0)

这应该可以解决问题。

&#13;
&#13;
// 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;
            }
        }
    }
}
&#13;
&#13;
&#13;