In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the relevant knowledge of "what are the process control statements of Java". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
01, related to if-else
1) if statement
The format of the if statement is as follows:
If (Boolean expression) {/ / if the condition is true, execute this code}
Draw a flow chart to show:
Let's write an example:
Public class IfExample {public static void main (String [] args) {int age = 20; if (age)
< 30) { System.out.println("青春年华"); } } } 输出: 青春年华 2)if-else 语句 if-else 语句的格式如下: if(布尔表达式){ // 条件为 true 时执行的代码块 }else{ // 条件为 false 时执行的代码块 } 画个流程图表示一下:Let's write an example:
Public class IfElseExample {public static void main (String [] args) {int age = 31; if (age)
< 30) { System.out.println("青春年华"); } else { System.out.println("而立之年"); } } } 输出: 而立之年 除了这个例子之外,还有一个判断闰年(被 4 整除但不能被 100 整除或者被 400 整除)的例子: public class LeapYear { public static void main(String[] args) { int year = 2020; if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) { System.out.println("闰年"); } else { System.out.println("普通年份"); } } } 输出: 闰年 如果执行语句比较简单的话,可以使用三元运算符来代替 if-else 语句,如果条件为 true,返回 ? 后面 : 前面的值;如果条件为 false,返回 : 后面的值。 public class IfElseTernaryExample { public static void main(String[] args) { int num = 13; String result = (num % 2 == 0) ? "偶数" : "奇数"; System.out.println(result); } } 输出: 奇数 3)if-else-if 语句 if-else-if 语句的格式如下: if(条件1){ // 条件1 为 true 时执行的代码 }else if(条件2){ // 条件2 为 true 时执行的代码 } else if(条件3){ // 条件3 为 true 时执行的代码 } ... else{ // 以上条件均为 false 时执行的代码 } 画个流程图表示一下: 来写个示例: public class IfElseIfExample { public static void main(String[] args) { int age = 31; if (age < 30) { System.out.println("青春年华"); } else if (age >= 30 & & age
< 40 ) { System.out.println("而立之年"); } else if (age >= 40 & & age
< 50 ) { System.out.println("不惑之年"); } else { System.out.println("知天命"); } } } 输出: 而立之年 4)if 嵌套语句 if 嵌套语句的格式如下: if(外侧条件){ // 外侧条件为 true 时执行的代码 if(内侧条件){ // 内侧条件为 true 时执行的代码 } } 画个流程图表示一下: 来写个示例: public class NestedIfExample { public static void main(String[] args) { int age = 20; boolean isGirl = true; if (age >= 20) {if (isGirl) {System.out.println ("legal age of marriage for girls");}
Output:
Legal marriage age for girls
02, switch statement
The switch statement is used to determine the equality between variables and multiple values. Variables can be of type byte, short, int, long, or corresponding wrapper types Byte, Short, Integer, Long, as well as strings and enumerations.
Take a look at the format of the switch statement:
Switch (variable) {case optional value 1: / / code executed after matching optional value 1; break; / / this keyword is the code executed after matching optional case optional value 2: / / optional value 2; break; / / this keyword is optional. Default: / / the keyword is optional / / the code executed after all optional values do not match}
Variables can have 1 or N values.
The value type must be consistent with the variable type, and the value must be determined.
The value must be unique and cannot be repeated, or the compilation will go wrong.
The break keyword is optional, if not, the next case is executed, and if there is, the switch statement is jumped out.
The default keyword is also optional.
Draw a flow chart:
Here's an example:
Public class Switch2 {public static void main (String [] args) {int age = 20; switch (age) {case 20: System.out.println ("go to school"); break; case 24: System.out.println ("Suzhou work"); break Case 30: System.out.println ("Luoyang work"); break; default: System.out.println ("unknown"); break; / / can be omitted}
Output:
Go to school
When the two values have the same code to execute, you can write the code to be executed in the next case statement, but there is nothing in the previous case statement. Take a look at the example:
Public class Switch3 {public static void main (String [] args) {String name = "Silent King II"; switch (name) {case "James": System.out.println ("basketball player"); break; case "Mourinho": System.out.println ("football coach") Break; case "Silence King II": case "Silence King III": System.out.println ("Table Tennis enthusiast"); break; default: throw new IllegalArgumentException ("name has no match");}
Output:
Table tennis enthusiast
Enumerations are also common as variables in switch statements, so take a look at the example:
Public class SwitchEnumDemo {public enum PlayerTypes {TENNIS, FOOTBALL, BASKETBALL, UNKNOWN} public static void main (String [] args) {System.out.println (createPlayer (PlayerTypes.BASKETBALL));} private static String createPlayer (PlayerTypes playerType) {switch (playerType) {case TENNIS: return tennis player Roger Federer Case FOOTBALL: return "football player Cristiano Ronaldo"; case BASKETBALL: return "basketball player James"; case UNKNOWN: throw new IllegalArgumentException ("unknown"); default: throw new IllegalArgumentException ("player type:" + playerType) }
Output:
Basketball player James
03. For cycle
1) ordinary for cycle
A normal for loop can be divided into four parts:
1) initial variable: the initial condition when the loop begins to execute.
2) condition: the condition to be judged each time the loop executes. If it is true, the loop body is executed; if it is false, it jumps out of the loop. Of course, the condition is optional, and if there is no condition, it will loop all the time.
3) Loop body: the block of code to be executed each time the loop is executed until the condition becomes false.
4) self-increasing / self-decreasing: the way to recognize the change of variables for the first time.
Take a look at the format of a normal for loop:
For (initial variable; condition; self-increasing / self-decreasing) {/ / cyclic body}
Draw a flow chart:
Here's an example:
Public class ForExample {public static void main (String [] args) {for (int I = 0; I < 5; iTunes +) {System.out.println ("Silent King is so beautiful");}
Output:
The Silence King is so beautiful! the Silence King is so beautiful.
"Oh, second brother, you really praise me in a different way."
"No, no, no."
"all right!"
"take a good look."
Public class PyramidForExample {public static void main (String [] args) {for (int I = 0; I < 5; iTunes +) {for (int j = 0 TJ)
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.