稳稳当当学java之循环(5)

第七章 循环

1.作业回顾

1,男大当婚,女大当嫁。如果女方家长要嫁女儿,当然要提出一定的条件。高:180cm以上;富:财富1千万以 上;帅:是。如果这三个条件同时满足,则:输出我一定要嫁给他,

如果三个条件中有为真的情况,则:比上 不足,比下有余,嫁吧

如果三个条件都不满足,则:不嫁。

import java.util.Scanner;

public class Day071 {
	public static void main(String[] args) {
		/*3,男大当婚,女大当嫁。如果女方家长要嫁女儿,当然要提出一定的条件。
		高:180cm以上;富:财富1千万以 上;帅:是。如果这三个条件同时满足,则:输出我一定要嫁给他,
		如果三个条件中有为真的情况,则:比上 不足,比下有余,嫁吧
		如果三个条件都不满足,则:不嫁。*/
		Scanner s = new Scanner(System.in);
		System.out.println("身高:");
		int height = s.nextInt();
		System.out.println("财富:");
		long wealth = s.nextLong();
		System.out.println("帅吗?");
		boolean handsome = s.nextBoolean();
		
		if(height >= 180 && wealth >= 10000000 && handsome){
			System.out.println("我一定要嫁给他");
		}else if(height < 180 && wealth < 10000000 && !handsome){
			System.out.println("不嫁");
		}else{ //满足一个条件或者两个条件的都进入else代码块
			System.out.println("比上不足,比下有余,嫁吧");
		}
		s.close();
	}
}

2,编写程序,从键盘接受整数,如果为1-7,打印对应的星期值,否则,打印非法数字。

import java.util.Scanner;

public class Day072 {
	public static void main(String[] args) {
		// 4,编写程序,从键盘接受整数,如果为1-7,打印对应的星期值,否则,打印非法数字。
		System.out.println("请输入1—7:");
		Scanner s = new Scanner(System.in);
		int num = s.nextInt();
		switch (num) {
		case 1:
			System.out.println("星期一");
			break;
		case 2:
			System.out.println("星期二");
			break;
		case 3:
			System.out.println("星期三");
			break;
		case 4:
			System.out.println("星期四");
			break;
		case 5:
			System.out.println("星期五");
			break;
		case 6:
			System.out.println("星期六");
			break;
		case 7:
			System.out.println("星期日");
			break;
		default:
			System.out.println("非法数字");
			break;
		}
		s.close();
	}
}

2.while 循环

循环就是一遍又一遍的执行同样的代码。

public class Day073 {
	public static void main(String[] args) {
		//使用while循环输出5个你好
		int count = 1; //计数器
		//while循环每次执行代码块之前,先检查括号中的条件表达式是否为true
		//如果条件表达式为true,那么就执行代码块
		//当条件表达式的结果为false,循环终止
		while(count <= 5) {
			System.out.println("你好");
			count++;
		}
		System.out.println("while循环终止");
		
		int x = 0;
		while(x < 5) {
			System.out.print(x);
			x++;
		}//01234
		
		int y = 5;
		while(y < 5) {
			System.out.print(y);
			y++;
		}//什么也不输出
	}
}

3.do while 循环

do while循环和while循环的区别是,do while循环至少会执行一次 。

do while循环先执行一次代码块,然后再检查条件是否满足。

public class Day074 {
	public static void main(String[] args) {
		int count = 5;
		do {
			System.out.println(count);
		}while(count < 5); //当条件为true时继续执行
		//5
		
		int x = 0;
		do {
			System.out.print(x);
			x++;
		}while(x < 5);
		//01234
		
		System.out.println(); //换行
		
		int y = 5;
		do {
			System.out.print(y);
			y--;
		}while(y > 0);
		//54321	
	}
}

4.for 循环

4.1 for自带初始化语句和自增语句

public class Day075 {
	public static void main(String[] args) {
		int x = 0;
		while(x < 5) {
			System.out.print(x);
			x++;
		}//01234
		
		System.out.println(); //换行
		
		//使用for循环达到上面while的效果
		//for循环包含三个部分
		//初始化语句: int i = 0;初始化语句只执行一次
		//条件语句: i < 5;代码块每次执行之前,都要检查条件语句是否满足,满足就执行,不满足for循环就终止
		//操作语句: i++;每次代码块结束之后,执行一次操作语句
		for (int i = 0; i < 5; i++) {
			System.out.print(i);
		}//01234
		
		System.out.println(); //换行
		
		//摘出来没有意义
		int i = 0;
		for (; i < 5;) {
			System.out.print(i);
			i++;
		}//01234
	}
}

