Monday, 6 June 2016

Chapter 3 Exercise 15, Introduction to Java Programming, Tenth Edition Y. Daniel LiangY.

**3.15 (Game: lottery) Revise Listing 3.8, Lottery.java, to generate a lottery of a three-digit 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.


import java.util.Scanner;
 
public class ProgrammingEx3_15 {
 public static void main(String[] args) {
  // Generate a lottery
  int lottery = (int) (Math.random() * 1000);
 
  // Prompt the user to enter a guess
  Scanner input = new Scanner(System.in);
  System.out.print("Enter your lottery pick (three digits): ");
  int guess = input.nextInt();
 
  // Get digits from lottery
  int lotteryDigit1 = lottery / 100;
  int lotteryDigit2 = (lottery % 100) / 10;
  int lotteryDigit3 = lottery % 10;
 
  // Get digits from guess
  int guessDigit1 = guess / 100;
  int guessDigit2 = (guess % 100) / 10;
  int guessDigit3 = guess % 10;
 
  System.out.println("The lottery number is " + lotteryDigit1
    + lotteryDigit2 + lotteryDigit3);
 
  // Check the guess
  if (guess == lottery)
   System.out.println("Exact match: you win $10,000");
  else if ((guessDigit1 == lotteryDigit2 && guessDigit2 == lotteryDigit1 && guessDigit3 == lotteryDigit3)
    || (guessDigit1 == lotteryDigit2
      && guessDigit1 == lotteryDigit3 && guessDigit3 == lotteryDigit1)
    || (guessDigit1 == lotteryDigit3
      && guessDigit2 == lotteryDigit1 && guessDigit3 == lotteryDigit2)
    || (guessDigit1 == lotteryDigit3
      && guessDigit2 == lotteryDigit2 && guessDigit3 == lotteryDigit1)
    || (guessDigit1 == lotteryDigit1
      && guessDigit2 == lotteryDigit3 && guessDigit3 == lotteryDigit2))
   System.out.println("Match all digits: you win $3,000");
  else if (guessDigit1 == lotteryDigit1 || guessDigit1 == lotteryDigit2
    || guessDigit1 == lotteryDigit3 || guessDigit2 == lotteryDigit1
    || guessDigit2 == lotteryDigit2 || guessDigit2 == lotteryDigit3
    || guessDigit3 == lotteryDigit1 || guessDigit3 == lotteryDigit2
    || guessDigit3 == lotteryDigit3)
   System.out.println("Match one digit: you win $1,000");
  else
   System.out.println("Sorry, no match");
 }
}

2 comments :

  1. there are small error here at
    condition 2
    (guessDigit1 == lotteryDigit2
    && guessDigit1 == lotteryDigit3 && guessDigit3 == lotteryDigit1)


    it should be
    (guessDigit1 == lotteryDigit2
    && guessDigit2 == lotteryDigit3 && guessDigit3 == lotteryDigit1)

    ReplyDelete
  2. package MyProgrammingLabChapter03;

    import java.util.Scanner;

    public class GameLottery {

    public static void main(String[] args) {
    Scanner input = new Scanner(System.in);

    int threeDigitNumber = (int) (100 + (Math.random() * 900));
    int d1 = threeDigitNumber % 10;
    int d2 = (int) ((threeDigitNumber % 100) / 10);
    int d3 = (int) ((threeDigitNumber % 1000) / 100);

    System.out.println("Enter a three-digit number");
    int guess = input.nextInt();
    int guessD1 = guess % 10;
    int guessD2 = (int) ((guess % 100) / 10);
    int guessD3 = (int) ((guess % 1000) / 100);

    boolean threeDigit = (guess > 99) && (guess < 1000);
    if (threeDigit) {
    if (threeDigitNumber == guess) {
    System.out.println("You've won 10_000$");
    } else if ((d1 == guessD2 && d2 == guessD1 && d3 == guessD3)
    || (d1 == guessD2 && d2 == guessD3 && d3 == guessD1)
    || (d1 == guessD1 && d2 == guessD3 && d3 == guessD2)
    || (d1 == guessD3 && d2 == guessD1 && d3 == guessD2)
    || (d1 == guessD3 && d2 == guessD2 && d3 == guessD1)) {
    System.out.println("You've won 3_000$");
    } else if (d1 == guessD1 || d1 == guessD2 || d1 == guessD3 || d2 == guessD1 || d2 == guessD2
    || d2 == guessD3 || d3 == guessD1 || d3 == guessD2 || d3 == guessD3) {
    System.out.println("You've won 1_000$");
    } else {
    System.out.println("Sorry. No match");
    }

    } else {
    System.out.println("Please enter a three-digit number: ");
    }

    }

    }

    ReplyDelete