Java basics: flow control (branch flow if branch and switch branch)

1.1 Process Control

The execution flow and execution sequence of the command procedure;

1.1.1 Classification of Process Control

I. Sequence flow

The program is executed line by line;

During the execution of the program, different strategies may be adopted according to different business situations. Execute different code flows;

III. Circulation process

It is hoped that some code can be executed back and forth through a process without the need to repeatedly write these codes;

1.2 Branching Process

1.2.1 switch branch

  JDK17 switchUsage of zhuanlan.zhihu.com/p/408799706

switch(month){
    case 12,1,2->System.out.println("你就像那冬天里的一把火");	
    case 3,4,5->System.out.println("春天在哪里呀!春天在哪里~");
    case 6,7,8->System.out.println("夏天夏天悄悄过去 留下小秘密");
    case 9,10,11->System.out.println("就让秋风带走我的思念 ");
}
复制代码
//测试switch语法结构
public class SwitchTest{
	public static void main(String[] args) {
		
		//声明一个变量 这个变量存储月份1-12, 通过存储的变量的值判定当前月份所属的季节

		//1:声明变量存储月份
		char month = 5;

		//2:通过switch去完成
		switch(month){
			case 12:
			case 1:
			case 2:
				System.out.println("你就像那冬天里的一把火");	break;
			case 3:	
			case 4:	
			case 5:
				System.out.println("春天在哪里呀!春天在哪里~");break;
			case 6:				
			case 7:				
			case 8:
				System.out.println("夏天夏天悄悄过去 留下小秘密");break;
			case 9:				
			case 10:				
			case 11:
				System.out.println("就让秋风带走我的思念 ");break;
			default:
				System.out.println("您的输入有误。。。");break;	
		}


		System.out.println("GAME OVER");


	}
}
复制代码
I. Grammatical structure
switch(expression){
    case value1:
        statement1;break;
    case value2:
        statement2;break;
   	case value3:
        statement3;break;
    ............. 
    default:
        statement3;[break;]
}
复制代码
II. Execution order
  • evaluate the value of an expression
  • Match the corresponding case in turn. If the match is successful, the corresponding execution statement is executed;
  • Be sure to make sure to break later and jump out of the entire switch structure;
  • If all cases are not satisfied, default is executed, if it exists;
III. Precautions
  • The result of the expression can be converted to int through automatic type conversion, and only four types of basic data types can be used: byte, short, int, and char;
  • JDK1.5After that, enumeration types are supported Enum, JDK1.7and String strings are supported later;

1.2.2 if branch structure

// Math.random() 指定区间的随机数(整数)  [1,8]  [min,max]  
// (int)(Math.random()*(max-min+1)+min)
复制代码
I, single branch structure
//测试if单分支
public class IfTest01{
	public static void main(String[] args) {
			

		// 生成一个随机数 查看随机数是否大于50 大于则显示随机数的结果反之则结束

		double random = Math.random(); //[0-1)随机小数
		System.out.println(random);


		// Math.random() 指定区间的随机数(整数)  [1,8]  [min,max]  
		// (int)(Math.random()*(max-min+1)+min)

		// 产生一个[45,80]
		int num = (int)(Math.random()*36+45);
		System.out.println(num);


		//判定
		if(num>50){
			System.out.println("产生的数字大于50,数字是:"+num);
		}


		System.out.println("GAME OVER");
	}
}
复制代码
A. Grammatical structure
if(expression){
    statement;
}
复制代码
B. The order of execution
  • Determine the result of expression if the result is true, execute the corresponding execution statement; end the entire if single branch
  • If the result of the expression is false, skip the if single branch and execute the subsequent code
C. Precautions
  • The branch is relatively simple and may not be able to fully use the more complex business;
// 声明一个变量
boolean flag = true;

if(flag=false){
    System.out.println("aa");
}

System.out.println("bb");
复制代码
II. Double branch structure
//测试if双分支
public class IfTest02{
	public static void main(String[] args) {
			
		// 判定某个数字的奇偶性

		// 1: 随机生成一个数字 [20,56]
		int random = (int)(Math.random()*37+20);

		System.out.println("产生的随机数是:"+random);



		//2: 判定奇偶性
		//boolean flag = (random%2)==0;
		/*if((random%2)==0){
			System.out.println("产生的随机数是:"+random+",它是一个偶数");
		}else{ //flag = false
			System.out.println("产生的随机数是:"+random+",它是一个奇数");
		}*/

		String msg = (random&1)==1?"奇数":"偶数";

		//System.out.println(msg);
		System.out.println("产生的随机数是:"+random+",它是一个"+msg);


		System.out.println("GAME OVER");
		
	}
}
复制代码
A. Grammatical structure
if(expression){
    statement1;
}else{
    statement2;
}
复制代码
B. The order of execution
  • Determine the result of expression if the result is true, execute the corresponding execution statement statement1; end the entire if double branch
  • If the result of the expression is false, execute statement2, then skip the if double branch and execute the subsequent code
C. Precautions
  • The branch is relatively simple and may not be able to fully use the more complex business;
  • The if-else in the double branch must be executed one;
III. Multi-branch structure
//测试if多分支

// 分别使用if和switch 来完成方向控制
// 45上 46右 47下 48左 

public class IfTest03{
	public static void main(String[] args) {
			
		// 随机生成一个分数 判定分数的等级
		// 100 NB  [90-100) 优秀  [80-90) 良好  [70-80) 还行  [60-70) 勉强 

		//1:生成一个随机数
		int score = (int)(Math.random()*92+9);
		System.out.println("生成的分数是:"+score);

		if(score==100){
			System.out.println("NB");
		}else if(score>=90 && score<100){
			System.out.println("优秀");	
		}else if(score>=80 && score<90){
			System.out.println("良好");	
		}else if(score>=70 && score<80){
			System.out.println("还行");	
		}else if(score>=60 && score<70){
			System.out.println("勉强");	
		}else{
			System.out.println("男女混合双打");
		}

		System.out.println("GAME OVER");

	}
}
复制代码
A. Grammatical structure
if(expression1){
    statement1;
}else if(expression2){
    statement2;
}else if(expression3){
    statement3;
}..........{
    
}[else{    statementN;}]
复制代码
B. The order of execution
  • expression1If the result of the judgment is true, execute the corresponding execution statement statement1; end the entire if multi-branch
  • If the result of the expression is false, the result of the judgment expression2, if the result is true, execute statement2, then skip the if multi-branch and execute the subsequent code
  • and so on
  • Until all the results are not satisfied, if there is an else in the code, execute the content of the else
C. Precautions
  • Multiple branches may cause the code to be too bloated, so it is generally not recommended to have more than 3 branches;
  • When writing conditions, the decision efficiency of the entire branch can be appropriately increased;
  • If does interval judgment, switch is equivalent judgment, and they can communicate with each other;

Guess you like

Origin juejin.im/post/7135776606500421640