Use the IF statement to determine
public class TestDemo { public static void main(String args[]) { double score = 90.0; // 定义变量 if (score > 60.0) { // 设置判断条件 System.out.println("及格了!"); } }}程序执行结果: 及格了!
Use If...else to judge
public class TestDemo { public static void main(String args[]) { double score = 30.0; // 定义变量 if (score > 60.0) { // 条件判断满足 System.out.println("及格了!"); } else { // 条件判断不满足 System.out.println("小白的成绩!"); } }}程序执行结果: 小白的成绩!
Use If...else If...else to judge
public class TestDemo { public static void main(String args[]) { double score = 91.0; // 定义变量 if (score < 60.0) { // 条件判断 System.out.println("小白的成绩!") ; } else if (score >= 60 && score <= 90) {// 条件判断 System.out.println("中等成绩") ; } else if (score > 90 && score <= 100) {// 条件判断 System.out.println("优秀成绩") ; } else { // 条件判断都不满足 System.out.println("你家的考试成绩这么怪异!") ; } }}程序执行结果: 优秀成绩
Switch
For multi-conditional judgments, use IF. Else If...else can be judged by a Boolean condition, and if it is a majority, it can be done via switch with the following syntax
switch(整数 | 字符 | 枚举 | String) { case 内容 : { 内容满足时执行 ; break ; } case 内容 : { 内容满足时执行 ; break ; } case 内容 : { 内容满足时执行 ; break ; } ... default : { 内容都不满足时执行 ; break ; }}
public class Testdemo {public static void main (String args[]) {int ch = 1; Switch (CH) {//judge the number Case 2: {//Determine if the content is 2 System.out.println ("content is 2"); Break } Case 1: {//Determine if the content is 1 System.out.println ("content is 1"); Break } Case 3: {//Determine if the content is 3 System.out.println ("content is 3"); Break } default: {//judgment does not satisfy System.out.println ("No matching content"); Break }}}} program execution result: content is 1
public class TestDemo { public static void main(String args[]) { String str = "HELLO"; switch (str) { // 判断的是字符串 case "HELLO": { System.out.println("内容是HELLO"); break; } case "hello": { System.out.println("内容是hello"); break; } case "mldn": { System.out.println("内容是mldn"); break; } default: { System.out.println("没有匹配内容"); break; } } }}程序执行结果: 内容是HELLO
Achieve 1 to 100 accumulation--using the while loop
public class TestDemo { public static void main(String args[]) { int sum = 0; // 保存总和 int current = 1; // 循环的初始化条件 while (current <= 100) { // 循环结束条件 sum += current; // 累加 current++; // 改变循环条件 } System.out.println(sum); }}程序执行结果: 5050
Use do. The while loop implements the additive operation
public class TestDemo { public static void main(String args[]) { int sum = 0; // 保存总和 int current = 1; // 循环的初始化条件 do { // 循环结束条件 sum += current; // 累加 current++; // 改变循环条件 } while (current <= 100); // 循环结束判断 System.out.println(sum); }}程序执行结果: 5050
1 to 100 cumulative with for loop
public class TestDemo { public static void main(String args[]) { int sum = 0; // 保存总和 // 设置循环初始化条件current,同时此变量作为累加操作使用 // 每次执行循环体前都要进行循环判断(current <= 100) // 循环体执行完毕后会自动执行“current++”改变循环条件 for (int current = 1; current <= 100; current++) { sum += current; // 循环体中实现累加操作 } System.out.println(sum); }}程序执行结果: 5050
Output multiplication Table
public class TestDemo { public static void main(String args[]) { for (int x = 1; x <= 9; x++) { // 控制循环的行数 for (int y = 1; y <= x; y++) {// 控制列数 System.out.print(x + "*" + y + "=" + (x * y) + "\t"); } System.out.println();// 换行 } }}1*1=1 2*1=2 2*2=4 3*1=3 3*2=6 3*3=9 4*1=4 4*2=8 4*3=12 4*4=16 5*1=5 5*2=10 5*3=15 5*4=20 5*5=25 6*1=6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=36 7*1=7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49 8*1=8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64 9*1=9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81
Loop control
Normally, as long as the loop is executed, the code of the loop body executes as long as the loop condition is met, but there are two loop-stop control statements in the program: Continue (exiting this loop), break (exiting the entire loop). Statements of this class tend to be judged in conjunction with branch statements when used.
Observe continue
public class TestDemo { public static void main(String args[]) { for (int x = 0; x < 10; x++) { if (x == 3) { continue; // 之后的代码不执行,直接结束本次循环 } System.out.print("x = " + x + "、"); } }}程序执行结果: x = 0、x = 1、x = 2、x = 4、x = 5、x = 6、x = 7、x = 8、x = 9、
Observe break
public class TestDemo { public static void main(String args[]) { for (int x = 0; x < 10; x++) { if (x == 3) { break; // 退出整个循环 } System.out.print("x = " + x + "、"); } }}程序执行结果: x = 0、x = 1、x = 2、
Java Foundation 2.5_ Program Logical Structure