My Java Learning Notes: Order and Branch Structure

Keywords: Java less

Sequential structure:
Sequential structure refers to the order in which statements are arranged, and the order in which they are executed determines the order in which they are executed; for example, the following example generates an addition problem within 20 years.

import java.util.*;

public class Main {
		public static void main(String[] args) {
			Scanner kb=new Scanner(System.in);
			int a,b;
			a=(int)(Math.random()*20);//The random method in math class can generate random numbers between [0,1].
			b=(int)(Math.random()*20);
			System.out.printf("%d+%d=\n",a,b);
		}
	}

The execution results in an addition formula of less than 20:

The branching structure is divided into several types:

  • 1.if (conditional expression) statement A
  • 2.if (conditional expression) statement A; else statement B;
  • 3. Nested if statements
  • 4.switch (expression A){
    case Constant Expressions 1: Sentence Sequence 1; break;
    ......
    default: statement sequence n+1;
    }

1.if (conditional expression) statement A

import java.util.*;

public class Main {
	public static void main(String[] args) {
		Scanner kb=new Scanner(System.in);
		int a,b,t;
		a=(int)(Math.random()*20);
		b=(int)(Math.random()*20);
		if (a<b) {
			t=a;
			a=b;
			b=t;
		}
		System.out.printf("%d-%d=\n", a,b);
	}
}

The size of two numbers is judged by the branch statement of if, and a subtraction formula is generated. The results are as follows:
2.if (conditional expression) statement A; else statement B;

import java.util.*;

public class Main {
	public static void main(String[] args) {
		Scanner kb=new Scanner(System.in);
		int a,b,answer;
		a=(int)(Math.random()*20);
		b=(int)(Math.random()*20);
		System.out.printf("%d+%d=\n", a,b);
		answer=kb.nextInt();//Accept user input
		if(answer==a+b) {
			System.out.printf("Good!\n");//Return good if user input is correct
		}
		else {
			System.out.printf("Wrong!\n");
			System.out.printf("The right answer is %d\n",a+b);//Otherwise, return the correct answer.
		}
	}
}

The results are as follows:

3. Nested if statements
With a slight modification of if statement nesting, the functions of adding and subtracting and multiplying can be written together.

import java.util.*;
public class Main {
	public static void main(String[] args) {
		Scanner kb=new Scanner(System.in);
		int a,b,t,CorrectAnswer,answer,option;
		a=(int)(Math.random()*20);
		b=(int)(Math.random()*20);
		option=(int)(Math.random()*3);//Generate numbers of 0 or 1 or 2, using them to represent the branch of operation to be performed, respectively.
		if(option==0) {
			System.out.printf("%d+%d=\n", a,b);
			CorrectAnswer=a+b;
		}
		else {
			if(option==1) {
				if(a<b) {
					t=a;a=b;b=t;
				}
				System.out.printf("%d-%d=\n",a,b);
				CorrectAnswer=a-b;
			}
			else {
				System.out.printf("%d*%d=\n",a,b);
				CorrectAnswer=a*b;
			}
		}
		answer=kb.nextInt();
		if(answer==CorrectAnswer)
		{
			System.out.printf("Good!\n");
		}
		else {
			System.out.printf("Wrong!");
			System.out.printf("The Correct answer is %d\n", CorrectAnswer);
		}
	}
}//Random generation of an addition, subtraction or multiplication operation

The results are as follows:

4.switch statement
switch (expression A){
case Constant Expressions 1: Sentence Sequence 1; break;
......
default: statement sequence n+1;
}
The previous example of if nesting can also be slightly modified, using switch statements to complete the same function. It should be noted that,
1. The expressions in switch () brackets can be byte, short, int or char, but not the other four types.
2. The constant expression directly followed by case can only be a constant, not a variable, and the constants behind different cases are different.
3. Break is to jump out of a switch directly after a case runs. It is not necessary. According to the logic of the program, you can choose whether you need break or not.

import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Scanner kb=new Scanner(System.in);
		int a,b,t,CorrectAnswer,answer,option;
		a=(int)(Math.random()*20);
		b=(int)(Math.random()*20);
		option=(int)(Math.random()*3);//Generate numbers of 0 or 1 or 2, using them to represent the branch of operation to be performed, respectively.
		//The value of option is treated as an expression of switch
		switch(option) {
		case 0:
			System.out.printf("%d+%d=\n", a,b);
			CorrectAnswer=a+b; break;
		case 1:
			if(a<b) {
				t=a;a=b;b=t;
			}
			System.out.printf("%d-%d=\n",a,b);
			CorrectAnswer=a-b; break;
		default:  //It is equivalent to case 2:
			System.out.printf("%d*%d=\n",a,b);
			CorrectAnswer=a*b;
		}	
		answer=kb.nextInt();
		if(answer==CorrectAnswer){
			System.out.printf("Good!\n");
			}
		else {
			System.out.printf("Wrong!");
			System.out.printf("The Correct answer is %d\n", CorrectAnswer);
			}
		}
}//Random generation of switch writing for an addition, subtraction or multiplication operation

The results of operation are shown as follows:

Posted by tom92 on Fri, 30 Aug 2019 05:51:03 -0700