Control sentence selection structure

3.1 Selection structure
3.1.1 if single selection structure
Flow chart:
Insert picture description here
1. The use of random () in math
double variable name = Math.random (); // returns a random number in the range of [0, 1)
2. Programming habits
If you can execute the first statement without writing braces, add braces when writing if to enhance the readability of the program.


/**
 * 测试if单选择结构
 * @author 14323
 *
 */
public class TestIf {
	public static void main(String[] args){
		double d = Math.random();
		int e = (int)(6*d)+1;
		System.out.println(e);
		if(e<=3) {
			System.out.println("小");
		}
	
		System.out.println("#################");
		//通过掷三骰子看看今天的手气如何
		int i = (int)(6*Math.random()+1);
		System.out.println("第一个骰子的点数是:"+i);
		int j = (int)(6*Math.random()+1);
		System.out.println("第二个骰子的点数是:"+j);
		int k = (int)(6*Math.random()+1);
		System.out.println("第三个骰子的点数是:"+k);
		int count = i+j+k;
		//如果三个骰子之和大于15,则手气还不错
		if(count>15) {
			System.out.println("今天手气不错");
		}
		//如果三个骰子之和在10到15之间,则手气一般
		if(count<=15&&count>=10) {
			System.out.println("今天手气一般");
		}
		//如果三个骰子之和小于10,则手气不好
		if(count<10) {
			System.out.println("今天手气不好");
		}
		System.out.println("得了"+count+"分");
	}
}

3.1.2 if-else double selection structure
Flow chart of structure:
Insert picture description here
1. Mathematical function: mathematical square function: Math.pow (variable name, power)
pi: Math.PI
2. Example of if-else application

import java.math.*;

/**
 * 测试if—else双选择结构
 * @author 14323
 *
 */

public class TestIfElse {
	public static void main(String[] args){
		int h = (int)(6*Math.random());
		System.out.println(h);
		if(h<=3){
			System.out.println("小");
		}else {
			System.out.println("大");
		}
		
		System.out.println("##############");
		
		//随机产生一个[0,4)区间的半径,根据半径求圆的面积和周长
		double r = 4*Math.random();
		
		//求圆的面积,Math.pow(r,2),求半径r的平方
		double area = Math.PI*Math.pow(r,2);
		//求圆的周长
		double circle = 2*Math.PI*r;
		System.out.println("半径为:"+r);
		System.out.println("面积为:"+area);
		System.out.println("周长为:"+circle);
		//进行比较
		if(area>=circle){
			System.out.println("面积大于周长");
		}else{
			System.out.println("面积小于周长");
		}	
	}
}

3.1.3 if-else if-else multi-select structure
1, structure flow:
Insert picture description here
2, application examples

/**
 * 测试多选择结构
 * @author 14323
 *
 */
public class TestIfElseIfElse {
	public static void main(String[] args){
		int age = (int)(100*Math.random());//岁数范围是[0,99]
		System.out.println("年龄是"+age);
		if(age<15){
			System.out.println("属于儿童,喜欢玩!");
		}else if(age<25){
			System.out.println("属于青年,要学习!");
		}else if(age<45){
			System.out.println("属于中年,要工作!");
		}else if(age<65){
			System.out.println("属于中老年,要补钙!");
		}else if(age<85){
			System.out.println("属于老年,多运动!");
		}else{
			System.out.println("属于老寿星,古来稀!");
		}
	}
}

3.1.4 Switch multi-select structure
1. It is generally used to make multi-value judgments. The process structure is as shown in the figure.
Insert picture description here
2. Break statement: When the switch encounters enough break, it will jump out of the switch selection structure.

/**
 * 测试switch语句
 * @author 14323
 *
 */

public class TestSwitch {
	public static void main(String[] args) {
		char c = 'a';
		int rand = (int)(26*Math.random());
		char c2 = (char)(c+rand);
		System.out.println(c2+":");
		switch(c2) {
		case 'a':
		case 'e':
		case 'i':
		case 'o':
		case 'u':
			System.out.println("元音");
			break;//从表达式对应值处开始执行,只要不碰到break,则会执行到末尾
		case 'y':
		case 'w':
			System.out.println("半元音");
			break;
		default:
			System.out.println("辅音");
		}
	}
}

3. After jdk1.7, case operation strings are allowed.

Published 6 original articles · praised 3 · visits 196

Guess you like

Origin blog.csdn.net/weixin_43855187/article/details/105570182