Java编程入坑三(循环控制)

本文深入解析了程序设计中的流程控制语句,包括顺序、选择和循环结构,并详细阐述了if、switch、for、while及do...while循环的使用场景与语法。同时,介绍了方法的定义、调用、参数传递及返回值,探讨了方法重载的概念。

流程控制语句

1.顺序结构

​ 我们的代码执行就是顺序结构

​ 从上到小 从左到右

2.选择结构

### 1.if语句的第一种形式

if(表达式){
	执行语句
}if里面  这个小括号里面的表达式  是一个boolean类型的   
//		想要使用咱们的boolean类型,第一种  直接给他传一个boolean类型
//		第二种  使用逻辑运算符进行判断
public class IfDemo {
	public static void main(String[] args) {
//		选择结构
//		判断语句   if
		/**
		 * if(表达式){
		 * 	执行语句
		 * }
		 */
		int a = 6;
		int c = 7;
		
//		在if里面  这个小括号里面的表达式  是一个boolean类型的   
//		想要使用咱们的boolean类型,第一种  直接给他传一个boolean类型
//		第二种  使用逻辑运算符进行判断
	
//		定义第三方变量用于直接认定最大值
		int max = c;
		
//		如果你比我大  我就把第三方变量进行重新赋值操作
		if (a > c) {
			max = a;
		}
		
//		打印第三方变量
		System.out.println("比较大的那个数是" + max);

		
//		需求:键盘录入两个数,使用单if  判断两个数字的大写,输出较大的那个数

	}
}

2.if的第二种形式

if(表达式){
	执行语句
}else{
    执行语句
}

public class IfTest {
	public static void main(String[] args) {
		/*
		 * 输入两个学生的成绩
		 * 
		 * 如果学生A成绩高,那就打印A
		 * 
		 * 如果学生B成绩高,那就打印B
		 * 
		 * if....else
		 * 
		 * */
		
//		键盘录入
		Scanner sc = new Scanner(System.in);
		
		System.out.println("请录入学生A的成绩");
		int A = sc.nextInt();
		
		System.out.println("请录入学生B的成绩");
		int B = sc.nextInt();
		
		if (A > B) {
			System.out.println(A);
		}else{
			System.out.println(B);
		}
	}
}

3.if的第三种结构

if .... else if ....无限个else if ... else
public class IfTets2 {
	public static void main(String[] args) {
//		对下面学会成绩进行分组
//		录入学生成绩   
//		如果分数大于90   返回评级为A
//		如果分数在80-90之间  返回评级为B
//		如果分数在60-80之间  返回评级为C
//		如果低于60分    返回评级为D
		
		/**
		 * if(表达式1){
		 * 	执行语句1
		 * }else if(表达式2){
		 * 	执行语句2
		 * }...else{
		 * 	执行语句3
		 * }
		 */
		
		Scanner sc = new Scanner(System.in);
		
		System.out.println("请输入学生成绩");
		
		int stu = sc.nextInt();
		
		
//		正确的循环判断
		if(stu >= 90){
			System.out.println("学生成绩评分为A");
		}else if (stu < 90 && stu >= 80) {
			System.out.println("学生成绩为B");
		}else if (stu < 80 && stu >= 60) {
			System.out.println("学生成绩为C");
		}else {
			System.out.println("学生成绩为D");
		}
}
		
//		因为if(表达式)放boolean类型
//		三目运算符返回类型是什么  不定型
		
//		如果想在if表达式中使用三目运算符  那么就要保证三目运算符的返回结果值一定是个boolean值
		
//有bug版的循环判断   如果次序写反   就会造成  提前被拦截打印
//		if(stu >= 90){
//			System.out.println("学生成绩评分为A");
//		}else if (stu >= 80) {
//			System.out.println("学生成绩为B");
//		}else if (stu >= 60) {
//			System.out.println("学生成绩为C");
//		}else {
//			System.out.println("学生成绩为D");
//		}
//		
		
		
//		if (stu >= 90) {
//			System.out.println("学生成绩评分为A");
//		}
//		if (80 <= stu && 90 > stu) {
//			System.out.println("学生成绩为B");
//		}
//		if (60 <= stu && 80> stu) {
//			System.out.println("学生成绩为C");
//		}else{
//			System.out.println("学生成绩为D");
//		}
//		if (stu < 60) {
//			System.out.println("学生成绩为D");
//		}
	}

