将用户输入与java中生成的3个随机数进行比较

时间:2016-06-02 14:22:59

标签: java random

所以我希望用户在1-10之间输入3个数字,然后系统将生成3个随机数,然后进行比较。如果他们匹配一个你得到250美元,匹配2 500美元。匹配3,订单为$ 1000,$ 0为不匹配。

到目前为止,我认为我得到了发电机工作(不能实际测试它),但我如何比较它们?

这是代码

import java.util.Random

public class lab3
{
    public static void main(String[] args
    {
        System.out.println("Enter three numbers between 1-10 ");

        System.out.println("Enter first guess: ");
        Int firstGuess= keyboard.nextInt();

        System.out.println("Enter second guess: ");
        Int secondGuess= keyboard.nextInt();

        System.out.println("Enter third guess: ");
        Int thirdGuess= keyboard.nextInt();

        Random rnd= new Random();
        for (int counter = 1; counter<= 3; ++counter) {
            int Lottery = rnd.nextInt(10);
            System.out.print("Generated numbers: " + counter + ": ");
        }

5 个答案:

答案 0 :(得分:0)

您应该将数字(由用户提供)存储在数组中。然后每次生成一个数字时,检查数组中是否存在该数字(带有“for”循环)。在第一场比赛中,您增加一个计数器并生成以下随机数,直到达到3次迭代。

答案 1 :(得分:0)

更简单的方法是将2x3整数存储在2个数组中:

int userInt[];
int randomInt[];

然后你在阵列中推送你的号码

userInt[0] = keyboard.nextInt(); //...Etc

最后使用double for循环遍历两个数组:

for(int i= 0; i<userInt.size(); i++){
    for (int j =0; j<randomInt.size(); j++){
        if (userInt[i] == randomInt[j]){
            //Do your stuff here

此外,您可能希望使用变量来存储正匹配的数量(我让您弄清楚如何)。

答案 2 :(得分:0)

也许是这样的:

int matchCount = 0;
for (int counter = 1; counter <= 3; ++counter) {
     int lottery = rnd.nextInt(10);
     System.out.print("Generated numbers: " + counter + ": ");
     if ((lottery == firstGuess) || (lottery == secondGuess) || (lottery == thirdGuess)) {
        matchCount++;
     }
  }

然后使用matchCount来确定玩家获胜的数量。

答案 3 :(得分:0)

您应该添加另一个变量,该变量将包含检查后获得的金额。

int moneyWon = 0;

在for循环中:

if(lottery == firstGuess || lottery == secondGuess || lottery == thirdGuess)            {
    if(moneyWon == 0){
        moneyWon = 250;
    }
    else{
        moneyWon = moneyWon * 2
    }
}

在循环之后你可以打印这样的东西:

System.out.println("Your total is: $" + moneyWon);

答案 4 :(得分:0)

Scanner keyboard = new Scanner(System.in);
    System.out.println("Guess the number and win the prize:");
    System.out.println("FIRST TRY ====> $1,00,000");
    System.out.println("SECOND TRY ===> $50,000");
    System.out.println("LAST TRY =====> $10,000");
    System.out.println("Guess the Number between 0 to 10(First)");
    int guess= keyboard.nextInt();

    Random rnd= new Random();
    int lotteryNumber = rnd.nextInt(10);
    if(lotteryNumber == guess){
        System.out.println("Your guess is true.");
        System.out.println("Congratulation!!! You won ---$1,00,000---");
    } else {
        System.out.println("Your Guess is not true.");
        System.out.println("Guess the Number between 0 to 10(Second)");
        int guess1= keyboard.nextInt();
        if(lotteryNumber== guess1) {
            System.out.println("Your Second Guess is true.");
            System.out.println("Congratulation!!!  You won ---$50,000---");
        } else {
            System.out.println("Your Guess is not true.");
            System.out.println("Guess the Number between 0 to 10(Third)");
            int guess2= keyboard.nextInt();
            if(lotteryNumber == guess2) {
                System.out.println("Your Third guess is true.");
                System.out.println("Congratulation!!!   You won ---$10,000---");
            } else {
                System.out.println("TRY AGAIN..");
            }
        }
    }
    System.out.println("Lottery Number is = " + lotteryNumber);