从上到下依次执行
用于判断是否可行
if(布尔表达式){ //如果布尔表达式为true将执行的语句 }
e.g.
package structure; import java.util.Scanner; public class ifDemo01 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("请输入内容:"); String s = scanner.nextLine(); //输完scanner.nextLine();Alt+Enter //equals:判断字符串是否相等 此处不能用== if (s.equals("Hello")){ System.out.println(s); } System.out.println("End"); scanner.close(); } }
if(boolean){ //if boolean = true }else{ //if booolean = false }
package structure; import java.util.Scanner; public class Demo02 { public static void main(String[] args) { //score>60 pass;<60 fail Scanner scanner = new Scanner(System.in); System.out.println("Please enter the score:"); int score = scanner.nextInt(); if (score>60){ System.out.println("PASS"); }else{ System.out.println("FAIL"); } scanner.close(); } }
if(boolean 1){ //if boolean 1 = true }else if(boolean 2){ //if boolean 2 = true }else if(boolean 3){ //if boolean 3 = true }else{ //if all of these ?= true }
package structure; import java.util.Scanner; public class Demo03 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("PLease enter the score:"); int socre = scanner.nextInt(); if (socre==100){ System.out.println("Congratulations!"); }else if (socre<100 && socre>=90){ System.out.println("A"); }else if (socre<90 && socre>=75){ System.out.println("B"); }else if (socre<75 && socre>=60){ System.out.println("C"); }else if (socre<60 && socre>=0){ System.out.println("D"); }else{ //别漏了这个! System.out.println("Null!"); } scanner.close(); } }
if(boolean 1){ //if bollean 1 = true if(boolean 2){ //if boolean 2 = true } }
switch(expression){ case value: //statements break;//可选 case value: //statements break;//可选 //你可以有任意数量的case语句 default://可选 //statements }
package structure; public class switchDemo04 { public static void main(String[] args) { //case穿透 //switch匹配一个具体的值 char grade = 'B'; switch (grade){ case'A': System.out.println("EXCELLENT!"); break;//不加break就会全部输出 case 'B': System.out.println("GOOD!"); break; case 'C': System.out.println("PASS"); break; case 'D': System.out.println("FAIL"); break; default: System.out.println("UNKNOWN"); } } }
反编译——字符的本质是数字 看源码
while(布尔表达式){ //循环内容 }
package structure; public class whileDemo05 { public static void main(String[] args) { int i = 0; while (i<100){ i++; System.out.println(i); } } }
package structure; public class whileDemo06 { public static void main(String[] args) { //calculate 1+2+3+...+100=? int i = 0; int sum = 0; while (i<=100){ sum = sum + i; i++; } System.out.println(sum); } }
do{ //code statements }while(布尔表达式);
while先判断后执行,dowhile先执行后判断
for(初始化;布尔表达式;更新){ //statements }
package structure; public class ForDemo07 { public static void main(String[] args) { int a = 1;//初始化条件 while (a<=100){//条件判断 System.out.println(a); a+=2;//迭代 } System.out.println("while循环结束"); //初始化 条件判断 迭代 for (int i = 1;i<=100;i++){ System.out.println(i); } //快捷键:100.for System.out.println("For循环结束"); } }
for(; ; ){ //死循环 }
package structure; public class ForDemo08 { public static void main(String[] args) { //calculate the sum of odd and even between 0 and 100 int oddSum = 0; int evenSum = 0; for (int i = 0; i <= 100; i++) { if (i%2!=0){ oddSum+=i; }else{ evenSum+=i; } } System.out.println("The sum of odd:"+oddSum); System.out.println("The sum of even:"+evenSum); } }
package structure; public class ForDemo09 { public static void main(String[] args) { /*print the number that can be divided by 5 between 1 and 1000 print 3 of them every line */ for (int i = 0; i <= 1000; i++) { if (i%5==0){ System.out.print(i+"\t"); } if (i%(5*3)==0){ System.out.println(); //System.out.print("\n") } } } }