选择结构
思考以下问题:
常用的逻辑运算符及其作用?
请写出判断分数大于 60 并且分数小于 100 的表达式
if-else 选择结构执行的顺序是什么?
多重 if 选择结构的执行流程是怎样的?
if 选择结构书写规范有哪些?
通过下面学习笔记来解决上述问题
if else 结构
import java.util.Scanner;
public class Demo01 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入成绩:");
int score = scanner.nextInt();
if (score >= 90) {
System.out.println("优秀");
} else {
System.out.println("其它");
}
}
}
if 结构
import java.util.Scanner;
public class Demo02 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入成绩:");
int score = scanner.nextInt();
if (score >= 90) {
System.out.println("奖励");
}
System.out.println("程序结束");
}
}
if elseif else结构
public class Demo03 {
public static void main(String[] args) {
/* 注意
1. 多选一
2. 从上到下,逐一判断,一旦为真,不会往下判断
3. else if是可以不定项
*/
int score = 88;
if (score >= 90) {
System.out.println("优秀");
} else if (score >= 80) {
System.out.println("良好");
} else if (score >= 70) {
System.out.println("中等");
} else if (score >= 60) {
System.out.println("及格");
} else {
System.out.println("差");
}
}
}
if结构省略写法
public class Demo04 {
public static void main(String[] args) {
int score = 98;
if (score >= 90) System.out.println("优秀");
else if (score >= 80) System.out.println("良好");
else if (score >= 70) System.out.println("中等");
else if (score >= 60) System.out.println("及格");
else System.out.println("差");
}
}
与或非
public class Demo05 {
public static void main(String[] args) {
// java html,只要有一门成功达到90分,就能去动物园游玩。否则,在家里休息。
int java = 89, html = 90;
if (java>=90 || html>=90) System.out.println("去动物园游玩");
else System.out.println("在家休息");
}
}
幸运抽奖
import java.util.Scanner;
public class Demo06 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("> 幸运抽奖\n");
System.out.print("请输入4位会员号:");
int id = scanner.nextInt();
int baiwei = id / 100 % 10;
/*
Math.random() 产生一个左闭右开 [0.0~1.0) 之间的一个小数
目标产生0-9之间的数,一共10个
[0.0~1.0) * 10 -> [0.0~10.0) -> 强转 [0~10)
*/
int luck = (int)(Math.random()*10);
System.out.println("幸运数字是:" + luck);
if (baiwei == luck) System.out.println("奖励mp3一个。");
else System.out.println("谢谢惠顾。");
}
}
字符串比较
import java.util.Scanner;
public class Demo07 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入是否是会员(y/n):");
String yesno = scanner.next();
// == 用于基本数据类型(byte short int long float double char boolean)的比较
// 字符串的比较,采用字符串的equals()方法
if (yesno.equals("y")) {
System.out.println("是会员");
} else {
System.out.println("不是会员");
}
}
}
嵌套if
import java.util.Scanner;
public class Demo08 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入是否是会员(y/n):");
String yesno = scanner.next();
System.out.println("请输入购物金额:");
int money = scanner.nextInt();
double zk = 1.0; // 默认值,没有折扣
if (yesno.equals("y")) {
if (money >= 200) zk = 0.75;
else zk = 0.8;
} else {
if (money >= 100) zk = 0.9;
}
System.out.println("实际支付:" + money * zk);
}
}
switch结构
import java.util.Scanner;
public class Demo09 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入名次:");
int mingci = scanner.nextInt();
if (mingci == 1) System.out.println("夏令营");
else if (mingci == 2) System.out.println("笔记本一台");
else if (mingci == 3) System.out.println("移动硬盘");
else System.out.println("无");
// 使用switch的条件:等值判断
switch (mingci) {
case 1:
System.out.println("夏令营");
break;
case 2:
System.out.println("笔记本一台");
break;
case 3:
System.out.println("移动硬盘");
break;
default:
System.out.println("无");
break;
}
}
}
switch支持数据类型
public class Demo10 {
public static void main(String[] args) {
String mingci = "1";
/* switch可以使用哪几种数据类型
1. byte short int char
2. String JDK7开始才支持(目前我们使用的是JDK8)
3. enum 枚举(未来再说)
3. long float double不支持
*/
switch (mingci) {
case "1":
System.out.println("夏令营");
break;
case "2":
System.out.println("笔记本一台");
break;
case "3":
System.out.println("移动硬盘");
break;
default:
System.out.println("无");
break;
}
}
}
switch透传
public class Demo11 {
public static void main(String[] args) {
String mingci = "星期一";
// 透传现象的使用场景
switch (mingci) {
case "星期一":
case "星期三":
System.out.println("画画");
break;
case "星期二":
System.out.println("休息");
break;
case "星期四":
System.out.println("休息");
break;
case "星期五":
System.out.println("移动硬盘");
break;
case "星期六":
case "星期天":
System.out.println("街舞");
break;
default:
System.out.println("错误值");
break;
}
}
}
商品购换
import java.util.Scanner;
public class Demo12 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入消费金额:");
double money = scanner.nextInt();
System.out.println("是否换购:");
System.out.println("1.满50元,加2元换购商品1");
System.out.println("2.满100元,加3元换购商品2");
System.out.println("3.满100元,加10元换购商品3");
System.out.println("4.满200元,加10元换购商品4");
System.out.println("5.满200元,加20元换购商品5");
System.out.println("0.不换购");
System.out.print("请选择:");
int select = scanner.nextInt();
String goods = "不换购"; // 全选中good单词,右键菜单Refactor下的rename
switch (select) {
case 1:
if (money > 50) {
money += 2;
goods = "商品1";
}
break;
case 2:
if (money > 100) {
money += 3;
goods = "商品2";
}
break;
case 3:
if (money > 100) {
money += 10;
goods = "商品3";
}
break;
case 4:
if (money > 200) {
money += 10;
goods = "商品4";
}
break;
case 5:
if (money > 200) {
money += 20;
goods = "商品5";
}
break;
}
System.out.println("消费金额:" + money);
System.out.println("成功换购:" + goods);
}
}