分支控制
单分支
双分支
多分支
嵌套分支(先…再…的逻辑)
1. 基本语法
if(){
if(){
}else{
}
}
//1.
/*
参加百米运动会,如果用时8秒以内进入决赛,否则提示淘汰。
并且根据性别提示进入男子组或女子组。
输入成绩和性别,进行判断和输出信息。
*/
import java.util.Scanner;
/**
* @author Hwang
* @create 2020-06-28 18:31
*/
public class NestedIf {
public static void main(String[] args) {
/*
参加百米运动会,如果用时8秒以内进入决赛,否则提示淘汰。
并且根据性别提示进入男子组或女子组。
输入成绩和性别,进行判断和输出信息。
*/
Scanner sc = new Scanner(System.in);
System.out.print("请输入用时:");
double times=sc.nextDouble();
System.out.print("请输入性别:");
//sc.next().charAt(0)表示先得到String,取出第一个字符“男”
char male=sc.next().charAt(0);
if(times<=8.0){
if(male=='男'){
System.out.println("性别:"+male+"成绩: "+times);
}else if(male=='女'){
System.out.println("性别:"+male+"成绩: "+times);
}else{
System.out.println("没有该项目");
}
}else{
System.out.println("淘汰");
}
}
}
//2.经典的嵌套分支 应用案例2
/*
出票系统:根据淡旺季的月份和年龄,打印票价
4_10 旺季:
成人(18-60):60
儿童(<18):半价
老人(>60):1/3
淡季:
成人:40
其他:20
*/
switch分支结构
- switch case流程执行图
1. 基本语法:
switch(表达式){//表达式会返回一个值
case 常量1: //当表达式的值等于常量1,就执行语句块1
语句块1;
break;//退出switch
case 常量2;
语句块2;
break;
...
case 常量n;
语句块n;
break;
default://一个都没有匹配上执行这里的语句块
default语句块;
break;
}
//switch 快速入门
//表达式:任何有值的都可以称为表达式
class SwitchTest{
public static void main(String[]args){
}
}
- switch细节
1. switch(A) ,括号A的取值只能是整数或者可以转换为整型的数值类型。
比如:(byte,short,int,char,enum[jdk5.0],String[jdk7.0])。A的类型
但是long 是不能作用在switch语句上的!
A应和case 后的常量类型一致,或者是可以自动转成可以相互比较的类型,比如输入的是字符,而常量是int。
2. case B : C; case是常量表达式,也就是说B 的取值只能是常量(需要定义一个final型的常量),而不能是变量。
3. default 就是如果没有符合的case就执行default 后的语句。
4. default子句是可选的,当没有匹配的case时,执行default
5. break语句用来在执行完一个case分支后使程序跳出switch语句块;
如果没有写break,程序会顺序执行到switch结尾【穿透现象】
public class SwitchDetail
{
public static void main(String[] args) {
//细节1
//表达式数据类型,应和case 后的常量类型一致,或者是可以自动转成可以相互比较的类型,
//比如输入的是字符,而常量是int
char myChar = 'd';
switch( myChar ) {
case 'a' :
System.out.println("a");
break;
case 'b' :
System.out.println("b");
break;
case 60 : // "60" 不行 60是int类型 myChar可以自动提升为int类型
System.out.println("c");
break;
//.......
default :
System.out.println("没有匹配上的...");
}
//细节2
// switch(表达式)中表达式的返回值必须是:
// (byte,short,int,char,enum(枚举),String)
// double d = 1.1;
//
// switch( d ) {
// case 1.1 :
// System.out.println("ok");
// break;
// //default
// }
//细节3 : case子句中的值必须是常量(字面量,就是具体值 比如 1, "abc"),而不能是变量
int d = 1;
int n = 1;
switch( d ) {
case 1 : //1 不能换成 n
System.out.println("ok");
break;
//default
}
//细节4 : default子句是可选的,当没有匹配的case时,执行default
//细节5: break语句用来在执行完一个case分支后使程序跳出switch语句块;
// 如果没有写break,程序会顺序执行到switch结尾, 或者遇到break (称为穿透)
int n1 = 2;
switch( n1 ) {
case 1 :
System.out.println("ok1");
case 2 :
System.out.println("ok2");
case 3 :
System.out.println("ok3");
break;
case 4 :
System.out.println("ok4");
}
}
}
- 细节1中的错误图解:[需要的类型是char]
- 细节2中的错误图解:[switch(表达式)中表达式的返回值必须是:(byte,short,int,char,enum(枚举),String)]
- 细节3中的错误图解**[提示需要的是常量表达式]**
- 一道switch case 的题目
import java.util.Scanner;
/**
* @author Hwang
* @create 2020-06-28 19:36
*/
public class SwitchExcer {
public static void main(String[] args) {
/*
* 对学生成绩大于60分的,输出“合格”。
* 低于60分的,输出“不合格”。(注:输入的成绩不能大于100)
* */
Scanner sc = new Scanner(System.in);
System.out.print("请输入学生成绩:");
int score = sc.nextInt();
//if(score>0&&score<=100){
//switch (score/10){
// case 6:
// case 7:
// case 8:
// case 9:
// case 10:
// System.out.println("及格");
// break;
// default:
// System.out.println("不及格");
//}
//}else {
// System.out.println("输入的成绩有误...");
//}
int temp=score/60;
if(score>0&&score<=100){
switch (temp){
case 1:
System.out.println("及格");
break;
case 0:
System.out.println("不及格");
break;
}
}else{
System.out.println("输入的成绩有误...");
}
}
}
- switch和if的比较
1. 如果判断的具体数值不多,而且符合byte、 short 、int、 char, enum, String这6种类型。
虽然两个语句都可以使用,建议使用swtich语句。
2. 其他情况:对区间判断,对结果为boolean类型判断,使用if,if的使用范围更广
for循环
1. 基本语法
① ② ④
for(循环变量初始化;循环条件;循环变量迭代){
③
循环操作(语句);
}
说明:
a:for关键字,表示循环控制
b: for有四要素:①循环变量初始化、②循环条件、③循环操作(语句)、 ④循环变量迭代
c: 循环操作,这里可以有多条语句,也就是我们要循环执行的
d: 如果 循环操作(语句) 只有一条语句,可以省略 {}, 建议不要省略
强调:i如果是上面的方式,只能在{}里使用。作用域的问题。
注意事项:(class ForDetail)
1.循环条件是返回一个布尔值的表达式
2. for(;循环判断条件;) 中的初始化和变量迭代可以不写(写到其它地方),但是两边的分号不能省略。
3.循环初始值可以有多条初始化语句,但要求类型一样,并且中间用逗号隔开,
循环变量迭代也可以有多条变量迭代语句,中间用逗号隔开。
-
for循环的执行流程图
-
for 测试题
//1.打印1~100之间所有是9的倍数的整数的个数及总和. [使用for完成]
int count=0;
int sum=0;
for (int i = 1; i <100 ; i++) {
if(i%9==0){
System.out.println("被9整除:"+i);
sum+=i;
count++;
}
}
System.out.println("个数为:"+count);
System.out.println("总和:"+sum);
//2.看程序结果写代码
import java.util.Scanner;
/**
* @author Hwang
* @create 2020-06-28 21:14
*/
public class ForTest {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("请输入一个整数:");
int num=sc.nextInt();
for (int i = 0; i <=num; i++) {
System.out.printf("%d + %d = %d\n",i,num-i,num);
}
}
}
/*
请输入一个整数:5
0 + 5 = 5
1 + 4 = 5
2 + 3 = 5
3 + 2 = 5
4 + 1 = 5
5 + 0 = 5
看上面的结果写出代码
分析:1.有规律的,先看第一竖,从0-5
2.可以先试着输出0-5
3.结果均为5
4.所以第二竖为结果减去第一竖
5.把代码变活
*/
while循环
1.基本语法
循环变量初始化;①
while(循环条件){ ②
循环体(语句);③
循环变量迭代;④
}
细节:
1. 循环条件是返回一个布尔值的表达式
2. while循环是先判断再执行语句
-
while流程图
-
一道while题
//不断输入姓名,直到输入 "exit" 为止[使用while完成]
public class excer {
public static void main(String[] args) {
//while版本
Scanner sc = new Scanner(System.in);
System.out.print("请输入姓名:");
String str=sc.next();
while(!str.equals("exit")){
System.out.print("请输入姓名:");
str=sc.next();
}
//do while 版本
/*
Scanner sc = new Scanner(System.in);
String name="";
do{
System.out.print("请输入姓名: ");
name = sc.next();
}while(!name.equals("exit"));
*/
}
}
do while循环
1.基本语法
循环变量初始化;①
do{
循环体(语句);②
循环变量迭代;③
}while(循环条件);④
说明:
1. 也有循环四要素,知识位置不一样
2. 先执行,再判断,也就是说,一定会执行一次
3. 最后有一个分号;
[适合做菜单]
一个小比喻解决while 与do while :去要账,
while 的话就是问他还不还钱先,问他还不还钱,打到还钱时,就不打了。
do while就是先打他一顿,再问他还不还钱,直到还钱时,就不打了。
细节:
5. 循环条件是返回一个布尔值的表达式
6. do..while循环是先执行,再判断
- do while 测试题
public class dowhileTest2 {
public static void main(String[] args) {
/*
* 如果老公同意老婆购物,则老婆将一直购物,直到老公说不同意为止
* */
Scanner sc = new Scanner(System.in);
char flag;//变量提升上来,让do 代码块 和while 代码块都可以使用这个变量
do{
System.out.println("买买买");
System.out.print("我可以继续购物吗?y/n: ");
flag = sc.next().charAt(0);
//对输入的数据进行数据校验 如果输入的不是y或者n会输出:输入数据有误...
if(flag=='n'||flag=='y'){
System.out.println("买买买");
}else{
System.out.println("输入数据有误...");
}
}while(flag=='y');
}
}
- 一道转换问题
/*
* 大小字母的转换,当输入不是字母时,输出错误
* a-->97 z-->122
* A-->65 Z-->90
* */
boolean flag=true;//定义一个标记,默认可以进入循环
char c;
do {
char swap;
Scanner sc = new Scanner(System.in);
System.out.print("请输入一个字母:");
c = sc.next().charAt(0);
if(c>='a'&&c<='z'){
swap=(char)(c-32);
System.out.println("转换后"+swap);
}else if(c>='A'&&c<='Z'){
swap=(char)(c+32);
System.out.println("转换后"+swap);
} else{
System.out.println("输入有误");
flag=false;//输入的不是字母,就将标记修改,将循环结束
}
}while (flag);
多重循环[重难点]
1. 将一个循环放在另一个循环体内,就形成了嵌套循环。
其中,for ,while ,do…while均可以作为外层循环和内层循环。【建议一般使用两层,最多不要超过3层】
2.实质上,嵌套循环就是把内层循环当成外层循环的循环体。
当只有【内层循环的循环条件为false时】,才会完全跳出内层循环,
才可结束外层的当次循环,开始下一次的循环
3. 设外层循环次数为m次,内层为n次,则内层循环体实际上需要执行m*n=mn次。
class MutiFor{
public static void main(String[] args) {
for(int i=0;i<2;i++){//控制有i 行
for(int j=0;j<3;j++){//控制有 j 列
//当只有【内层循环的循环条件为false时】,才会完全跳出内层循环
//当j<3不成立时,退出里面的for循环
//退到外面的for 对i进行判断
System.out.println("i="+i+"j="+j)
}
}
}
}
多重循环的练习题
/*
统计3个班成绩情况,每个班有5名同学,求出各个班的平均分和所有班级的平均分[学生的成绩从键盘输入]。每个班的几个人数。
统计三个班及格人数,每个班有5名同学。
**/
public class excer {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int sum=0;//用来记录每个班级的总成绩
int verg=0;//用来记录每个班级的平均成绩
int count=0;//用来记录每个班级的及格人数
int SUM=0;//用来记录所有班级的总成绩
int VERG=0;//用来记录所有班级的平均成绩
for (int i = 1; i <=3; i++) {
for (int j = 1; j <=5 ; j++) {
System.out.print("请输入"+i+"班第"+j+"个人的成绩:");
int score=sc.nextInt();
sum+=score;//每个班级总成绩
verg=sum/j;//每个班级的平均成绩
if(score>=60){
count++;//及格就计数器加1
}
}
System.out.println("sum="+sum);
System.out.println("verg="+verg);
System.out.println("count="+count);
SUM+=sum;
sum=0;//记录完总成绩之后再清零
verg=0;//记录完平均成绩之后再清零
count=0;记录完及格人数之后再清零
}
VERG=SUM/15;//再外层循环之后再输出所有平均人数
System.out.println("所有班级的平均分"+VERG);
}
}
9*9乘法表
for (int i = 1; i <=9 ; i++) {
for (int j = 1; j <=i; j++) {//当j<=i时退出该for循环
System.out.print(i +"*"+j +"="+(i*j)+"\t");
}
System.out.println();
}
/*
1*1=1
2*1=2 2*2=4
3*1=3 3*2=6 3*3=9
4*1=4 4*2=8 4*3=12 4*4=16
5*1=5 5*2=10 5*3=15 5*4=20 5*5=25
6*1=6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=36
7*1=7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49
8*1=8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64
9*1=9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81
规律:行数 列数
1 1
2 2
3 3
... ...
i j
*/
素数
/*
输出100以内的所有素数(只能被1和自己整除的数, 1不是素数),
每行显示5个;并求和。
*/
/**
* @author Hwang
* @create 2020-06-30 18:32
*/
public class PrimeNumber {
public static void main(String[] args) {
/*
* 【嵌套循环】
* 输出100以内的所有素数(只能被1和自己整除的数, 1不是素数),每行显示5个;并求和。
* */
boolean flag;//是不是素数的标记
for (int i = 2; i <=100 ; i++) {//遍历2-100的数据
//从大浴缸出来,还有不是素数的标记(flag=false;)
// 需要重新看数据是不是素数 穿上新衣服 假设是素数 进去比较
flag=true;
//比较模块
for (int j = 2; j <=i-1; j++) {//从2到目标数的前一个数
if(i%j==0){//i不是素数 //这里的条件可以看作是大浴缸 一旦就去就表示不是素数了
flag=false;//印上不是素数的标记
break;//一旦找到素数 立即跳出此层的for循环
}
}
//输出模块
//比较完 看标记位 如果没有经过比较模块,flag任然是true,说明i没有进去过;所以是素数。打印一次
if (flag){
System.out.println("i = "+i);
}
}
}
}
break-跳转控制语句
- while 中的break执行流程图
- break-跳转控制语句的一个Test
/*
随机生成1-100的一个数,直到生成了97这个数,看看你一共用了几次?
提示使用 (int)(Math.random() * 100) + 1
思路分析: 循环,但是循环的次数不知道. -> break ,当某个条件满足时,终止循环
通过该需求可以说明其它流程控制数据的必要性,比如break
*/
public class breakTest {
public static void main(String[] args) {
int count=0;//计数
while(true){//死循环 如果while里面没有控制的条件
count++;
int rand=(int)(Math.random() * 100) + 1;
System.out.println("rand = "+rand);
if(rand==97){
break;//这里就是控制不要死循环的
}
}
System.out.println("count = "+count);//输出多少次后结束循环
}
}
1. 跳break语句用于终止某个语句块的执行,一般使用在switch或者循环[三大循环]中。
2. break语句出现在多层嵌套的语句块中时,可以通过标签指明要终止的是哪一层语句块。
3. 标签的基本使用
label1: { ……
label2: { ……
label3: { ……
break label2;
……
}
}
}
4. break 语句可以指定退出哪层
5. label1 是标签,由程序员指定。
6. break 后指定到哪个label 就退出到哪里
7. 在实际的开发中,尽量不要使用标签.
8. 没有指定break,默认退处最近的循环体
public class BreakDetail
{
public static void main(String[] args) {
lable1:
for( int j = 0; j < 5; j++) {
lable2:
for( int i = 0; i < 4; i++) {
if( i == 2) {
//break lable1; //退出 外层for
//break lable2; //退出 内层for
break; //默认退出 最近的 for
}
System.out.println("i=" + i + " j=" + j);
}
}
}
}
import java.util.Scanner;
/**
* @author Hwang
* @create 2020-06-28 23:44
*/
public class breakTest2 {
public static void main(String[] args) {
/*
* 实现登录验证,有三次机会,如果用户名为”张无忌” ,密码”888”提示登录成功,
* 否则提示还有几次机会,请使用for 循环+break完成
* */
int count=3;
Scanner sc = new Scanner(System.in);
for (int i = 1; i <=3; i++) {
System.out.print("请输入用户名:");
String username = sc.next();
System.out.print("请输入密码:");
String pwd = sc.next();
if(!(username.equals("admin")&&pwd.equals("888"))){
count--;
System.out.println("还有"+count+"次机会");
if(count<=0) {
break;
}
}else{
System.out.println("登录成功");
break;
}
}
}
}
跳转控制语句-continue
1. continue语句用于结束本次循环,继续执行下一次循环。
2. continue语句出现在多层嵌套的循环语句体中时,可以通过标签指明要跳过的是哪一层循环 , 这个和前面的标签的使用的规则一样.
3. 基本语法:
{ ……
continue;
……
}
4. continue语句,只能配合循环语言使用,不能单独和switch/if使用。
if ( 6 > 4) {
//语句
continue; //错误
}
int i=1;
while(i<=4){
i++;
if(i==2){
continue;
}
System.out.println("i="+i);
}
-
上面代码的解析图
-
continue结合标签使用
class ContinueTest2{
}