3.循环结构

1.选择结构之switch结构

### 1.switch格式
switch(表达式)://表达式可以放得值   byte  short   int   char   jdk1.7以后可以是String  枚举
	case value1:
		执行语句;
		break;
	case value2:
		执行语句;
		break;	
	case value3:
		执行语句;
		break;
	case value4:
		执行语句;
		break;
    default:
    	执行语句;
    	break;

执行流程:

​ 第一步先去执行表达式:

​ 第二步拿着表达式里面的值和case后面的value进行比对

​ 第三步:如果比对成功 执行case里面的语句

​ 第四步:如果没有匹配到对应的case,name执行default中的语句信息

2.案例

​ 键盘录入一个数字,然后匹配出它属于星期几,然后输出星期几

public class Test2 {
	public static void main(String[] args) {
		/*switch的格式
		 * switch(表达式): short   byte   int   char      JDK 1.7   String
		 * 		case value1:       value的值只能是常量  不能是变量
		 * 				执行语句;
		 * 				break;
			 * case value2:
			 * 				执行语句;
			 * 				break;
			 * case value3:
			 * 				执行语句;
			 * 				break;
			 * case value4:
			 * 				执行语句;
			 * 				break;
			 * case value5:
			 * 				执行语句;
			 * 				break;
		 * 
		 */
		
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入数字");
		
		int i = sc.nextInt();//1
		
		switch (i) {
			case 4:
				System.out.println("星期四");
				
			case 5:
				System.out.println("星期五");
				
			case 6:
				System.out.println("星期六");
				
			case 7:
				System.out.println("星期日");
				
			case 1:
				System.out.println("星期一");
				
			case 2:
				System.out.println("星期二");
				
			case 3:
				System.out.println("星期三");
			
		}
		
	}
}

3.注意事项

	- 1.case后面只能跟常量,不能跟变量;
	- 2.多个case之间的常量值不能相同
	- 3.case的顺序有没有要求
	- 4.default一定要放在最后吗?switch里面   一般情况都是先执行case,case匹配不到了 再执行default
	- 5.defautl可以省略吗?  可以  
	- 6.break可以省略吗?如果省略?代码该如何执行  可以省略的   执行完匹配成功的case以后,不在关心下面的case时候符合条件,直接执行case中的语句,直到执行的那条语句中有break;或者执行到了switch的最后   ,但是强烈建议,不要省略break,除非一些特殊情况下
	- 7.switch语句如何结束    要不遇见break;要不运行到大括号结束

4.案例部分

​ 1.使用case去完成春夏秋冬案例

public class Test3 {
	public static void main(String[] args) {
		// 1.使用case去完成春夏秋冬案例
		// 2.键盘录入两个数字和一个运算符,然后返回结果 5 + 5 5+5=10 5 * 5 5*5 = 25 +-*/
		// 3.switch如何实现分数评级查询 90 A 70 B 78 B

		Scanner sc = new Scanner(System.in);

		System.out.println(12|1|2);
		System.out.println(6|7|8);
		
		System.out.println("请输入月份");
		int month = sc.nextInt();

		
		switch (month) {
		case 12:
		case 1:
		case 2:
			System.out.println("冬季");
			break;

		case 3:
		case 4:
		case 5:
			System.out.println("春天到了,万物");
			break;

		case 6:
		case 7:
		case 8:
			System.out.println("夏天");
			break;

		case 9:
		case 10:
		case 11:
			System.out.println("秋天");
			break;


		default:
			System.out.println("请输入合法的月份");
			break;
		}

	}
}

​ 2.键盘录入两个数字和一个运算符,然后返回结果

public class Test4 {
	public static void main(String[] args) {
		// 2.键盘录入两个数字和一个运算符,然后返回结果 5 + 5 5+5=10 5 * 5 5*5 = 25 +-*/

		Scanner sc = new Scanner(System.in);
		System.out.println("请输入第一个数字");
		int a = sc.nextInt();

		System.out.println("请输入第二个数字");
		int b = sc.nextInt();

		System.out.println("请输入你想要操作的运算:+ - * /");
		String c = sc.next();

		switch (c) {
		case "+":
			System.out.println(a+"+"+b+"="+(a+b));
			break;
		case "-":
			System.out.println(a+"-"+b+"="+(a-b));
			break;
		case "*":
			System.out.println(a+"*"+b+"="+(a*b));
			break;
		case "/":
			System.out.println(a+"/"+b+"="+(a/b));
			break;

		default:
			System.out.println("暂不提成该类型的运算");
			break;
		}

	}
}

