Java_ Process control

Java_ Process control

Branching structure

if

  • The code that executes a branch is determined according to the result of the determination (true or false)

    if() {
    	doSomething1();
    }
    if() {
    	doSomething1();
    }else {
    	doSomething2();
    }
    if() {
    	doSomething1();
    }else if() {
    	doSomething2();
    }...{
    	doSomething3();
    }else {
    	doSomething4();
    }
    
  • Execution process:

    1. Judge the value of condition 1 first. If it is true, execute statement body 1 and the branch ends; If false, judge the value of condition 2
    2. If the value is true, execute statement body 2 and the branch ends; If false, judge the value of condition 3
    3. ...
    4. If none of the conditions is true, the statement body n+1 of the else branch is executed.

switch

  • It is also a branch to be executed by matching conditions. It is suitable for branch selection for value matching. It has clear structure and good format.

    switch() {
        case 1:
            doSomething1();
            break;
        case 2:
            doSomething2();
            break;
        case 3:
            doSomething3();
            break;
        default:
            doSomething4();
    }
    
  • Execution process:

    1. First execute the value of the expression and take this value to match the value after case.
    2. If the value of the matched case is true, the case will be executed, and the switch branch will jump out in case of a break.
    3. If the values after case do not match, the default code is executed.
  • Precautions for switch branch:

    1. The expression types can only be byte, short, int and char. JDK5 starts to support enumeration, and JDK7 starts to support String, but does not support double, float and long.
    2. The value given by case cannot be repeated, and can only be literal, not variable.
    3. Don't forget to write break, otherwise penetration will occur.

Penetration of switch

  • If the code is executed to a case block without a break, it will directly enter the next case block to execute the code (and no matching will be made),

    It does not jump out of the branch until it encounters a break, which is the penetration of switch.

  • switch penetration case (month days viewer)

    Requirement: you can enter a month to display the days of the month.

    • 1. March, may, July, August, October and December are 31 days
    • February has 29 days in leap years and 28 days in non leap years.
    • 4. June, September and November are 30 days
public static void showMonth() {
    Scanner in = new Scanner(System.in);
    int month = 0;
    System.out.print("Please enter month:");
    if (in.hasNextInt()) {
        month = in.nextInt();
    }
    switch(month){
        case 1: case 3: case 5: case 7:
            case 8: case 10: case 12:
                System.out.println("31 days of the lunar month");
                break;
        case 4: case 6: case 9: case 11:
            System.out.println("30 days in a small month");
            break;
        case 2:
            int year = new Date().getYear();
            int day = year % 4 == 0 && year % 100 > 0 || year % 400 == 0 ? 29 : 28;
            System.out.println(day == 29 ? "Leap year, 29 days" : "Non leap year, 28 days");
            break;
        default:
            System.out.println("Month error, please enter a number between 1 and 12!");;
    }
}

Cyclic structure

  • The difference between the three cycles

    • for loop and while loop (judgment before execution)
    • do...while (execute first and then judge)
  • Difference between for and while:

    • As like as two peas, the for cycle and the while cycle are identical.
    • If the number of cycles is known, it is recommended to use the for loop. If it is not clear how many cycles to cycle, it is recommended to use the while loop.
    • In the for loop, the variables that control the loop can only be used in the loop. In a While loop, the variables that control the loop can continue to be used after the loop.
  • Dead cycle: the execution of the cycle will continue all the time. It will not stop without intervention.

for(;;) {
	System.out.println("Hello World");
}
// Classic practice
while(true) {
	System.out.println("Hello World");
}
do {
	System.out.println("Hello World");
} while (true);
  • Random number technique

    • Function: the technology used to obtain random numbers in the program.
  • Use steps:

    1. Guide Package: tell the program to find random number technology in which package of JDK
    2. Write a line of code to get the random number object
    3. Call the function of random number to obtain the random number of 0-9
package com.itheima.random;
import java.util.Random;

public class Test {
	public static void main(String[] args) {
		Random r = new Random();
		int number = r.nextInt(10);
		System.out.println("Randomly generated: + number);
	} 
} 
  • be careful:
    • nextInt(n) function can only generate random numbers between 0 and n-1, excluding n.
    • How do Random numbers generate Random numbers between 65 – 91?
      1. 65 – 91 => (0 - 26)+ 65
      2. int number = r.nextInt(27) + 65;

Figure guessing game

  • Requirements:

    • Randomly generate a data between 1-100 to prompt the user to guess. The big guess prompt is too large and the small guess prompt is too small until the game is finished.
  • analysis:

    1. Randomly generate a data between 1-100
    2. Use the dead loop to let users constantly prompt users to guess. Guess the big prompt is too big, guess the small prompt is too small, and guess right to end the game.
public static void guessNum() {
    Scanner in = new Scanner(System.in);
    Random random = new Random();
    int trueNum = random.nextInt(100) + 1;
    while(true) {
        System.out.print("Please enter the number you guessed:(1 - 100)");
            int guessNum = in.nextInt();

            if (guessNum == trueNum) {
                System.out.println("Congratulations on your correct answer!");
                break;
            }else if (guessNum > trueNum) {
                System.out.println("Guess too big");
                System.out.println("You guessed wrong. Go on!");
            }else {
                System.out.println("Guess too small");
                System.out.println("You guessed wrong. Go on!");
            }
    }
}

Posted by keigowei on Tue, 23 Nov 2021 08:54:20 -0800