Java语言程序设计 例题3.15

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

结果如下:

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

差劲的厉害了

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值