​ 3.switch如何实现分数评级查询

public class Test5 {
	public static void main(String[] args) {
//		3.switch如何实现分数评级查询 
		
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入学生的成绩信息");
		int i = sc.nextInt();
		
		if (i < 0 || i > 100) {
			System.out.println("请输入正确的成绩");
		}else{
			switch (i / 10) {
			case 10:
			case 9:
			case 8:
				System.out.println("A");
				break;
				
			case 7:
			case 6:
				System.out.println("B");
				break;
				
			case 5:
				System.out.println("C");
				break;
				
			default:
				System.out.println("D");
				break;
				
			}
		}
		
		
	}
}

2.循环结构之for循环(重点,重中之重)

​ 为什么使用循环语句?

​ 通过打印100条helloworld来引出循环语句

​ 循环结构的组成:

​ 初始化语句

​ 判断条件语句

​ 循环体语句

​ 控制条件语句

​ for循环的格式

​ for(初始化语句;判断条件语句;控制条件语句){

​ 循环体语句

​ }

​ for循环的执行流程:

​ 1.先执行初始化语句

​ 2.再执行判断条件语句

​ 3.再去执行循环体语句

​ 4.再去执行控制条件语句

​ 5.然后再去执行判断条件语句

3.循环结构之while循环(重点)

基本格式:

​ while(判断条件语句){

​ 循环体语句;

​ }

扩展格式:

​ 初始化语句;

​ while(判断条件语句){

​ 循环体语句;

​ 控制条件语句

​ }

案例:打印一句话100次

while循环和for循环的简单区别

​ 就是关于初始化语句中变量使用之后是否存在的区别

4.循环结构之do…while循环

1.while循环和do…while循环

### 1.1 while循环(重点)

基本格式:

​ while(判断条件语句){

​ 循环体语句;

​ }

扩展格式:

​ 初始化语句;

​ while(判断条件语句){

​ 循环体语句;

​ 控制条件语句

​ }

案例:打印1–100的和

		int  i = 1;
		
		int sum = 0;
		
		while( i <= 100){
			sum += i;
			i++;
		}
		
		System.out.println(sum);

​ 打印出1–100中奇数的总数量

		int  i = 1;
		
		int num = 0;
		
		while(i <= 100){
			
			num++;
			i+=2;
		}
		System.out.println(num);
		

while循环和for循环的简单区别

​ 就是关于初始化语句中变量使用之后是否存在的区别

​ while循环的初始化变量不会随着循环的结束而消失,这个变量可以被重复利用和调用

​ for循环的初始化变量是随着循环的结束而直接消失,不可以被重复利用

while循环和for循环的选择

​ 什么时候使用while循环

​ 什么时候使用for循环

​ 当不确定循环次数的时候,我们可以选择使用while循环

​ 当确定循环次数的时候,我们可以选择使用for循环

2.do…while循环(重点)

​ 基本格式:

​ do{

​ 循环语句;

​ 控制语句;

​ }while(判断语句)

while do…while for三种循环的选择:

​ 当考虑循环次数的时候,选择for;

​ 当不考虑循环次数的时候,使用while或者do…while

​ 当循环体语句至少要执行一次的时候,选择do…while

​ 否则,选择while

3.循环的嵌套(大重点)

​ 1.通过打印4行5列五角星引出循环的嵌套

​ 2.先对行进行分析,并使用外层循环进行包装

​ 3.在队列进行分析,使用内层循环进行包装

​ 分析代码的执行流程

public class Test5 {
	public static void main(String[] args) {
//		*****
//		*****
//		*****
//		*****
//		System.out.println("*****");
//		System.out.println("*****");
//		System.out.println("*****");
//		System.out.println("*****");
		
//		外层循环控制行数,内层循环控制列数
		for(int i = 0 ; i < 5 ; i++){
			
			for (int j = 0; j < 4; j++) {
				
				System.out.print("*");
				
			}
			
			System.out.println();
		}
		
	}
}

​ 案例设计1:

​ 打印出三角形的五角星

