**3.15 (Game: lottery) Revise Listing 3.8, Lottery.java, to generate a lottery of a threedigit
number. The program prompts the user to enter a three-digit number and
determines whether the user wins according to the following rules:
1. If the user input matches the lottery number in the exact order, the award is
$10,000.
2. If all digits in the user input match all digits in the lottery number, the award is
$3,000.
3. If one digit in the user input matches a digit in the lottery number, the award is
$1,000.
**3.15(游戏:彩票)修改清单3.8,生成一个三位数的彩票。程序提示用户输入一个三位数的数字,并按以下用户是否获胜:
1.如果用户输入与彩票号码完全匹配,则奖励10,000美元,
2.如果用户输入的所有数字与彩票号码中的所有数字相匹配,奖金为3000美元。
3.如果用户输入的数字与彩票号码中的数字相匹配,则奖励1000美元。
代码如下:
import java.util.Scanner;
public class Unite3Test15
{
public static void main(String[] args)
{
int lottery = (int)(Math.random() * 1000);
Scanner input = new Scanner(System.in);
System.out.print("Enter your lottery pick (three digits): ");
int guess = input.nextInt();
int lotteryDigit1 = lottery / 100;
int lotteryDigit2 = lottery % 100;
int guessDigit1 = guess / 100;
int guessDigit2 = guess % 100;
System.out.println("The lottery number is " + lottery);
if (guess == lottery)
System.out.println("Exact match: you win $10,000");
else if (guessDigit2 == lotteryDigit1
&& guessDigit1 == lotteryDigit2)
System.out.println("Match all digits: you win $3,000");
else if (guessDigit1 == lotteryDigit1|| guessDigit1 == lotteryDigit2|| guessDigit2 == lotteryDigit1
|| guessDigit2 == lotteryDigit2)
System.out.println("Match one digit: you win $1,000");
else
System.out.println("Sorry, no match");
}
}
结果如下: