Getting started with java | branch structure

Starting at: Getting started with java | branch structure
java entry series, from scratch!!!

Branching structure

1,if

Although the program of sequential structure can solve the problems of calculation and output, it can't be judged and selected again.
The branch structure should be used for the problem of judgment before selection.

1.1 form

    Single branch
    If (judging condition) {code satisfying the condition}

    Multibranched
    If (judgment condition){
       Code that meets the conditions
    }else{
       Code that does not meet the conditions
    }

    Nested branch
    If (judgment condition 1){
       Code 1
    }Else if (judgment condition 2){
       Code 2
    }Else if (judgment condition 3){
       Code 3
    }Else if (judgment condition 4){
       Code 4
    }else{
       Code that no one is satisfied with
    }

1.2 exercise 1: set a value within 10 and guess the number

package cn.qile;

import java.util.Scanner;

public class Test_If {
    public static void main(String[] args) {
        System.out.print("Please enter a number within 10:");
        int a = new Scanner(System.in).nextInt();

        if (a == 5) {
            System.out.print("congratulations! Guess right, the answer is 5!!!");
        }
    }
}

1.3 exercise 2: supplement the previous exercise. Only when the output is correct can the output be correct. If the output is wrong, the error will also be reported

package cn.qile;

import java.util.Scanner;

public class Test2_IfElse {
    public static void main(String[] args) {
        System.out.print("Please enter a number within 10:");
        int a = new Scanner(System.in).nextInt();

        if (a == 5) {
            System.out.print("congratulations! Guess right, the answer is 5!!!");
        } else {
            System.out.print("Young man, you guessed wrong, come again next time!!");
        }
    }
}

1.4 exercise 3: product discounts

Receive the original price entered by the user.
10% off for 1000.
20% off for over 2000.
50% off for 5000.

package cn.qile;

import java.util.Scanner;

public class Test3_ElseIf {
    public static void main(String[] args) {
        //1. Receive the original price entered by the user
        System.out.print("Original price of goods:");
        double price = new Scanner(System.in).nextDouble();

        //2. Judge and calculate the discounted price
        double now = price;//Record discounted price

        if(price >= 5000) {//Up to 5000
            now = price*0.5;//50% off
        }else if(price >= 2000) {//Over 2000
            now = price*0.8;//20% off
        }else if(price >= 1000) {//Up to 1000
            now = price*0.9;//10% off
        }

        System.out.println("The discounted price is:"+now);
    }
}

2,switch

When a case is established, all cases, including default, are penetrated backward from the case.
It doesn't end until the program ends or a break program is encountered.

2.1 form

    switch(Integer expression){
    case 1 : syso(1);break;
    case 2 : syso(2);
    case 3 : syso(3);
    case 4 : syso(4);
    case 5 : syso(5);
			
    default:syso(0);

    }

2.2 exercise 4: number matching

package cn.qile;

public class Test4_Switch {
    public static void main(String[] args) {
        int a = 1;
        //1. The parameter type in parentheses is integer: byte short int char jdk1.5 followed by String
        switch(a) {
            //3. When case is matched and there is no break, all cases including default will be penetrated backward
            //4. break can end the program
            case 1   : System.out.println(1);break;
            case 'a' : System.out.println(2);break;
            case 20  : System.out.println(3);break;

            //2. If there is no matching case, default will be executed
            default:System.out.println(0);
        }

    }
}

Tags: Programming Java

Posted on Wed, 20 May 2020 10:12:37 -0400 by bal bal