当前位置: 代码迷 >> 综合 >> 第五章第三十二题(游戏:彩票)(Game: lottery)
  详细解决方案

第五章第三十二题(游戏:彩票)(Game: lottery)

热度:78   发布时间:2024-03-05 23:48:56.0

第五章第三十二题(游戏:彩票)(Game: lottery)

  • **5.32(游戏:彩票)修改程序清单3-8,产生一个两位数的彩票。这两位数是不同的。(提示:产生第一个数,使用循环不断产生第二个数,直到它和第一个数不同为止。)
    **5.32(Game: lottery) Revise Listing 3.8, Lottery.java, to generate a lottery of a two-digit number. The two digits in the number are distinct. (Hint: Generate the first digit. Use a loop to continuously generate the second digit until it is different from the first digit.)
  • 参考代码:
package chapter05;import java.util.Scanner;public class Code_32 {
    public static void main(String[] args) {
    int lottery = (int) (Math.random() * 100);while(lottery / 10 == lottery % 10)lottery = (int) (Math.random() * 100);Scanner input = new Scanner(System.in);System.out.print("Enter your lottery pick (two digits): ");int guess = input.nextInt();int lotteryDigit1 = lottery / 10;int lotteryDigit2 = lottery % 10;int guessDigit1 = guess / 10;int guessDigit2 = guess % 10;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");elseSystem.out.println("Sorry, no match");}
}
  • 结果显示:
Enter your lottery pick (two digits): 34
The lottery number is 19
Sorry, no matchProcess finished with exit code 0
  相关解决方案