Java language programming (eight) switch statement and lottery winning problem

Immediately after the last Java language programming (7), whether we execute a statement is determined by the combination of several conditions. We can use logical operators to combine these conditions. Logical operators are also called Boolean operations. The operator is an operation on a Boolean value. It creates a new Boolean value. Boolean operators include the not operator (!) (same as logical not), and operator (&&) (same as logical and meaning), Or operator (||) (same as logical OR), XOR operator ( ^ ) (same as logical XOR), the meaning of XOR is: if and only if the two operands have different Boolean values When the XOR of the two Boolean operands is true.

      1. Switch statement

      If in the program, we need to perform further calculations based on the specific value of the variable, in order to fully consider all situations, we need to use nested if statements, but too many nested statements will make the program difficult to read, Java The switch statement is provided to effectively deal with the problem of multiple conditions. Let's use a program list to show it in detail.

      switch(status){

          case 0: compute taxes for single filers;

                       break;

          case 1: compute taxes for married filing jointly;

                       break;

          case 2: compute taxes for married filing separately;

                       break;

          case 3: compute taxes for head of household;

                       break;

          default: System.out.println("Errors: invalid status");

                       System.exit(0);

      The meaning of the above switch statement is to determine if the status is 0, calculate the tax of a single taxpayer, if the status is 1, calculate the tax of a married joint taxpayer, if the status is 2, calculate the tax of a married single taxpayer , If the status is 3, calculate the tax of the taxpayer of the household head, if not satisfied, output Errors, System.exit(0) and System.exit(1) are both end procedures, but 0 means normal end, 1 It ended abnormally.

      2. Lottery issues

      If we plan to develop a lottery game, the program randomly generates a two-digit lottery ticket, prompts the user to enter a two-digit number, and then determines whether the user can win according to the following rules:

      (1) If the user's input matches the actual order of the lottery, the prize is 10,000 USD.

      (2) If all the numbers entered by the user match all the numbers in the lottery (that is, the order is reversed), the prize is 3000 USD.

      (3) If a number entered by the user matches a number in the lottery, the prize is 1000 USD.

      We can use the random method of the Math class mentioned in the previous article to generate random numbers, and then enter the judgment sentence, divide the input two digits by 10 to take the first digit, and the input two digits to the remaining 10 to get the second , And then judged by conditional statements. The program list is as follows:

package lottery;

import java.util.Scanner;

/**
 *
 * @author john
 */
public class Lottery {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
         int lottery = (int)(Math.random()*100);
         Scanner input = new Scanner(System.in);
         System.out.println("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 $10000");
         else if(guessDigit1==lotteryDigit2&&guessDigit2==lotteryDigit1)
             System.out.println("Match all digits: you win $3000");
         else if(guessDigit1==lotteryDigit1||guessDigit1==lotteryDigit2||guessDigit2==lotteryDigit1||guessDigit2==lotteryDigit2)
             System.out.println("Match one digit: you win $1000");
         else
             System.out.println("Sorry,no match");
    }
    
}

image

      The number I entered is 40 and the randomly generated number is 16, showing the last case.

image

      The entered number is 39, and the randomly generated number is 37, indicating the third case, and the program is correct. In this program, we use the logical operators mentioned above. In the next article, I will write loop statements and answer questions through confirmation dialogs . I hope we all learn together and make progress together, thank you.


Guess you like

Origin blog.51cto.com/15064656/2602778