01 顺序结构
- Java的最基本的结构就是顺序结构
- 它是任何一个算法都离不开的一种基本算法结构
02 if选择结构
2.1 if单选择结构
public class Hello {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入");
String str = scanner.nextLine();
if(str.equals("hello")){
System.out.println(str);
}
System.out.println("end");
scanner.close();
}
}
2.2 if双选择结构
public class Hello {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入");
int num = scanner.nextInt();
if(num>60){
System.out.println("及格");
}else {
System.out.println("不及格");
}
scanner.close();
}
}
2.3 多选择结构
public class Hello {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入");
int num = scanner.nextInt();
if(num<=100 && num>=90){
System.out.println("A");
}else if(num<90 && num>=80){
System.out.println("B");
}else if(num<80 && num>=70){
System.out.println("C");
}else if(num<70 && num>=60){
System.out.println("D");
}else{
System.out.println("成绩不对");
}
scanner.close();
}
}
03 switch多选择结构
public class Hello {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入");
String str = scanner.nextLine();
switch (str){
case "fang":
System.out.println("fang");
break;
case "xiao":
System.out.println("xiao");
break;
default:
System.out.println("wu");
}
scanner.close();
}
}
- case穿透:如果没有break,则会按顺序执行,直到遇到break或者程序结束