传统纸媒公司网站建设需求营销策划公司靠谱吗
选择和分支结构
- 选择和分支结构
- 一、复习问答
- 二、选择结构
- 2.1 基础选择结构
- 2.2 if-else结构
- 2.3 多重if结构
- 2.4 嵌套if结构
- 三、分支结构
- 四、局部变量
选择和分支结构
一、复习问答
1、Java中基本数据类型
2、类型的转换的两种情形
3、数据类型提升的规则
二、选择结构
2.1 基础选择结构
语法:
if(条件表达式){
// 代码块}
执行流程:先判断条件,如果条件结果为true,则执行代码块,否则就跳过代码块。
public class Demo01 {// 基础if结构public static void main(String[] args) {// 如果敌方英雄血量低于200,越塔强杀int hp = 250;if(hp < 200) {System.out.println("越塔强杀");}int temp = 20;if(temp > 20) {System.out.println("开空调");}System.out.println("程序结束");}
}
2.2 if-else结构
语法:
if(条件表达式){
// 代码块1}else{
// 代码块2
}
执行流程:先判断条件,如果条件结果为true,则执行代码块1,否则就执行代码块2。
public class Demo02 {// if-else结构public static void main(String[] args) {// 如果敌方英雄血量低于200,越塔强杀,否则继续消耗int hp = 180;if(hp < 200) {System.out.println("越塔强杀");}else {System.out.println("继续消耗");}// 定义一个整数变量,如果值为奇数,输出是奇数,否则输出是偶数int num = 20;
// int num1 = 8765;
// int g = num1 % 10;
// int s = num1 / 10 % 10;if(num % 2 == 1) {System.out.println("是奇数");}else {System.out.println("是偶数");}}
}
2.3 多重if结构
语法:
if(条件表达式1){
// 代码块1}else if(条件表达式2){
// 代码块2
}else{
// 代码块3
}
执行流程:先判断条件1,如果条件结果为true,则执行代码块1,否则就去判断条件2,如果成立,则执行代码块2,否则执行代码块3。
注意:代码从上往下执行,任意一个条件成立,则不会继续向下执行其他判断。
public class Demo03 {// 多重if结构public static void main(String[] args) {// 如果成绩低于60,显示不及格,介于[60,80)之间,显示一般,[80,90)之间,显示良好,高于90,显示优秀int score = 95;
// if(score < 60) {
// System.out.println("不及格");
// }else if(score >= 60 && score < 80) {
// System.out.println("一般");
// }else if(score >= 80 && score < 90) {
// System.out.println("良好");
// }else {
// System.out.println("优秀");
// }if(score >= 90) {System.out.println("优秀");}else if(score >= 80) {System.out.println("良好");}else if(score >= 60) {System.out.println("一般");}else {System.out.println("不及格");}// 定义一个月份,输出该月有多少天(都按平年计算)int month = 5;if(month == 1 || month == 3 || month == 5 || month == 7 || month == 8|| month == 10 || month == 12) {System.out.println("31天");}else if(month == 2) {System.out.println("28天");}else {System.out.println("30天");}}
}
2.4 嵌套if结构
语法:
if(外层条件表达式){
// 外层代码块1
if(内层条件表达式){
// 内层代码块1 }else{
// 内层代码块2
}
}else{
// 外层代码块2
}
执行流程:先判断外层条件,如果条件结果为true,则执行外层代码块1,否则就执行外层代码块2。在执行外层代码块1后,发现又有内层if,继续判断内层条件是否成立,如果成立,则执行内层代码块1,否则执行内层代码块2.
public class Demo04 {// 嵌套ifpublic static void main(String[] args) {Scanner input = new Scanner(System.in);System.out.println("请选择阵营:1、联盟 2、部落");int select = input.nextInt();if(select == 1) {System.out.println("欢迎来到联盟");System.out.println("请选择种族:1、精灵 2、人类");select = input.nextInt();if(select == 1) {System.out.println("欢迎加入精灵");}else {System.out.println("欢迎加入人类");}}else {System.out.println("欢迎来到部落");}}
}
三、分支结构
语法:
switch(变量、表达式){
case 值1:
// 代码块1
case 值2:
// 代码块2
case 值3:
// 代码块3
default:
// 代码块4
}
运行流程:先得到变量或表达式的值,然后判断该值是否等于值1,等于则执行代码块1,如果不等于,继续向下判断值2,如果所有的值都不等于,则执行default中的内容。
变量的类型为:byte、short、int、char、String(jdk1.7+)、枚举
public class Demo05 {// switch用法// fall-downpublic static void main(String[] args) {int month = 3;switch(month) {case 1:case 3:case 5:case 7:case 8:case 10:case 12:System.out.println("31天");break;case 2:System.out.println("28天");break;case 4:case 6:case 9:case 11:System.out.println("30天");break;default:System.out.println("月份不对");}}
}
注意:当没有break时,代码会继续向下执行,直到switch结束或者遇到break为止。
public class Demo06 {// 求某年某月某日是当年的第几天,计算闰年// 判断闰年的规则是:年份能被400整除,或者年份能被4整除并且不能被100整除,public static void main(String[] args) {Scanner input = new Scanner(System.in);System.out.println("请输入年份:");int year = input.nextInt();System.out.println("请输入月份:");int month = input.nextInt();System.out.println("请输入日期:");int day = input.nextInt();int days = 0; // 计算总天数switch(month) {case 12:days += 30; // 加上11月case 11:days += 31; // 加上10月case 10:days += 30; // 加上9月case 9:days += 31; // 加上8月case 8:days += 31; // 加上7月case 7:days += 30; // 加上6月case 6:days += 31; // 加上5月case 5:days += 30; // 加上4月case 4:days += 31; // 加上3月case 3:if(year % 400 == 0 || year % 4 == 0 && year % 100 != 0) {days += 29;}else {days += 28;}case 2:days += 31; // 加上1月的天数// days = days + 31;case 1:days += day; // 加上当月的天数
// days = days + day;break;default:System.out.println("月份不对");}System.out.println(year + "年" + month + "月" + day + "日是当年的第"+days+"天");}
}
四、局部变量
- 声明在方法内部的变量,必须先赋值后使用。
- 作用域范围:是从定义的行开始到所在的代码块结束。
- 如果多个变量,在重合的作用域范围,不能同名,会冲突报错。
public class Demo07 {// 局部变量的作用域public static void main(String[] args) {
// System.out.println(month); // 没有定义之前不能使用int month = 5;int num = 8;if(month > 3) {
// int num = 6; // 范围冲突System.out.println(num);System.out.println(month);}else {
// int num = 5; // 范围冲突}System.out.println(num);System.out.println(month);}
}