Java Fundamentals 04 Branch Statement

Like you who love programming!
Learn SpringBoot actual combat course https://edu.csdn.net/course/detail/31433
Learn SpringCloud introductory course https://edu.csdn.net/course/detail/31451


Preface

Intelligent creatures will think: If XXX, I will be XXX, and artificial intelligence has also developed in this way. In this article, we begin to learn the branch judgment statement in the program.
Branch statements include:

  • if statement
  • if-else statement
  • Multiple if
  • Nested if
  • switch statement

if statement

Grammatical structures:

if(条件){
    语句;
}

When playing the game, if it is judged that if the opponent hero's HP is less than 100, then go to beat him, how to use the program?
Execution process: Insert picture description here
Exercise 1: Complete the above case

int hp = 80;
//判断血量是否小于100,就进行攻击
if(hp < 100){
	System.out.println("赶快冲去打他");
}

Exercise 2: Say the result of the following code
1) Do not add a semicolon after the parentheses

int n = 1220;
if(n < 100);{
	System.out.println("n < 100");
}

2) If there are multiple statements, they must be enclosed in braces

if(n < 1000)
	System.out.println("test1......");
	System.out.println("test2......");

3) Use == when making equality judgment, not =

boolean b1 = false;
boolean b2 = true;
if(b2 = b1){
	System.out.println("相同.");
}

Exercise 3: Define age and gender variables. If age is greater than or equal to 18 and gender is "female", output "female big eighteen change".
Note: When judging the value of the String variable for equality, the equals method should be used

if( age >= 18 && sex.equals("女")){
		System.out.println("女大十八变!");
}

if-else statement

Grammatical structures:

if(条件){
    语句1;
}else{
    语句2;
}

If the condition is satisfied, execute statement 1 after if, if not, execute statement 2 after else;

If the opponent's hero's HP is less than 100, he will chase him, otherwise he will hide in the grass.
Execution process:
Insert picture description here
Exercise 1: Complete the above case

Scanner input = new Scanner(System.in);
System.out.println("请输入对方的HP:");
int hp = input.nextInt();
if(hp < 100){
	System.out.println("我们过去打他");
}else{
	System.out.println("我们躲在草丛里");
}

Exercise 2: Enter the amount of bank deposits, if it exceeds 100w, output "I don't go to work!!!", otherwise output "Boss, 996 is okay"

Multiple if statements

Grammatical structures:

if(条件1){
	语句1;
}else if(条件2){
	语句2;
}else{
	缺省语句;
}

There are N conditions, judged in turn, as long as one condition is established, the statement after the condition is executed and then ends, otherwise the next condition is judged.

If the opponent's attack power exceeds 100, find 3 people to beat him, otherwise if it exceeds 50, 2 people will beat him.
flow chart:
Insert picture description here

Exercise 1: Complete the example above

//1、定义攻击力变量
int attack;
//2、输入攻击力
Scanner input = new Scanner(System.in);
System.out.println("请输入对方的攻击力:");
attack = input.nextInt();
//判断攻击力是否超过100
if(attack > 100){
	System.out.println("我们三个一起过去打他");
}else if(attack > 50){//否则如果超过50就2个人打他
	System.out.println("找两个一起过去打他");
}else{//没有50就和他单挑。
	System.out.println("我过去跟他单挑");
}

Exercise 2: Input test scores from the keyboard

int score = ....; 
score >=90时输出:"学霸实在牛" 
score >=80...学神要加油 
score >=70....学民好害羞 
score >=60....学弱打酱油 
score <60.... 学渣泪在流

Nested if statement

In a team battle, if the HP exceeds 200, you will join the battle. If the HP is less than 200, you will quit the battle. During the battle, if the mana is more than 300, you will increase your moves, and if the HP is less than 300, you will physically attack.

Grammatical structure:
in an if structure, completely embed another if structure

if(外部条件){
	if(内部条件){
		语句;
	}else{
		语句;
	}
}else{
	语句;
}

Execution process: when the external conditions are established, the internal conditions are judged
Insert picture description here

