javase的for循环

循环

一、for循环

1.语法

		/**
		 * for循环是对while循环的语法升级,把循环的三要素放在一起,便于管理,提高代码的可读性
		 */
//		for(第一要素;第二要素;第三要素) {
//			循环体
//		}
		for(int i=1;i<=5;i++) {
			System.out.println("hello world-----"+i);
		}

2.案例

//	    1.打印1~100之间 所有的偶数
		for(int i=1;i<=100;i++) {
			if(i%2 == 0) {
				System.out.println(i);
			}
		}

//	    2.求1~100之间 所有偶数的和
		//定义变量存储累加和
		int sum = 0;
		for(int i=1;i<=100;i++) {
			if(i%2 == 0) {
				//如果i是偶数则累加
				sum = sum + i;
			}
		}
		System.out.println(sum);
//	    3.求1~100之间 偶数出现的次数
		//定义变量计数
		int count = 0;
		for(int i=1;i<=100;i++) {
			if(i%2 == 0) {
				count++;
			}
		}
		System.out.println(count);

二、流程控制语句

break(结束)--结束循环
continue(继续)--结束本次循环,继续下一次循环

1.break

image-20210122152937851

		for(int i=1;i<=10;i++) {
			if(i == 5) {
				break;
			}
			System.out.println("冀天赐跑第"+i+"圈");
		}

2.continue

image-20210122153013792

		for (int i = 1; i <= 10; i++) {
			if(i == 5) {
				continue;
			}
			System.out.println("冀天赐跑第" + i + "圈");
		}

三、总结

如果知道要循环多少次,优先选择for循环

如果不知道要循环多少次,优先选择while循环

案例

有一张白纸,厚度1mm,对这个白纸进行对折,问对折多少次超过100mm
		//定义变量计数
		int count = 0;
		//定义变量存储纸的厚度
		int height = 1;
		while(true) {
			//对折
			height = height*2;
			//统计次数
			count++;
			//当纸的厚度大于100时结束循环
			if(height >100) {
				break;
			}
		}
		System.out.println(count);
1万元,存入银行,年利率是3%,多少年后本息和超过10万元
		//定义变量计数
		int count = 0;
		//定义变量存储本金
		double money = 10000;
		while(true) {
			//计算每年的本息和
			money = money + money*0.03;
			
			count++;
			//当本息和大于10万时结束循环
			if(money > 100000) {
				break;
			}
		}
		System.out.println(count);
1万元,存入银行,年利率是3%,10年后本息和是多少
		//定义变量存储本金
		double money = 10000;
		for(int i=1;i<=10;i++) {
			//计算每年的本息和
			money = money + money*0.03;
		}
		System.out.println(money);

四、ASCII码

所有的计算机设备都内置的有一张表格,在这个表格里面描述了每一个常见字符所对应的十进制数

image-20210625182307769

语法

		/**
		 * 每一个字符都对应这一个十进制整数
		 */
		char a = ' ';
		//字符可以自动转换为int
		int n = a;
		System.out.println(n);
		
		
		/**
		 * 十进制整数可以转字符
		 */
		int m = 122;
		//int强转为字符
		char b = (char) m;
		System.out.println(b);

案例1

//接收用户输入一个字符,判断它是数字还是大写字母,还是小写字母,还是其它符号
常规做法
		Scanner input = new Scanner(System.in);
		System.out.println("请输入一个字符");
		//先用字符串接收
		String s = input.next();
		//把字符串转字符
		char a = s.charAt(0);
		System.out.println(a);
		//接收用户输入一个字符,判断它是数字还是大写字母,还是小写字母,还是其它符号
		//把字符串转成整数
		int n = a;
		if(n>=48 && n<=57) {
			System.out.println("是数字");
		}else if(n>=65 && n<=90) {
			System.out.println("是大写字母");
		}else if(n>=97 && n<=122) {
			System.out.println("是小写字母");
		}else {
			System.out.println("是其它符号");
		}

简化做法
    	/**
		 * 字符在参与运算时会自动转int,再做运算符
		 */
		Scanner input = new Scanner(System.in);
		System.out.println("请输入一个字符");
		//先用字符串接收
		String s = input.next();
		//把字符串转字符
		char a = s.charAt(0);
		System.out.println(a);
		//接收用户输入一个字符,判断它是数字还是大写字母,还是小写字母,还是其它符号
		//把字符串转成整数

		if(a>='0' && a<='9') {
			System.out.println("是数字");
		}else if(a>='A' && a<='Z') {
			System.out.println("是大写字母");
		}else if(a>='a' && a<='z') {
			System.out.println("是小写字母");
		}else {
			System.out.println("是其它符号");
		}
    

案例2

//接收用户输入一个字符,如果是大写字母,把它转为小写字母,如果是小写字母,把它转为大写字母
		Scanner input = new Scanner(System.in);
		System.out.println("请输入一个字符");
		//先用字符串接收
		String s = input.next();
		//把字符串转字符
		char a = s.charAt(0);
		//判断并转换
		if(a>='A' && a<='Z') {
			System.out.println((char) (a +32));
		}else if(a>='a' && a<='z') {
			System.out.println((char) (a -32));
		}else {
			System.out.println("您输入的不是字母");
		}

案例3

		//输出不换行
		System.out.print("----------------");
		//输出并换行
		System.out.println("************");
使用循环将a - j这十个字母,按下图的要求的格式输出(使用ascii码)

a    b

c    d

e    f

g    h

i    j
		for(char i='a';i<='j';i++) {
			System.out.print(i);
			//偶数换行
			if(i%2==0) {
				System.out.println();
			}
		}

五、循环嵌套

案例一

使用循环打印一个9*9的正方形,要求每一个输出语句只输出一个星

*  *  *  *  *  *  *  *  *
*  *  *  *  *  *  *  *  *
*  *  *  *  *  *  *  *  *
*  *  *  *  *  *  *  *  *
*  *  *  *  *  *  *  *  *
*  *  *  *  *  *  *  *  *
*  *  *  *  *  *  *  *  *
*  *  *  *  *  *  *  *  *
*  *  *  *  *  *  *  *  *

推导过程

image-20220325165816205

		for(int i=1;i<=9;i++) {
			for(int j=1;j<=9;j++) {
				System.out.print("* ");
			}
			System.out.println();
		}

案例二

使用循环打印9*9等腰直角三角形,要求每一个输出语句只输出一个星

*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * * * * *

推导过程

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-n4FZRHFx-1661443866670)(C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\image-20220325173744293.png)]

		System.out.println("开始");
		for(int i=1;i<=9;i++) {
			for(int j=1;j<=i;j++) {
				System.out.print("* ");
			}
			System.out.println();
		}

案例三

打印九九乘法表

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 

img

		for(int i=1;i<=9;i++) {
			for(int j=1;j<=i;j++) {
				System.out.print(j+"*"+i+"="+(j*i)+" ");
			}
			System.out.println();
		}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值