Java SE 04 循环结构

Java SE 04(循环结构)

1.循环结构语句

循环语句可以在满足循环条件的情况下,反复执行某一段代码,这段被重复执行的代码被称为循环体语句,当反复执行这个循环体时,需要在合适的时候把循环判断条件修改为false,从而结束循环,否则循环将一直执行下去,形成死循环。

1.1 循环四要素:

循环初始化:一条或多条语句,这些语句完成一些初始化操作,让循环开始执行
循环继续条件:控制循环是否继续或结束的条件
循环体:需要被循环执行的代码
循环步长(间距,周期):当前循环和下一次循环之间的差量

2. for 循环

2.1 for 循环具有编写循环的简明语法。
2.2 for 循环的语法如下:
for(初始化语句;继续条件;步长){
	循环体;
}
2.3执行流程:
  1. 执行初始化语句
  2. 执行继续条件判断执行继续条件判断
    如果是true,继续执行
    如果是false,循环结束
  3. 执行循环体语句
  4. 执行步长
  5. 回到 2 继续
    在这里插入图片描述

2. while 循环

2.1 while 循环在条件为真的情况下,重复地执行语句。
2.2 while循环语句格式:
	while(循环继续条件){
		循环体语句
	}
或
	循环初始化语句
	while(循环继续条件){
		循环体语句
		步长
	}
2.3 执行流程

在这里插入图片描述

3. do - while 循环

3.1 do-while 循环和 while 循环基本一样,不同的是它先执行循环体一次,然后判断循环继续条件。
3.2 do-while 循环是 while 循环的变体。它的语法如下:
do {
 循环体 ;
语句(组 );
} while ( 循环继续条件);
3.3 执行流程
  1. 执行循环体
  2. 计算循环继续条件
    如果计算结果为 true, 则重复执行循环体
    如果为 false,则终止 do-while 循环。
    在这里插入图片描述

4.循环结构while语句和for语句的区别

for循环语句和while循环语句可以等价转换

最大区别:

for循环针对一个范围判断进行操作
while循环适合判断次数不明确操作

5.控制跳转语句

5.1 关键字 break 和 continue 在循环中提供了额外的控制。
5.2 控制跳转语句break
break的使用场景:
  • 在选择结构switch语句中
  • 在循环语句中
  • 离开使用场景的存在是没有意义的
break的作用:
  • 跳出单层循环
  • 跳出多层循环:带标签跳出,标签名:循环语句
5.3控制跳转语句continue
continue的使用场景:
  • 在循环语句中
  • 离开使用场景的存在是没有意义的
continue的作用:
  • 退出当前循环,开启下一次循环,也可带标签
5.4控制跳转语句return

return关键字不是为了跳转出循环体,更常用的功能是结束一个函数,也就是退出一个方法。
跳转到上层调用的方法。
本质上就是结束当前函数。

6. 关键术语

英文中文
break statementbreak 语句
continue statementcontinue 语句
do-while loopdo-while 循环
for loopfor 循环
infinite loop无限循环、死循环
input redirection输入重定向
iteration迭代
loop循环
loop body循环体
nested loop嵌套循环
off-by-one error差一错误
output redirection输出重定向
posttest loop后测循环
pretest loop前测循环
sentinel value标志值
while loopwhile 循环

