简体   繁体   English

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

[英]Having An Issue With Randomly Generated Numbers - Java

I am working on this project from the Java Programming book by Joyce Farrell, and I am having an issue with the Randomly Generated number and the user's guesses not being checked correctly.我正在从 Joyce Farrell 的 Java Programming 一书中研究这个项目,我遇到了随机生成数字的问题,并且用户的猜测没有被正确检查。 For example the user has 3 guesses, lets say their first guess it 2 and the first randomly generated number is 2 the program will print out You lose.例如,用户有 3 次猜测,假设他们的第一次猜测是 2,第一个随机生成的数字是 2,程序将打印出你输了。 When the guess is actually correct.当猜测实际上是正确的。 Please help me.请帮我。 I have added the details of the program plus what I have done so far.我已经添加了程序的详细信息以及我到目前为止所做的事情。


Create a lottery game application.创建彩票游戏应用程序。 Generate three random numbers (see Appendix D for help in doing so), each between 0 and 9. Allow the user to guess three numbers.生成三个随机数(请参阅附录 D 以获得帮助),每个随机数介于 0 和 9 之间。允许用户猜测三个数字。 Compare each of the user's guesses to the three random numbers and display a message that includes the user's guess, the randomly determined three-digit number, and the amount of money the user has won as follows.将用户的每个猜测与三个随机数进行比较,并显示一条消息,其中包括用户的猜测、随机确定的三位数字以及用户赢得的金额,如下所示。

Matching Numbers Award($)匹配数字奖($)

Any one matching 10 Two matching 100 Three matching, not in order 1000 Three matching, in exact order 1,000,000任意一个匹配 10 二匹配 100 三匹配,不按顺序 1000 三匹配,完全按顺序 1,000,000

No match 0没有匹配 0

Make certain that your application accommodates repeating digits.确保您的应用程序可以容纳重复数字。 For example, if a user guesses 1, 2, and 3, and the randomly generated digits are 1, 1, and 1, do not give the user credit for three correct guesses - just one.例如,如果用户猜测 1、2 和 3,并且随机生成的数字是 1、1 和 1,则不要将三个正确猜测的功劳归于用户 - 只有一个。 Save the file as Lottery.将文件另存为 Lottery。


My Source Code我的源代码

// 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);
            }
    }
}

You are printing newly generated numbers with ranNum.nextInt(LIMIT) , however you are comparing the user input with the numbers stored in the GenXXX variables.您正在使用ranNum.nextInt(LIMIT)打印新生成的数字,但是您正在将用户输入与存储在GenXXX变量中的数字进行比较。

Solution: Print the variables instead.解决方案:改为打印变量。

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

If you still want to use a loop for printing you can store the numbers in an array.如果您仍想使用循环进行打印,您可以将数字存储在数组中。

// 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] + " ");

This is not the problem of the randomly generated numbers, but if your way of showing them to the user.这不是随机生成的数字的问题,而是您向用户展示它们的方式。

Before your if / else if statements, in the for-loop you are generating new random numbers.在您的if / else if语句之前,在 for 循环中您正在生成新的随机数。 That means, the number compared to the users input ( genFirst ) can be 3 , but the number shown to the user in the for loop is a new random number, for example 2 .这意味着,与用户输入( genFirst )相比的数字可以是3 ,但在 for 循环中向用户显示的数字是一个新的随机数,例如2

To fix this problem, you should display the generated numbers like that:要解决此问题,您应该像这样显示生成的数字:

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

This piece of code creates an array of the generated numbers and loops through them printing them.这段代码创建了一个生成数字的数组,并通过它们循环打印它们。 Obviously, you can also print GenFirst , then print GenSecond and then print GenThird .显然,您也可以print GenFirst ,然后再print GenSecond ,然后再print GenThird

I hope this helps!我希望这有帮助!

Exactly, why are you printing对了,你为什么要打印

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

when you should be just printing当你应该只是打印

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

This should do the trick.这应该可以解决问题。

 // 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; } } } }

Maybe this will help!也许这会有所帮助!

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;
            }
        }
    }   
}

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

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