Java基础 (for循环与while循环)

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

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

for循环语句格式:
for(初始化语句;继续条件;步长){
	循环体
}

执行流程:
A.执行初始化语句
B.执行继续条件判断
   如果是true,继续执行
   如果是false,循环结束
C.执行循环体语句
D.执行步长
E.回到B继续

循环结构for语句的注意事项:

  • 循环继续条件语句的结果是一个boolean类型
  • 循环体语句如果是一条语句,大括号可以省略
  • 有左大括号就没有分号,有分号就没有左大括号

while循环:

while循环语句格式:

	while(循环继续条件){
		循环体语句
	}
或
	循环初始化语句
	while(循环继续条件){
		循环体语句
		步长
	}

循环结构while语句和for语句的区别:
for循环语句和while循环语句可以等价转换
最大区别:

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

循环结构循环注意事项之死循环:

while(true){}
for(;;)

控制跳转语句break:
break的使用场景:

  • 在选择结构switch语句中
  • 在循环语句中
  • 离开使用场景的存在是没有意义的

break的作用:

  • 跳出单层循环跳出多层循环:
  • 带标签跳出,标签名:循环

控制跳转语句continue:
continue的使用场景:

  • 在循环语句中
  • 离开使用场景的存在是没有意义的

continue的作用:

  • 退出当前循环,开启下一次循环,也可带标签

题一:
在这里插入图片描述
Scanner类常用方法:
  Scanner scanner=new Scanner(System.in);
   int line=scanner.nextInt();

1、String next():接收控制台输入的字符串(备注:不能将空格作为字符串接收) ;
2、String nextLine():接收控制台输入的字符串;
3、int nextlnt():接收控制台输入的int类型的数据;
4、double nextDouble:接收控制台输入的double类型的数据;
5、boolean nextBoolean):接收控制台输入的boolean类型的数据;
6、输入char类型的数据;
Scanner类没有直接输入char类型的方法,可以通过charAt()方法从next()或nexyLine()获取。

%-3d含义:

定于输出格式。d表示输出整数,3表示输出的数字占3个字符的位置。-号表示对齐方式。是左对齐。如果是+号或者不写,表示右对齐。

*代码:*
package javase;
import java.util.Scanner;
public class day01 {

	public static void main(String[] args) {
		Scanner scanner=new Scanner(System.in);
		System.out.print("Enter the line:");
		int line=scanner.nextInt();
		for(int i=1;i<=line;i++){		//行数
			for(int k=1;k<=line-i;k++){
				System.out.print("   ");
			}
	
			for(int j=-i;j<=i;j++){    //对于金字塔的类型使用绝对值思想
				if(j!=0&&j!=1){
					System.out.printf("%-3d",(int)(Math.pow(2,i-Math.abs(j))));
				}
			}
			System.out.println();			
		}
	}
}

前三行指数表示:   
   0
 0 1 0
0 1 2 1 0
总结:Math.pow(2,i-Math.abs(j))//i为行数,j为 第二个for循环所赋的循环变量。

编写过程中出现的错误:

请输入行数:4
Exception in thread “main” java.util.IllegalFormatConversionException: d != java.lang.Double
原因:Math.power()为double类型,此处需要加(int)。

运行结果:

请输入行数:8
                            1   
                        1   2   1   
                    1   2   4   2   1   
                1   2   4   8   4   2   1   
            1   2   4   8   16  8   4   2   1   
        1   2   4   8   16  32  16  8   4   2   1   
    1   2   4   8   16  32  64  32  16  8   4   2   1   
1   2   4   8   16  32  64  128 64  32  16  8   4   2   1   

题二:
java编写输出各种形状的三角

package javase;
public class Test4_5 {
	public static void main(String[] args) {
		/*
			*
			**
			***
			****
			*****
		*/
		for(int i=1;i<=5;i++){
			for(int j=1;j<=i;j++){
				System.out.print("*");
			}
			System.out.println();
		}
		/*		
			*******
			******
			*****
			****
			***
			**
			*
		*/
		for(int i=1;i<=7;i++){
//			for(int j=1;j<=8-i;j++){
//				System.out.print("*");
//			}
			for(int j=8-i;j>=1;j--){
				System.out.print("*");
			}
			System.out.println();
		}
		
		
		/*
			 -----*
			 ----**
			 ---***
			 --****
			 -*****
			 ******
		*/
		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();
		}
		
		/*
					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
			*****	7	 1 2 3 4 5 	6 7
			****	8	  1 2 3 4 	5 6 7 8 
			***		9	   1 2 3 		4 5 6 7 8 9
			**		10	    1 2 		3 4 5 6 7 8 9 10
			*		11	     1 			2 3 4 5 6 7 8 9 10 11
		*/
		System.out.println("=============================================");
		for(int i=1;i<=11;i++){
			for(int j=1;j<=i&&(i+j)<=12;j++){
				System.out.print("*");
			}
			System.out.println();
		}
		/*				i	k
			----*		1	-4	
			---* *		2	-3
			--* * *		3	-2
			-* * * *	4	-1
			* * * * *	5	0
			-* * * *	6	1
			--* * *		7	2
			---* *		8	3
			----*		9	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();
		}
		
		/*   
			    *
			   * *
			  *   *
			 *     *
			*       *
			 *     *
			  *   *
			   * *
			    *
		 */
		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||j==i||j+i==10){
					System.out.print("* ");
				}else{
					System.out.print("  ");
				}
			}
			System.out.println();
		}
	}
}	

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值