今日目标
分支结构(if-else if-else)
分支结构(switch-case)
循环结构(for/do-while/while)
教学导航
- 掌握分支结构
- 掌握循环结构
- 总结区分循环结构中for、do-while/while循环的用法
1.分支结构(if-else if-else)
1.1需求分析
A:流程控制语句中的分类:
-
顺序结构
程序中最简单的流程控制,没有特定的语法,是按照代码的先后顺序,依次执行,程序中大多数都是这样执行的。
-
选择结构
-
循环结构
1.2 步骤分析
if(表达式){
}
if(判断表达式){
语句块1;
}
if(判断表达式){
语句块1;
}else{
语句块2;
}
if(判断表达式1){
语句块1;
}else if(表达式2){
语句块2;
}else if(判断表达式3){
语句块3;
}else{
语句块4;
}
2.分支结构 switch-case
2.1需求分析
2.2技术分析
1)语法格式
switch(变量/表达式){
case 字面值:
语句块1;break;
case 字面值:
语句块2;break;
......
default:
语句块n;
}
2.3执行流程
计算表达式的值/变量的值======》判断是不是匹配字面值;
如果匹配,执行语句块1,执行break跳出当前结构;
如果不匹配,判断是不是匹配字面值2;
如果匹配,执行语句块2.执行break跳出当前结构;
如果判断上面都不匹配,若有default的话,就执行default的语句。
练习1
package cn.tedu.day04;
import java.util.Scanner;
public class SwitchCaseDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
/* int num = 2;
switch (num) {//byte ,short,int,char,String(JDK 1.7之后)
case 1:
System.out.println(“1111”);
break;
case 2:
System.out.println(“120”);
case 3:
System.out.println(“119”);
break;
default:
System.out.println(“666”);
break;
}/
/
* 1.根据用户输入的年和月,计算一下该月有多少天?
* year % 4 ==0 && year % 100 !=0 || year % 400 ==0 闰年
*/
Scanner sc = new Scanner(System.in);
System.out.println(“请输入年和月份:”);
int year = sc.nextInt();
int month = sc.nextInt();
int day = 30;
switch (month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
day++;
break;
case 2:
day = year % 4 ==0 && year % 100 !=0 || year % 400 ==0 ? 29: 28;
break;
case 4:
case 6:
case 9:
case 11:
break;
}
System.out.println(year + “年” + month + “月有” + day + “天”);
}
}
练习2
package cn.tedu.day04;
import java.util.Scanner;
/**
- if的用法
- @author Administrator
*
*/
public class IfDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
/*
* if(表达式){
语句块1;
}
*/
int age = 22;//删除整行,ctrl+D
if(age < 18){
System.out.println("我是未成年!");
}
/*
* if(判断表达式){
语句块1;
}else{
语句块2;
}
*/
if(age < 18) {
System.out.println("我是未成年");
}else{
System.out.println("我们很年轻!");
}
/*
* if(判断表达式1){
语句块1;
}else if(判断表达式2){
语句块2;
}else if(判断表达式3){
语句块3;
}else{
语句块4;
}
*/
//王者荣耀
/*用Scanner三步走:
* 一.导包:
*1.导包:手动导包,java.util.Scanner;
*2.红色的小叉叉,系统去修改。
*3.快捷方式导包:ctrl+shift+o
*二.创建对象
*三.接收数据
*/
/*Scanner sc = new Scanner(System.in);
System.out.println("请输入你的rank分数,进行段位查询:");
int rank = sc.nextInt();
if (rank < 50) {
System.out.println("渣渣");
}else if (rank >= 50 && rank < 60){
System.out.println("黄金段位");
}else if (rank >= 60 && rank < 70){
System.out.println("铂金段位");
}else if (rank >= 70 && rank <80){
System.out.println("钻石段位");
}else if (rank >= 80 && rank <90){
System.out.println("星耀段位");
}else if (rank >= 90 && rank < 100){
System.out.println("最强王者");
}else{
System.out.println("无人能敌!");
}
sc.close();*/
/*
* 练习
* 根据键盘用用户的输入使用if-else if - else
* 90-100 优秀
* 80-89 良好
* 70-79 中等
* 60-69 差
* 60分以下 补考去吧!
* 2.判断闰年。(if-else)
* 根据用户输入的年份,
* 由程序去判断输入的年份是否是闰年
* 3.用户输入三个数,判断这三个数的最大值。
*/
//1.
/* Scanner sc = new Scanner(System.in);
System.out.println(“请输入您的成绩,来查询您的等级:”);
int scure = sc.nextInt();
if(scure < 60){
System.out.println(“补考去吧!”);
}else if (scure >= 60 && scure < 70){
System.out.println(“差”);
}else if (scure >= 70 && scure < 80){
System.out.println(“中等”);
}else if (scure >= 80 && scure < 90){
System.out.println(“良好”);
}else{
System.out.println(“优秀”);
}*/
//2.
Scanner sc = new Scanner(System.in);
System.out.println(“请输入一个年份:”);
int year = sc.nextInt();
if (year % 4 == 0 && year % 100 != 0){
System.out.println(“是闰年!”);
}else if(year % 400 ==0){
System.out.println(“是闰年!”);
}else{
System.out.println(“不是闰年”);
}
}
}
练习3
package cn.tedu.day04;
/**
-
顺序执行
-
@author Administrator
*
*/
public class OrderDemo {public static void main(String[] args) {//main alt+/ 回车
// TODO Auto-generated method stub
System.out.println(“111”);
System.out.println(“222”);
System.out.println(“000”);
System.out.println(“66666”);
}
}