public class 顺序Dem0 {
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");
//从上到下依次执行
}
}
选择结构
我们很多时候需要判断一个东西是否可行,然后我们采取执行,这样一个过程在程序中用if语句来表示
public class If单Deom01 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入一个内容:");
final String s = scanner.nextLine();
//equals:判断字符串是否相等
if (s.equals("Hello")){
System.out.println(s); //如果输入的内容是Hello 则输出Hello
}
System.out.println("SB"); //否则输出SB
scanner.close();
}
}
if双选择结构
public class If双选择Demo02 {
public static void main(String[] args) {
// 考试分数大于60就是及格,小于60分就不及格。
Scanner scanner =new Scanner(System.in);
System.out.println("请输入成绩:");
final int score = scanner.nextInt();
if (score>60){
System.out.println("及格");
}else {
System.out.println("不及格");
}
scanner.close();
}
}
if多选择结构
public class If多选择Demo03 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
/*if语句中,至多有一个else 语句,else 语句在所有的else if 语句之后。
if语句中,可以有很多 else if语句,但是必须要在else 语句之前。
一旦其中一个else if语句为 true,其他的else if语句和else 语句都将跳过执行。
*/
System.out.println("请输入成绩:");
final int score = scanner.nextInt();
if (score == 100) {
System.out.println("NB");
} else if (score >= 90 && score < 100) {
System.out.println("优秀");
} else if (score >= 70 && score < 90) {
System.out.println("良好");
} else if (score >=60 && score < 70) {
System.out.println("一般");
}else if (score >=0 && score < 60) {
System.out.println("不及格");
}else {
System.out.println("成绩不合法");
}
scanner.close();
}
}
嵌套的if结构
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-HNE3GkEA-1653918412804)(C:\Users\wuhaibo\AppData\Roaming\Typora\typora-user-images\1653914202117.png)]
switch多选择结构
public class SwitchDemo01 {
public static void main(String[] args) {
// case穿透(无break时,输出命令后的所有结果);
// switch 匹配一个具体的值
char grade='E'; //需要输出的命令。(可以为任何值 )
switch (grade) {
case 'A':
System.out.println("优秀"); //定义A为优秀
break; //可选,代表终止命令。
case 'B':
System.out.println("良好"); //定义B为良好
break; //可选,代表终止命令。
case 'C':
System.out.println("及格"); //定义C为及格
break; //可选,代表终止命令。
case 'D':
System.out.println("再接再厉"); //定义D为再接再厉
break; //可选,代表终止命令。
case 'E':
System.out.println("挂科"); //定义E为挂科
break;//可选,代表终止命令。
default:
System.out.println("未知等级"); //输出结果不在范围内,定义为未知等级
}
}
}