分支结构和循环结构

1. 分支结构
1.1 生活中的分支结构
大量的使用在
	条件判断,等级判断中
1.2 开发中的分支结构
1.2.1 if 分支结构
if (/* 判断条件 boolean */) {
    // 语句体
}

/*
执行流程:
	当程序运行到if分支结构,首先判断if之后小括号里面的内容是否为true,如果为true,执行语句体,如果为false,执行大括号之后的语句
*/
/* if 分支结构 */
class Demo6 {
	public static void main(String[] args) {
		boolean ret = false;
		
		if (ret) {
			System.out.println("中(河南话)");
		}
		
		System.out.println("请讲普通话");
	}
}
1.2.2 if else 分支结构
if (/* 条件判断 boolean true or false */) {
	// true语句体
} else {
	// false语句体
}
/*
执行流程:
	当程序运行到if else分支结构,首先判断if之后小括号里面的内容结果情况,如果是true,执行if之后大括号true语句体内容,如果是false,执行else之后大括号false语句体内容。
	世界上最遥远的距离,是我在if里,你在else里
*/
/* if else结构 */
class Demo7 {
	public static void main(String[] args) {
		boolean ret = false;
		
		if (ret) {
			// true语句体
			System.out.println("中");
		} else {
			// false语句体
			System.out.println("不中");
		}
	}
} 
1.2.3 if else if 分支结构
if (/* 条件1 */) {
    // 处理方式1
} else if (/* 条件2 */) {
    // 处理方式2
} else if (/* 条件3 */) {
    // 处理方式3
} else {
    // 最终处理方法
}

/*
执行流程:
	当程序运行到if else if分支结构,首先匹配 if 之后的条件,如果存在匹配项,执行对应的处理方式,结束分支结构。如果没有任何一个条件匹配,执行else中最终处理方式。
	
学生成绩等级划分
	90 ~ 100 秀儿
	80 ~ 89 良儿
	70 ~ 79 中儿
	60 ~ 69 过儿
	60以下 渣儿~~ 让你爹穿着拖鞋来学校
*/
/*
学生成绩等级划分
	90 ~ 100 秀儿
	80 ~ 89 良儿
	70 ~ 79 中儿
	60 ~ 69 过儿
	60以下 渣儿~~ 让你爹穿着拖鞋来学校
*/
class Demo8 {
	public static void main(String[] args) {
		int score = 59;
		
		if (score >= 90 && score <= 100) {
			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("让你爹穿着拖鞋来学校");
		}
	}
}
【补充知识点 Scanner使用 CV大法/直接不会】
1. 点技能点
	导包 在class之前 复制粘贴 
	import java.util.Scanner;

2. 创建Scanner变量
	固定格式:
		Scanner sc = new Scanner(System.in);

3. 通过Scanner变量,调用方法获取用户输入数据
	需要获取int类型 
		int num = 0;
		num = sc.nextInt();
	需要获取float类型 
		float num = 0.0F;
		num = sc.nextFloat();
	需要获取char类型 
		char ch;
		ch = sc.nextLine().charAt(0);
// 使用完记得关闭资源:scanner.close();
/*
next():
一定要读取到有效字符后才可以结束输入
对输入有效字符之前遇到的空白,next() 方法会自动将其去掉
只有输入有效字符后才将其后面输入的空白作为分隔符或者结束符
next() 不能得到带有空格的字符串

nextLine()
以Enter为结束符,也就是说,nextLine() 方法返回的是输入回车之前的所有字符
可以获得空格符
*/
public static void main(String[] args) {

        double sum = 0;// 输入数字之和
        int a = 0 ; // 输入几个数字

        Scanner scanner = new Scanner(System.in);
		// scanner.hasNextDouble() 判断下一行输入的是否是double
        while (scanner.hasNextDouble()) {

            double v = scanner.nextDouble();
            a += 1;
            sum = sum + v;


        }
        System.out.println(a);
        System.out.println(sum);
        scanner.close();
    }
【补充知识点 用户友好性提示】
在开发中,我们的产品需要考虑用户体验,增强用户粘性!!!
【补充知识点 用户输入数据合法性判断】
用户输入数据有可能出现输入错误,超出范围,不合理的情况!!!
加入合法性判断,如果数据不再合理范围以内,退出程序。
/*
学生成绩等级划分
	90 ~ 100 秀儿
	80 ~ 89 良儿
	70 ~ 79 中儿
	60 ~ 69 过儿
	60以下 渣儿~~ 让你爹穿着拖鞋来学校
*/
// 导包 点技能点
import java.util.Scanner;

class Demo8 {
	public static void main(String[] args) {
		int score = 0;
		// 创建Scanner变量
		Scanner sc = new Scanner(System.in);
		
		System.out.println("请输入学生的成绩:");
		// 从键盘上获取用户输入的int类型数据
		score = sc.nextInt();
		
		// 用户输入数据合法性判断
		if (score > 100 || score < 0) {
			System.out.println("亲,学生成绩输入错误哦~~~啾咪(づ ̄3 ̄)づ╭?~");
			// 退出程序
			System.exit(0);
		}
	
		if (score >= 90 && score <= 100) {
			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("让你爹穿着拖鞋来学校");
		}
	}
}
1.3 switch - case 分支结构
switch (/* 匹配数据 */) {
    case 常量1:
        处理方式1;
        break;
    case 常量2:
        处理方式2;
        break;
    case 常量3:
        处理方式3;
        break;  
    default:
        最终处理方式;
        break;
}
/*
执行流程:
	当前程序运行到switch case结构,首先获取switch 之后小括号里面的数据内容,根据数据内容匹配case选项,如果有对应的case出现,执行对应处理方式,通过break关键字跳出switch结构,如果没有任何一个case匹配,执行default操作。
	AJCG规范中,default不建议省略。
*/
/* 
switch case演示
中药:
	1. 枸杞
	2. 陈皮
	3. 菊花
	4. 蒲公英 
 */