Exercise 1: Complete the example above

Scanner input = new Scanner(System.in);
System.out.println("输入血量:");
int hp = input.nextInt();
System.out.println("输入蓝量:");
int mp = input.nextInt();
//外层判断血量
if(hp > 200){
	//内层判断蓝量
	if(mp > 300){
		System.out.println("我放大招了!!");
	}else{
		System.out.println("我物理攻击!!");
	}
}else{
	System.out.println("兄弟们,我撤了");
}

Exercise 2: Enter gender and age. If the gender is "male", you can retire if the age is over 65, otherwise you can continue to work,
if the gender is "female", you can retire if the age is over 60, otherwise continue to work

Scanner input = new Scanner(System.in);
System.out.println("输入性别");
String sex = input.next();
System.out.println("输入年龄");
int age = input.nextInt();
//外层判断性别
if(sex.equals("男")){
	//判断男的退休年龄
	if(age >= 65){
		System.out.println("老爹爹你可以退休了");
	}else{
		System.out.println("兄弟你还是好好上班吧");
	}
}else if(sex.equals("女")){
	//判断女的退休年龄
	if(age >= 60){
		System.out.println("老婆婆你可以退休了");
	}else{
		System.out.println("美女你还是好好上班吧");
	}
}else{
	System.out.println("你的性别出错了");
}

switch structure

grammar:

switch(表达式或变量){
	case 值1:
		语句1;
	break;
	case 值2:
		语句2;
	break;
	....
	default:
		缺省语句;
}

Related rules of switch

  1. The type of expression or variable judged by switch: byte\short\int\char\String\enum (enumeration)
  2. Break can end the switch structure, break can be omitted, after the omission, the execution of the statement will continue to execute the next case
  3. The value after the case cannot be repeated
  4. The default statement can be omitted
  5. The order of the cases can be reversed

Comparison of switch and multiple if:

  1. The operating process of switch is the same as that of multiple ifs, and the switch syntax is more concise
  2. Switch can only be used to judge a single value, multiple ifs can judge a range

Exercise 1: Control the skills of the ice shooter through the skill index, input index 1, output ten thousand arrows, input index 2, output eagle hit the sky, input index 3, output the ultimate crystal arrow.

Scanner scanner = new Scanner(System.in);
System.out.println("输入寒冰MM的技能:");
int skill = scanner.nextInt();
switch(skill){
	case 1:
		System.out.println("万箭齐发");
		break;
	case 2:
		System.out.println("鹰击长空");
		break;
	case 3:
		System.out.println("水晶箭");
		break;
	default:
		System.out.println("按错了");
		break;
}

Exercise 2: Analog bank phone service, enter 1, prompt "bank card business"; enter 2, prompt "credit card business"; enter 3, prompt "business consulting"; enter 4, prompt "manual service", other numbers, prompt" Hang up"

End

Leave your homework, and finishing here means you have mastered today’s content.

  1. Completion code:
    Enter the mobile phone price
    and output "Go to buy iPhone" if it exceeds 5000, otherwise output "Android phone is fine"
  2. Input the weather temperature from the keyboard When the temperature
    is greater than 40, the output: "Everyone is cooked" When the
    temperature is between 30 and 40, the output: "We only need the air conditioner, watermelon and WIFI" When the
    temperature is between 20 and 30, the output: "The weather is cold, you can wave "When the
    temperature is less than 20, output: "wear more clothes to avoid catching a cold"
  3. Enter the month number and output a holiday in that month.
  4. Realize a simple lottery program.
    Generate a random number between 0 and 9,
    enter a number and compare it with the random number, and
    output whether you won the prize.
    The method of using random numbers:
    1 Add
    import java.util.Random in front of the definition class ;
    2 Get the random number code:
    Random rand = new Random();
    int number = rand.nextInt(10); //Get 0~ Random integer between 9

If you need to learn other Java knowledge, poke here ultra-detailed knowledge of Java Summary

Guess you like

Origin blog.csdn.net/u013343114/article/details/112272131