public class Test5 {
	public static void main(String[] args) {
//		*****
//		*****
//		*****
//		*****
//		System.out.println("*****");
//		System.out.println("*****");
//		System.out.println("*****");
//		System.out.println("*****");
		/*
		 *     *      1 
		 *     **     2
		 *     ***    3
		 *     ****   4
		 *     *****  5 
		 *     ****** 6
		 *     
		 *     *****  5
		 *     ****	  4
		 *     ***    3
		 *     **     2
		 *     *      1
		 * 
		 */
		
//		外层循环控制行数,内层循环控制列数
		for(int i = 0 ; i < 5 ; i++){
			
			for (int j = 0; j < 5 ; j++) {       
				  								  
				System.out.print("*");         
				
			}
			
			System.out.println();
		}
		
		System.out.println("-------------------");
		
		for (int i = 0; i < 6; i++) {
			for (int j = 0; j < i + 1; j++) {
				System.out.print("*");
			}
			System.out.println();
		}
		
		System.out.println("----------------------------------");
		
		
		for (int i = 0; i < 5; i++) {
			for (int j = i; j < 5; j++) {
				System.out.print("*");
			}
			System.out.println();
		}
		
	}
}

​ 案例设计2:(重点)

​ 打印99乘法表

public class Test6 {
	public static void main(String[] args) {
//				1x1=1
//				1x2=2   2x2=4
//				1x3=3   2x3=6   3x3=9
//				1x4=4   2x4=8   3x4=12   4x4=16
//				1x5=5   2x5=10  3x5=15  4x5=20  5x5=25
//				1x6=6   2x6=12  3x6=18  4x6=24  5x6=30  6x6=36
//				1x7=7   2x7=14  3x7=21  4x7=28  5x7=35  6x7=42  7x7=49
//				1x8=8   2x8=16  3x8=24  4x8=32  5x8=40  6x8=48  7x8=56  8x8=64
//				1x9=9   2x9=18  3x9=27  4x9=36  5x9=45  6x9=54  7x9=63  8x9=72  9x9=81
		
		for (int i = 1; i < 10; i++) {
			for (int j = 1; j <= i; j++) {
				System.out.print(j + "*" + i + "=" + (j*i) + "\t");
			}
			System.out.println();
		}

		System.out.println("\"helloworld\"");
	}
}

2.跳转流程控制语句

​ 如何结束流程控制语句

​ 跳出流程控制语句的三个关键字:

​ break: switch语句中,循环语句中,离开场景无意义,

​ 结束单层循环,执行后面代码

​ continue:循环语句中,离开场景无意义,结束本次循环, 继续下次循环

​ return:结束整个方法,并返回给方法的调用者

代码引入:

public class Test7 {
	public static void main(String[] args) {
		/*
		 * 结束流程控制语句的三个关键字
		 * 
		 * break:    switch   循环语句中都有所使用   结束循环或者选择 break cannot be used outside of a loop or a switch
		 * continue:	循环语句中使用   单独使用无意义  结束循环  continue cannot be used outside of a loop
		 * return:   结束循环  或者给方法返回一个值
		 * 
		 * 
		 * 
		 * break:可以跳出当前单层循环  继续执行循环外的代码
		 * continue:跳过本次循环,执行下次循环  
		 * return:结束所有运行代码
		 */
		
		for (int i = 0; i < 5; i++) {
			for (int j = 0; j < 10; j++) {
				if (j == 3) {
					break;
//					continue;
//					return;
				}
				System.out.println(j);
			}
		}
		
		
		
		System.out.println("我是循环外的东西");
		
	}
}

​ 案例设计:

for(int i = 0 ; i <= 10 ; i++){
 	if(i == 3){
        //break;
        //continue;
        //return
    }
    System.out.println("i");
 }
System.out.println("over");

3.案例结束语句体

public class Test9 {
	public static void main(String[] args) {
//		小娟的妈妈每天给她3元钱,她都会存起来,但是,每当这一天的天数是存钱的第6天或者6的倍数的话,
//		她都会花去5元钱,请问,经过多少天,小娟才可以存到100元钱。
		
		int money = 0;
		int day = 0;
		do {
			day++;
			money+=3;
			
			System.out.println(money);
			
			if (day % 6 ==0) {
				money -= 5;
			}
			
		} while (money < 100);
		
		System.out.println(day);

	}
}

4.方法的定义和调用(重中之重)

	### 1.如何引出方法:

