选择结构
- if单选择结构
- if双选择结构
- if多选择结构
- 嵌套的if结构
- switch多选择结构
if单选择结构
-
我们很多时候需要去判断一个东西是否可行,然后我们才去执行,这样一个过程在程序中用if语
句来表示 -
语法:
if(布尔表达式){ //如果布尔表达式为true将执行的语句 }
案例:if单选择结构
public class Name1 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入内容:");
String s = scanner.nextLine();
//equals()判断字符串是否相等
if (s.equals("Hello")){
System.out.println(s);
}
System.out.println("End");
//凡是属IO流的类如果不关闭会一直占用资源.要养成好习惯用完就关掉
scanner.close();
}
}
if双选择结构
-
那现在有个需求,公司要收购一个软件,成功了,给人支付100万元,失败了,自己找人开发。
这样的需求用一个if就搞不定了,我们需要有两个判断,需要一个双选择结构, 所以就有了if - else结构。 -
语法:
if(布尔表达式){
//如果布尔表达式的值为true
}else{
//如果布尔表达式的值为false
}
案例:if双选择结构
public class Name1 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
//考试分数大于60就是及格,小于60分就不及格。
System.out.println("请输入成绩: ");
int score = scanner.nextInt();
if (score > 60) {
System.out.println("及格");
} else {
System.out.println("不及格");
}
//凡是属IO流的类如果不关闭会一直占用资源.要养成好习惯用完就关掉
scanner.close();
}
}
if多选择结构
-
我们发现刚才的代码不符合实际情况,真实的情况还可能存在ABCD,存在区间多级判断。比如90-100就是A, 80-90就是B…等等,在生活中我们很多时候的选择也不仅仅只有两个,所以我们需要一个多选择结构来处理这类问题!
-
语法:
if(布尔表达式1){
//如果布尔表达式1的值为true执行代码
}else if(布尔表达式2){
//如果布尔表达式2的值为true执行代码
}else if( 布尔表达式3){
//如果布尔表达式3的值为true执行代码
}else {
//如果以上布尔表达式都不为true执行代码
}
- 注意:
if 语句至多有 1 个 else 语句,else 语句在所有的 else if 语句之后。
if 语句可以有若干个 else if 语句,它们必须在else,语句之前。
一旦其中一个 else if 语句检测为 true, 其他的 else if 以及 else 语句都将跳过执行。
案例:if多选择结构
public class Name1 {
public static void main(String[] args) {
//考试分数100恭喜满分,90-100就是A, 80-90就是B...等等 0-60就是不及格
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();
}
}
案例:if判断成绩分数
public class Name1 {
public static void main(String[] args) {
Scanner a = new Scanner(System.in);//用户输入 Scanner
System.out.println("请输入数字");
int b = a.nextInt();
if (b >= 60 && b < 70) {
System.out.println("及格");
} else if (b >= 70 && b < 80) {
System.out.println("还行");
} else if (b >= 80 && b < 90) {
System.out.println("良好");
} else if (b >= 90 && b < 100) {
System.out.println("非常棒");
} else if (b >= 30 && b < 60) {
System.out.println("很差");
} else {
System.out.println("非常差");
}
}
}
嵌套的if结构
-
使用嵌套的 if…else 语句是合法的。也就是说你可以在另一个 if 或者 else if 语句中使用 if 或
者 else if 语句。你可以像 if 语句一样嵌套 else if…else -
语法:
if(布尔表达式 1){
//如果布尔表达式1 的值为true执行代码
if(布尔表达式 2){
//如果布尔表达式2 的值为true执行代码
}
}
switch多选择结构
-
多选择结构还有一个实现方式就是 switch case 语句。
-
switch case 语句判断一个变量与一系列值中某个值是否相等,每个值称为一个分支。
switch(expression){
case value:
//语句
break; //可选
case value:
//语句
break; //可选
//你可以有任意数量的case语句
default: //可选
//语句![请添加图片描述](https://img-blog.csdnimg.cn/0687560874dc4bef85460605bd8fee2f.png)
}
- switch 语句中的变量类型可以是:
- byte、 short,、int 或者char.
- 从JavaSE7开始
- switch 支持字符串 String 类型了
- 同时 case 标签必须为字符串常量或字面量。
public class Name1 {
public static void main(String[] args) {
//case 穿透 switch 匹配一个具体的值
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; //可选
default: //可选
System.out.println("未知等级");
}
}
}
案例:switch 语句 字符串判断
public static void main(String[] args) {
//case 穿透 switch 匹配一个具体的值
//JDK7的新特性,表达式结果可以是字符串! ! !
//字符的本质还是数字
//反编译java---class (字节码文件) ---- 反编译(IDEA)
//Scanner scanner =new Scanner(System.in);
//String a = scanner.nextLine();
String name = "恰北北";
switch (name) {
case "靠北北":
System.out.println("谁呀?");
break;
case "恰北北":
System.out.println("真美");
break;
default:
System.out.println("弄啥勒!");
}
}
案例:与class文件对比