简体   繁体   中英

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). Below are the conditions:

  1. If the user input matches the lottery numbers in exact order, the reward is Php 100,000.
  2. If all numbers in the user input matches all the lottery numbers, the reward is Php 30,000.
  3. If only one number in the user input matches a lottery number, the reward is Php 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."

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!");
    }
}

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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