一、顺序结构
java默认就是顺序结构执行语句,即从上往下,一句一句依次执行。
示例1:
public class Demo01 {
public static void main(String[] args) {
System.out.println("Hello1");
System.out.println("Hello2");
System.out.println("Hello3");
System.out.println("Hello4");
System.out.println("Hello5");
System.out.println("Hello6");
System.out.println("Hello7");
}
}
二、选择结构
1、if单选择结构
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();
if (s.equals("Hello")){
System.out.println("Pass!");
}
System.out.println("End!");
scanner.close();
}
}
2、if双选择结构
import java.util.Scanner;
public class ifDemo02 {
public static void main(String[] args) {
//考试分数大于60分就及格,小于60分不及格
Scanner scanner = new Scanner(System.in);
System.out.println("请输入分数:");
int score = scanner.nextInt();
if(score>60){
System.out.println("及格!");
}
else {
System.out.println("不及格!");
}
scanner.close();
}
}
3、if多选择结构
import java.util.Scanner;
public class ifDemo03 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入分数:");
int score = scanner.nextInt();
if(score == 100){
System.out.println("恭喜满分!");
}
else if (score<100 && score>=90) {
System.out.println("A级");
}
else if (score<90 && score>=80) {
System.out.println("B级");
}
else if (score<80 && score>=70) {
System.out.println("C级");
}
else if (score<70 && score>=60) {
System.out.println("D级");
}
else if (score<60 && score>=0){
System.out.println("不及格!!!");
}
else {
System.out.println("输入不合法!");
}
scanner.close();
}
}
3、switch多选择结构
public class SwitchDemo01 {
public static void main(String[] args) {
char grade = 'C';
switch (grade){
case 'A':
System.out.println("优秀");
break;
case 'B':
System.out.println("良好");
break;
case 'C':
System.out.println("不错");
break;
case 'D':
System.out.println("再接再厉");
break;
case 'E':
System.out.println("挂科");
break;
}
}
}