使用java

时间:2018-03-06 22:54:27

标签: java if-statement java.util.scanner

我们被指派开发一个程序来玩彩票。该程序将随机生成一个两位数的彩票,并提示用户输入一个两位数的数字。如果数字只是一个数字(0-9),则必须将这些数字视为两位数字(00 - 09)。以下是条件:

  1. 如果用户输入按照确切的顺序匹配彩票号,则奖励为10万菲律宾比索。
  2. 如果用户输入中的所有数字都与所有彩票号码匹配,则奖励为Php 30,000。
  3. 如果用户输入中只有一个号码与彩票号码匹配,则奖励为Php 10,000。
  4. 这是我的代码到目前为止......我实际上在第二个条件(奖励是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!");
        }
    }`
    

1 个答案:

答案 0 :(得分:1)

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

}