编程练习题

  1. (统计正数和负数的个数然后计算这些数的平均值)编写程序,读入未指定个数的整数,判断读入的正数有多少个,读入的负数有多少个,然后计算这些输入值的总和及其平均值(不对 0 计数)。当输入为 0时,表明程序结束。将平均值以浮点数显示。

    下面是一个运行示例:

    Enter an integer, the input ends if it is 0:1 2-1 3 0 Enter
    The number of positives is 3
    The number of negatives is 1
    The total is 5.0
    The average is 1.25

    代码:

    import java.util.Scanner;
    class Demo4_1{
    	/*
    	数据:正数的个数 负数的个数 总和 平均值
    	指令:
    	1.循环输入数字
    	2.在输入的同时计算正数个数 复数个数 总和
    	3.当输入0时,结束循环
    	4.计算平均值
    	5.输出
    
    	循环:
    	1.NULL
    	2.while(true)
    	3.4.输入一个数字 判断正负
    		3.1 正 正数计数+1
    		3.2 负 负数计数+1
    		3.3 0 退出循环
    		都要进行累加
    	*/
    	public static void main(String[] args){
    		Scanner scanner=new Scanner(System.in);
    		int positiveCount=0;//正数的个数
    		int negativeCount=0;//负数的个数
    		double sum=0;//总和
    		System.out.print("Enter an integer ,the inputs ends if it is 0:");
    		while(true){
    			int num=scanner.nextInt();
    			if(num==0){
    				break;
    			}else if(num>0){
    				positiveCount++;
    			}else{
    				negativeCount++;
    			}
    			sum+=num;
    		}	
    		double average=sum/(positiveCount+negativeCount);
    		System.out.println("The number of positives is "+positiveCount);
    		System.out.println("The number of negatives is "+negativeCount);
    		System.out.println("The total is "+sum);
    		System.out.println("The average is "+average);
    	}
    }
    
  2. class rDemo4_2{
    	public static void main(String[] args){
    		/*
    		1.如何打印:
    				i	j
    		*		1	1
    		**		2	1 2
    		***		3	1 2 3
    		****	4	1 2 3 4
    		*****	5	1 2 3 4 5
    		******	6	1 2 3 4 5 6
    		*/
    		for(int i=1;i<=6;i++){
    			//打印行
    			for(int j=1;j<=i;j++){
    				System.out.print("*");
    			}
    			//换行
    			System.out.println();
    		}
    
    		/*
    		2.如何打印:
    				k	i	j
    		     *	5	1	1
    		    **	4	2	1 2
    		   ***	3	3	1 2 3
    		  ****	2	4	1 2 3 4
    		 *****	1	5	1 2 3 4 5
    		******	0	6	1 2 3 4 5 6
    		*/
    		for(int i=1;i<=6;i++){
    			//打印空格
    			for(int k=1;k<=6-i;k++){
    				System.out.print(" ");
    			}
    			//打印星星
    			for(int j=1;j<=i;j++){
    				System.out.print("*");
    			}
    			//换行
    			System.out.println();
    		}
    		/*
    		3.如何打印:
    				i	j
    		*		1	1	
    		**		2	1 2
    		***		3	1 2 3
    		****	4	1 2 3 4
    		*****	5	1 2 3 4 5
    		******	6	1 2 3 4 5 6		j<=i
    		*****	7	1 2 3 4 5	
    		****	8	1 2 3 4
    		***		9	1 2 3
    		**		10	1 2
    		*		11	1				i+j<=12
    		j<=i&&i+j<=12
    		*/
    		for(int i=1;i<=11;i++){
    			for(int j=1;j<=i&&i+j<=12;j++){
    				System.out.print("*");
    			}
    			System.out.println();
    		}
    
    		/*
    		4.如何打印:
    				k(i-5)	i
    		     *		-4	1
    			* *		-3	2
    		   * * *	-2	3
    		  * * * *	-1	4
    		 * * * * *	0	5
    		  * * * *	1	6
    		   * * *	2	7
    		    * *		3	8
    			 *		4	9
    			y=|x|[-4,4]
    		*/
    		for(int i=1;i<=9;i++){
    			for(int k=1;k<=Math.abs(i-5);k++){
    				System.out.print(" ");
    			}
    			for(int j=1;j<=i&&i+j<=10;j++){
    				System.out.print("* ");
    			}
    			System.out.println();
    		}
    		/*
    		5.如何打印
    					i	j
    		     *		1	    1
    			* *		2	   1 2
    		   *   *	3	  1 2 3
    		  *     *	4	 1 2 3 4
    		 *       *	5	1 2 3 4 5
    		  *     *	6	 1 2 3 4 
    		   *   *	7	  1 2 3
    		    * *		8	   1 2
    			 *		9	    1
    		j==1 j==i i+j==10
    		*/
    		for(int i=1;i<=9;i++){
    			for(int k=1;k<=Math.abs(i-5);k++){
    				System.out.print(" ");
    			}
    			for(int j=1;j<=i&&i+j<=10;j++){
    				if(j==1||i==j||i+j==10){
    					System.out.print("* ");
    				}else{
    					System.out.print("  ");
    				}
    			}
    			System.out.println();
    		}
    		//打印乘法口诀表
    		//1*1=1
    		//2*1=1 2*2=4
    		//3*1 3*2 3*3
    		for(int i=1;i<=9;i++){
    			for(int j=1;j<=i;j++){
    				System.out.printf("%d*%d=%d\t",i,j,i*j);
    			}
    			System.out.println();
    		}
    	}
    }
    
  3. (显示金字塔)编写程序,提示用户输入一个在1到 15 之间的整数,然后显示一个金字塔形状的图案,如下面的运行示例所示:
    在这里插入图片描述

    import java.util.Scanner;
    class Demo4_3{
    	public static void main(String[] argss){
    		Scanner scanner=new Scanner(System.in);
    		System.out.print("Enter the number of lines:");
    		int num=scanner.nextInt();
    		for(int i=1;i<=num;i++){
    			//空格
    			for(int k=1;k<=num-i;k++){
    				System.out.print("  ");
    			}
    			//数字
    			//利用 y= |x|+1 函数
    			for(int j=-(i-1);j<=i-1;j++){
    				System.out.print(Math.abs(j)+1+" ");
    			}
    			System.out.println();
    		}
    	}
    }
    
  4. (打印金字塔形的教字)编写一个嵌套的 for 循环,打印下面的输出:
    在这里插入图片描述
    代码:

    class Demo3_17{
    	/*						i		j(2^j)			
    			1				1		0		
    		 1  2  1			2		0  1  0    		
    	  1  2  4  2  1			3		0  1  2  1  0	
       1  2  4  8  4  2  1		4		0  1  2  3  2  1  0			
    
    	4				0  1  2  3  2  1  0			
    	|x|	(-3,3)		3  2  1  0  1  2  3	
    	|x|-3			0 -1 -2 -3 -2 -1  0
    	||x|-3|			0  1  2  3  4  5  6
    	*/
    	
    	public static void main(String[] args){
    		int num=8;
    		for(int i=1;i<=num;i++){
    			//空格
    			for(int k=0;k<num-i;k++){
    				System.out.print("    ");
    			}
    			//数字 
    			//利用y=2^||x|-3| 函数
    			for(int j=-(i-1);j<i;j++){
    				System.out.printf("%4d",(int)(Math.pow(2,Math.abs(Math.abs(j)-i+1))));
    			}
    			System.out.println();
    		}
    	}
    }
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值