10. 分支语句嵌套
【摘要】 Java系列第十课
1、多分支if嵌套
2、多分支switch嵌套
3、练习题1
代码如下:
import java.util.Scanner;
public class Demo9 {
public static void main(String[] args) {
Scanner superman = new Scanner(System.in);
int time;
System.out.println("你的跑步成绩为:");
time = superman.nextInt();
if (time <= 50){
System.out.println("恭喜你进入决赛");
String sex;
System.out.println("你的性别是:");
sex = superman.next();
switch (sex){
case "男":
System.out.println("你被分到了男子组");
break;
case "女":
System.out.println("你被分到了女子组");
break;
}
}else{
System.out.println("你被淘汰了!");
}
System.out.println("-----------* __ * -----------");
}
}
结果是:
4、练习2:计算你的真实年龄
代码如下:
import java.util.Scanner;
public class Demo1{
public static void main(String[] args){
Scanner superman = new Scanner(System.in);
int year;
int month;
int day;
int nowYear;
int nowMonth;
int nowDay;
System.out.println("请输入出生年份:");
year = superman.nextInt();
System.out.println("请输入出生月份:");
month = superman.nextInt();
System.out.println("请输入出生日子:");
day = superman.nextInt();
System.out.println("请输入当前年份:");
nowYear = superman.nextInt();
System.out.println("请输入当前月份:");
nowMonth = superman.nextInt();
System.out.println("请输入当前日子:");
nowDay = superman.nextInt();
if (nowYear > year){
if (nowMonth > month){
System.out.println("年龄:" + (nowYear - year));
}else if (nowMonth < month){
System.out.println("年龄:" + (nowYear - year - 1));
}else{
if (nowDay > day){
System.out.println("年龄:" + (nowYear - year));
}else{
System.out.println("年龄:" + (nowYear - year - 1));
}
}
}else{
System.out.println("您输入的年份有误");
}
}
}
结果如下:
5、练习3
思路如下:
代码如下:
import java.util.Scanner;
public class Demo11 {
public static void main(String[] args) {
Scanner superman = new Scanner(System.in);
int money;
String str;
System.out.println("请问您是会员,还是普通会员:");
str = superman.next();
System.out.println("请输入您的购物金额:");
money = superman.nextInt();
switch (str){
case "会员":
if (money < 200){
System.out.println("打8折");
}else{
System.out.println("打7.5折");
}
break;
case "普通会员":
if (money > 100){
System.out.println("打9折");
}else{
System.out.println("不打折");
}
break;
}
System.out.println("-------------*____*---------------");
}
}
结果如下:
【版权声明】本文为华为云社区用户原创内容,未经允许不得转载,如需转载请自行联系原作者进行授权。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)