简体   繁体   English

java中使用Math.random()的彩票游戏

[英]Lottery Game using Math.random() in java

We are assigned to develop a program to play lottery.The program will randomly generates lottery of a two-digit numbers, and prompts the user to input a two-digit numbers.我们被指派开发一个程序来玩彩票。该程序会随机生成一个两位数的彩票,并提示用户输入一个两位数的号码。 And if the numbers is only a single digit(0-9) the numbers must be treated as a two-digit number (00 - 09).如果数字只是一位数 (0-9),则必须将数字视为两位数 (00 - 09)。 Below are the conditions:以下是条件:

  1. If the user input matches the lottery numbers in exact order, the reward is Php 100,000.如果用户输入的号码与彩票号码完全匹配,则奖励为 100,000 菲律宾比索。
  2. If all numbers in the user input matches all the lottery numbers, the reward is Php 30,000.如果用户输入中的所有号码都匹配所有彩票号码,则奖励为 30,000 菲律宾比索。
  3. If only one number in the user input matches a lottery number, the reward is Php 10,000.如果用户输入中只有一个号码与彩票号码匹配,则奖励为 10,000 菲律宾比索。

This is my code so far...I actually have a problem on the 2nd condition (reward is Php30,000) whenever I matched only 1 number it says that "You matched all the lottery numbers. You won Php30,000."到目前为止,这是我的代码......我实际上在第二个条件(奖励是 Php30,000)上遇到了问题,每当我只匹配 1 个号码时,它就会说“你匹配了所有的彩票号码。你赢了 Php30,000”。

public static void main(String[] args) {
    Scanner lottery = new Scanner(System.in);
    int lottery1, lottery2, lottery3;
    System.out.println("\t\t\t\t\t\t~Welcome to THE LOTTERY~");
    System.out.println("Enter your first two-digit lucky number!");
    int guess1 = lottery.nextInt();
    System.out.println("Enter your second two-digit lucky number!");
    int guess2 = lottery.nextInt();
    System.out.println("Enter your last two-digit lucky number!");
    int guess3 = lottery.nextInt();
    System.out.println("\nThe winning numbers are: ");
    System.out.printf("%02d", lottery1 = (int)(Math.random() * 10));
    System.out.printf("\n" + "%02d", lottery2 = (int)(Math.random() * 10));
    System.out.printf("\n" + "%02d", lottery3 = (int)(Math.random() * 10));
    if(guess1 == lottery1 && guess2 == lottery2 && guess3 == lottery3 ){
        System.out.println("\n\nCongratulations! You matched all the lottery numbers in order.");
        System.out.println("You won Php100,000!");
    }else if(guess1 == lottery1 || lottery1 == guess1 && guess1 == lottery2 || lottery2 == guess1 && guess1 == lottery3 || lottery3 == guess1
            && guess2 == lottery1 || lottery1 == guess2 && guess2 == lottery2 || lottery2 == guess2 && guess2 == lottery3 || lottery3 == guess1
            && guess3 == lottery1 || lottery1 == guess3 && guess3 == lottery2 || lottery2 == guess3 && guess3 == lottery3 || lottery3 == guess3){
        System.out.println("\n\nCongratulations! You matched all the lottery numbers.");
        System.out.println("You won Php30,000!");
    }else if(guess1 == lottery1 || guess1 == lottery2 || guess1 == lottery3
            || guess2 == lottery1 || guess2 == lottery2 || guess2 == lottery3
            || guess3 == lottery1 || guess3 == lottery2 || guess3 == lottery3){
        System.out.println("\n\nCongratulations! You matched a lottery number.");
        System.out.println("You won Php10,000!");
    }else{
        System.out.println("\n\nSorry, your lucky numbers didn't matched any of the lottery numbers!");
    }
}`
 public static void main(String[] args) {
    Scanner lottery = new Scanner(System.in);
    int lottery1, lottery2, lottery3;
    int guess1, guess2, guess3;
    System.out.println("\t\t\t\t\t\t~Welcome to THE LOTTERY~");
    System.out.println("Enter your first two-digit lucky number!");
    guess1 = lottery.nextInt();
    System.out.println("Enter your second two-digit lucky number!");
    guess2 = lottery.nextInt();
    System.out.println("Enter your last two-digit lucky number!");
    guess3 = lottery.nextInt();
    System.out.println("The winning numbers are: ");
    System.out.printf("%02d", lottery1 = (int)(Math.random() * 100));
    System.out.printf("\n" + "%02d", lottery2 = (int)(Math.random() * 100));
    System.out.printf("\n" + "%02d", lottery3 = (int)(Math.random() * 100));
    if(guess1 == lottery1 && guess2 == lottery2 && guess3 == lottery3){
        System.out.println("\n\nCongratulations! You matched all the lottery numbers in order.\nYou won Php100, 000!");
    }
    else if(((guess1 == lottery1) || (guess1 == lottery2) || (guess1 == lottery3))
            && ((guess2 == lottery1) || (guess2 == lottery2) || (guess2 == lottery3))
            && ((guess3 == lottery1) || (guess3 == lottery2) || (guess3 == lottery3))){
        System.out.println("\n\nCongratulations! You matched all the lottery numbers.\nYou won Php30, 000!");
    }
    else if(guess1 == lottery1 || guess1 == lottery2 || guess1 == lottery3
        || guess2 == lottery1 || guess2 == lottery2 || guess2 == lottery3
        || guess3 == lottery1 || guess3 == lottery2 || guess3 == lottery3){
        System.out.println("\n\nCongratulations! You matched a lottery number.\nYou won Php10, 000!");
    }else{
        System.out.println("\n\nSorry, your lucky numbers didn't matched any of the lottery numbers!");
    }
}

} }

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

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