4.2 可以有多个初始化语句和操作语句

public class Day0751 {
	public static void main(String[] args) {
		for (int i = 0, j = 9; i < 5 || j > 6; i++, j--) {
			//for快捷键 Alt + /
			System.out.print(i);
			System.out.print(j);
			System.out.println();
		}
//		09
//		18
//		27
//		36
//		45
	}
}

4.3 for循环的嵌套

import java.util.Scanner;

public class Day0752 {
	public static void main(String[] args) {
		// for循环的嵌套 
		// 每个练习都有1-3题,做1-4练习 
		// 练习1的第1题
		// 练习1的第2题
		// 练习1的第3题 
		// 练习2的第1题 
		// 练习2的第2题 
		// 练习2的第3题
		// 练习3的第1题 
		// 练习3的第2题 
		// 练习3的第3题 
		// 练习4的第1题
		// 练习4的第2题 
		// 练习4的第3题
		
		//外层for循环
		for (int i = 1; i <= 4; i++) {
			//内层for循环
			for (int j = 1; j <= 3; j++) {
				System.out.println("练习" + i + "第" + j + "题"); //12次
			}	
		}
		
		Scanner s = new Scanner(System.in);
		System.out.println("做几个练习?");
		int a = s.nextInt(); //练习几
		System.out.println("每个练习题几道题?");
		int b = s.nextInt(); //第几题
	
		for (int i = 1; i <= a; i++) {
			//内层for循环
			for (int j = 1; j <= b; j++) {
				System.out.println("练习" + i + "第" + j + "题"); //12次
			}	
		}	
	}
}

4.4 while和do while的嵌套

public class Day0753 {
	public static void main(String[] args) {
		//while 和 do while 也可以嵌套
		//变量的作用域,在哪声明就在哪个大括号中
		int i = 1;
		while(i <= 4) {
			int j = 1;
			while(j <= 3) {
				System.out.println("练习" + i + "第" + j + "题");
				j++;
			}
			i++;
		}
	}
}

5.break语句

break语句可以使循环终止。

public class Day076 {
	public static void main(String[] args) {
		for (int i = 0; i < 10; i++) {
			System.out.print(i);
		}//0123456789
		
		System.out.println();
		System.out.println("******************");
		
		//break语句使循环终止
		//break在那个循环体中,就会导致哪个循环体终止
		for (int i = 0; i < 10; i++) {
			System.out.print(i);
			//当 i == 5时,执行break语句,循环终止
			if(i == 5) {
				break;
			}
		}//012345
		
		System.out.println();
		System.out.println("******************");
		
		//break在那个循环体中,就会导致哪个循环体终止
		for (int i = 0; i < 2; i++) {
			for (int j = 0; j < 10; j++) {
				System.out.print(i + "" + j + " "); //字符串连接,防止数字相加
				if(j == 5) {
					break;
				}
			}	
		}//00 01 02 03 04 05 10 11 12 13 14 15  	
	}	
}

6.continue 语句

continue语句会跳过代码块后续的语句,并开始执行下一个迭代。

public class Day077 {
	public static void main(String[] args) {
		for (int i = 0; i < 10; i++) {
			System.out.print(i);
		}//0123456789
		
		System.out.println();
		System.out.println("******************");
		
		//continue语句会跳过代码块后续的语句,并开始执行下一个迭代
		//continue在哪个循环体中,就会跳过哪个循环体后续的语句
		for (int i = 0; i < 10; i++) {
			//当 1 == 5时,执行continue语句,会跳过代码块后续的语句,并开始下一个迭代
			if(i == 5) {
				continue;
			}
			System.out.print(i);
		}//012346789
	}
}
public class Day0771 {
	public static void main(String[] args) {
		//continue和break都可以在while和do while循环中使用
		int x = 0;
		while(x < 10) {
			x++;
			if(x == 5) {
				continue;
			}
			System.out.print(x);
		}//1234678910
		
		System.out.println();
		System.out.println("******************");
		
		//与break一样,continue在哪个循环中,就对哪个循环起作用
		for (int i = 0; i < 2; i++) {
			for (int j = 0; j < 10; j++) {
				if(j == 5) {
					continue; //只对内层循环起作用
				}
				System.out.print(i + "" + j + " ");
			}		
		}//00 01 02 03 04 06 07 08 09 10 11 12 13 14 16 17 18 19	
	}
}

7.练习

1,使用for循环求1-100的和。

2,使用for循环打印1-100的偶数。

3,使用嵌套的for循环打印99乘法表。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

十年之伴

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值