import java.util.Scanner;
 
class Demo1 {
	public static void main(String[] args) {
		// 用于保存用户输入的选项
		int choose = 0;
		Scanner sc = new Scanner(System.in);
		
		System.out.println("1. 枸杞 100/KG");
		System.out.println("2. 陈皮 188/0.25KG");
		System.out.println("3. 菊花 15/KG");
		System.out.println("4. 蒲公英 200/KG");
		System.out.println("请选择您要购买的药物:");
		
		choose = sc.nextInt();
		
		switch (choose) {
			case 1:
				System.out.println("人到中年不得已,保温杯里泡枸杞");
				break;
			case 2:
				System.out.println("陈皮 糖~~~");
				break;
			case 3:
				System.out.println("菊花 茶~~~");
				break;
			case 4:
				System.out.println("蒲公英 ");
				break;
			default:
				System.out.println("请输入正确的选项");
				break;
		}	
	}
}
1.4 switch case 使用注意事项
1. 在 switch case结构中,能够执行的代码,有且只能是case之后,或者default之后的内容
2. switch case结构中,不允许出现相同的case选择
3. case之后可以省略break,当前case选项执行过程中,需要明确的找到下一个break,跳出switch case结构
4. 允许多个case并列出现,同时执行同一个操作过程。
5. default可以省略,但是基于开发原则,不建议省略default操作。
2. 循环结构
2.1 为什么要用循环
	代码中一定会出现,大量重复功能的代码模块,如果使用CV大法会导致以下一些问题:
	1. 代码冗余,过于臃肿
	2. 阅读性极差
	3. 维护性极差
2.2 while循环结构
while (/* 循环条件判断 boolean true or false */) {
    // 循环体
    // (循环条件变更)
}
/*
执行流程:
	当程序运行到while循环结构,首先判断while之后的表达式是否为true,如果为true,执行循环体,再来进行循环条件判断,直到条件为false,终止循环!
*/
/* while循环结构 */
class Demo3 {
	public static void main(String[] args) {
		int num = 10;
		
		while (num > 0) {
			System.out.println("中午吃热干面~~~");
			num -= 1; // num--; --num;
		}
	}
}
2.3 do while循环结构
do {
    // 循环体
    // (循环条件变更操作)
} while (/* 循环条件判断 boolean true or false */);

/*
执行流程:
	程序执行到do while循环结构,首先执行一次循环体(循环条件变更操作),再来判断while之后的循环条件是否为true,true继续循环,false终止循环
*/
/* do while循环结构 */
class Demo4 {
	public static void main(String[] args) {
		int num = 10;
		
		do {
			System.out.println("请讲普通话");
			num -= 1;
		} while (num > 0);
	}
}
2.4 for循环结构【重点,重点,重点】
for (/* 循环条件初始化 */; /* 循环条件判断 */; /* 循环条件变更 */) {
    // 循环体
}

在这里插入图片描述

// 打印九九乘法表
public class test {
    public static void main(String[] args) {
		// i 外层循环,可以理解为一共有九行
        for (int i = 1; i <= 9; i++) {
            // 内层循环,j<=i,每行几个元素,用i约束
            for (int j = 1; j <= i; j++) {
                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	
*/
2.5 break和continue区别
break:直接跳出循环,终止循环
continue:跳出本次循环,继续下一次循环,即判断是否开始下一次循环,回到循环最开始的地方
public class TestBreak {
    public static void main(String[] args) {

        for (int i = 1; i <= 100; i++) {
            if (i % 10 == 0) {
                break;
            }
            System.out.print(i+"\t"); // 输出结果 1	 2	3	4	5	6	7	8	9
        }
    }
}

public class TestContinue {
    public static void main(String[] args) {

        for (int i = 1; i <= 100; i++) {
            if (i % 10 == 0) {
                continue;
            }
            if(i%5==0) {
                System.out.println();
            }
            System.out.print(i+ " ");
        }
    }
}
/*
1 2 3 4 
5 6 7 8 9 11 12 13 14 
15 16 17 18 19 21 22 23 24 
25 26 27 28 29 31 32 33 34 
35 36 37 38 39 41 42 43 44 
45 46 47 48 49 51 52 53 54 
55 56 57 58 59 61 62 63 64 
65 66 67 68 69 71 72 73 74 
75 76 77 78 79 81 82 83 84 
85 86 87 88 89 91 92 93 94 
95 96 97 98 99 
*/
2.6 for循环练习
// 打印三角形
public class Test {
    public static void main(String[] args) {

        for (int i = 0; i <= 5; i++) {
            for (int j = 5; j >= i; j--) {
                System.out.print(" ");
            }
            for (int j = 1; j <=2*i-1;j++) {
                System.out.print("*");
            }
            System.out.println();
        }
    }
}

/*   
    *
   ***
  *****
 *******
*********
*/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值