​ 通过举例子,龙珠的布尔玛的智能胶囊引出为什么要封 装方法;

​ 封装方法的原因:

​ 因为代码的重复书写,造成代码冗余,冗余代码,不仅给程序员本身带来巨大的读写痛苦,而且大量书写重复代码,也会造成浪费大量时间和精力,所以,我们可以把重复度高的代码进行封装,然后起一个便于理解和读写的名字,只要在需要这段代码的时候,对这段代码进行调用,就能实现我们想要的功能,这就是为什么要定义方法的原因,定义方法也是封装的一种体现

	### 2.方法的书写格式:

​ 修饰符 返回值类型 方法名(参数列表){

​ 方法体

​ }

​ 什么是无返回值方法:

public class Test10 {
	public static void main(String[] args) {
//		如果我在敲代码的过程中,每隔5行我就要浪一下
//		我非要打印100条1000行代码
//		你怎么做
		
//		1.每次都自己去敲着1000行代码
//		2.自己敲一遍,然后将这1000行代码装到一个文件里面,给这个文件起个名字,每次想要使用这1000行代码的时候
//				就调用这个文件的名字,他就能自动帮我们打印出那1000行代码
		
//		定义方法的格式: 修饰符   返回值类型   方法名(参数类型 参数列表){
		
//								方法体
//						}
//		参照main方法
		
//		
		printHelloWorld();
		printNum();
		
	}
	
	public static void  printHelloWorld(){
		for (int i = 0; i < 10; i++) {
			System.out.println("helloworld");
		}
		
	}
	
	public static void printNum(){
		for (int i = 0; i < 100; i++) {
			System.out.println(i);
		}
	}
	
}

3.方法如何调用:

​ 通过在main函数中直接书写方法名字加上小括号调用

4.如何定义有参方法,为什么要使用有参方法?

​ 通过定义一个求和方法,引出如何定义有参方法,然后如何对有参方法就行传递参数的操作

​ 什么时候可以定义有参方法?

​ 当我们想给自己定义的方法传递一些数据,让其参与运算的时候,就可以定义一个有参方法,通过给他传递参数,让其在内部参与计算,并且输入值

​ 案例设计:

public class Test11 {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		System.out.println("请输入第一个数");
		int a = sc.nextInt();
		System.out.println("请输入第二个数");
		int b = sc.nextInt();
		System.out.println("请选择要进行的运算:+-");
		String s = sc.next();
		
		if(s.equals("+")){
			getSum(a,b);
		}else{
			getCha(a,b);
		}
		
	}
	
	/**
	 * 1.求任意两个数的和的方法
	 * 
	 * 通过上面的需求发现,此方法这样实现不太符和需求
	 * 因为任意两个数字,是有用户键盘录入的
	 * 那么我们应该让键盘录入的两个数字在此方法里面进行求和运算
	 * 
	 * 2.我不单单手动录入两个数字了  我也不再需求一个方法   +-/*
	 * 
	 * 根据你录入的运算符  调用不同的方法
	 */
	public static void getSum(int i,int j){
		
		System.out.println(i + j);
		
	}
	
	public static void getCha(int a,int b){
	
		System.out.println(a - b);
		
	}
}

求三个数中的最大值:

public class Test12 {
/**
 * 键盘录入三个数,求三个数中的最大值
 */
	
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入第一个数字");
		int a = sc.nextInt();
		System.out.println("请输入第二个数字");
		int b = sc.nextInt();
		System.out.println("请输入第三个数字");
		int c = sc.nextInt();
		
		getMax(a, b, c);
	}
	
	public static void getMax(int a,int b,int c){
		System.out.println(a > b && a > c ? a : b > c ? b : c);
	}
}

5.如何定义有返回值的方法

​ 通过案例引出有返回值方法:比如求和案例

​ 什么是有返回值方法:

​ 如何定义有返回值方法

​ 如何返回方法的返回值:return

​ 为什么要使用有返回值的方法:

​ 当我们不需要方法进行打印输出,而只是想让他参与运算,并且把运算后的结果提供给我们的时候,这个时候,我们就可以选择使用有返回值的方法.

​ 如何接受方法的返回值

​ 案例设计:

6.什么是重载方法

​ 通过定义两个名字一样的方法引出方法的重载

​ 方法重载:方法名字一样,参数列表不同,与返回值无关的方法,我们都称之为方法的重载

​ 案例设计:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值