java流程控制——循环结构

循环结构 :

①根据循环条件,重复性的执行某段代码。 
②while、do…while、for种循环语句。 
注:JDK1.5提供了foreach循环,方便的遍历集合、数组元素。

衡量一个功能代码的优劣:
①正确性;
②可读性;
③健壮性;
④高效率与低存储:时间复杂度、空间复杂度(衡量算法的好坏)。

如何理解流程控制的练习:
流程控制结构的使用 + 算法逻辑

for循环

for循环结构的使用:

一、循环结构的4个要素
    ①初始化条件;
    ②循环条件;--是boolean类型。
    ③循环体;
    ④迭代条件;

二、for循环的结构

for(①;②;④){
    ③;
}

执行过程:①->②->③->④->②->③->④->②->③->④->...->②;

class ForTest {
	public static void main(String[] args) {
		//输出5次 方式一:
		System.out.println("Hello World!");
		System.out.println("Hello World!");
		System.out.println("Hello World!");
		System.out.println("Hello World!");
		System.out.println("Hello World!");

		//输出5次 方式二:
		for (int i = 1;i <= 5 ; i++){
			System.out.println("Hello World!");
		}
		//i:只在for循环内有效,出了循环就失效了。

		//练习:
		int num = 1;
		for (System.out.print('a');num <= 3 ;System.out.print('c'),num++ ){
			System.out.println('b');
		}
		//输出结果:abcbcbc

		//例题:遍历100以内的偶数,并输出所有偶数的和,输出偶数的个数。
		int sum = 0;//记录所有偶数的和。
		int count = 0;//记录偶数的个数。
		for (int i = 1;i <= 100 ;i++ ){
			if (i % 2 == 0){
				System.out.println(i);
				sum += i;
				count++;
			}
		}
		System.out.println("偶数的总和为:" + sum);
		System.out.println("偶数的个数为:" + count++);
	}
}

for循环练习1

1、编写程序从1循环到150,并在每行打印一个值,另外在每个3的倍数行
上打印出“foo”,在每个5的倍数行上打印“biz”,在每个7的倍数行上打印
输出“baz”。
 

class ForExer1 {
	public static void main(String[] args) {

		System.out.println("方式一:");
		//方式一:
		for (int i = 1;i <= 150;i++){
			if (i % 3 == 0 && i % 5 ==0 && i % 7 ==0){
				System.out.println(i + ",foo"+",biz"+",baz");
			}else if (i % 3 ==0 && i % 5 ==0){
				System.out.println(i + ",foo"+",biz");
			}else if (i % 3 ==0 && i % 7 ==0){
				System.out.println(i + ",foo"+",baz");
			}else if (i % 5 ==0 && i % 7 ==0){
				System.out.println(i + ",biz"+",baz");
			}else if (i % 3 == 0){
				System.out.println(i + ",foo");
			}else if (i % 5 == 0){
				System.out.println(i + ",biz");
			}else if (i % 7 ==0){
				System.out.println(i + ",baz");
			}else{
				System.out.println(i);
			}
		}

		System.out.println("方式二:");
		//方式二:
		for (int i = 1;i <= 150;i++){
			System.out.print(i + " ");
			 if (i % 3 == 0){
				System.out.print("foo ");
			}
			if (i % 5 == 0){
				System.out.print("biz ");
			}
			if (i % 7 ==0){
				System.out.print("baz ");
			}
			System.out.println();
		}
		
	}
}

for循环练习2

2、题目:输入两个正整数m和n,求其最大公约数和最小公倍数。
    比如:12和20的最大公约数是4,最小公倍数是60。
    说明:break关键字的使用

import java.util.Scanner;
class ForExer2{
	public static void main(String[] args){

		Scanner scan = new Scanner(System.in);
		System.out.println("请输入正整数m:");
		int m = scan.nextInt();
		System.out.println("请输入正整数n:");
		int n = scan.nextInt();
		
		//获取最大公约数
		//1.获取两个数的较小值
		int min = (m >= n)? n : m;
		//2.遍历
		for (int i = min;i >= 1;i--){
			if (m % i == 0 && n % i == 0){
				System.out.println("最大公约数为:" + i);
				break;//一旦循环中执行到break,就跳出循环。
			}
		}

		//获取最小公倍数
		//1.获取两个数中的较大值
		int max = (m >= n)?m : n;
		//2.遍历
		for (int i = max;i <= m * n ;i++ ){
			if (i % m ==0 && i % n == 0){
				System.out.println("最小公倍数为:" + i);
				break;
			}
		}
		
	}
}

for循环练习3

3、打印1~100之间所有奇数的和。

class ForExer3 {
	public static void main(String[] args) {

		int sum = 0;
		for (int i = 1;i <= 100;i++){
			if (i % 2 != 0){
				sum += i;
			}
		}
		System.out.println("100以内的奇数之和为:" + sum);
	}
}

for循环练习4

4、打印1~100之间所有是7的倍数的整数的个数及总和(体会设置计数器的思想)

class ForExer4 {
	public static void main(String[] args) {
		
		int sum = 0;
		int count = 0;
		for (int i = 1; i <= 100;i++ ){
			if (i % 7 ==0){
				sum += i;
				count++;
			}
		}
		System.out.println("100以内7的倍数的总和为:" + sum);
		System.out.println("100以内7的倍数的个数为:" + count);
	}
}

for循环练习5

5、输出所有的水仙花数,所谓水仙花数是指一个3位数,其各个位上数
字立方和等于其本身。
例如: 153 = 1*1*1 + 3*3*3 + 5*5*5
 

class ForExer5{
	public static void main(String[] args) {
		
		for (int i = 100;i <= 999;i++){
			int B = i / 100;
			int S = i % 100 /10;
			int G = i % 10;
			if (B*B*B + S*S*S + G*G*G == i){
				System.out.println(i);
			}
		}
	}
}

while循环

while循环的结构

①初始化条件;
while(②循环条件部分){
    ③循环体部分;
    ④迭代部分;
}

执行过程:①->②->③->④->②->③->④->②->③->④->...->②;

说明:
1.写while循环千万小心不要丢了迭代条件。一旦丢了,可能导致死循环!
2.我们写程序,要避免出现死循环。
3.for循环和while循环是可以相互转换的! 
    区别:for循环和while循环的初始化条件部分的作用范围不同。

算法:有限性。

class WhileTest1 {
	public static void main(String[] args) {
		
		//遍历100以内的所有偶数
		int i = 0;
		while (i <= 100){
			if (i % 2 == 0){
				System.out.println(i);
			}
			i++;
		}
		//出了while循环以后,仍能调用i。
		System.out.println(i);//100
	}
}

do-while循环

do-while循环的结构

①;
do{
    ③;
    ④;
}while(②);

执行过程:执行过程:①->③->④->②->③->④->②->③->④->...->②;

说明:
1.do-while循环至少会执行一次循环体!
2.开发中,使用for和while更多一些。
 

class DoWhileTest1 {
	public static void main(String[] args) {
		
		//遍历100以内的偶数,并计算所有偶数的和及偶数的个数。
		int i = 1;
		int sum = 0;//记录总和
		int count = 0;//记录个数
		do{
			if (i % 2 ==0){
				System.out.println(i);
				sum += i;
				count++;
			}
			i++;
		}
		while (i <= 100);

		System.out.println("总和为:" + sum);
		System.out.println("个数为:" + count);


		//************体会do-while至少执行一次循环体******************
		int number1 = 10;
		while (number1 > 10){
				System.out.println("hello:while");
				number1--;
		}


		int number2 = 10;
		do{
			System.out.println("hello:do-while");
			number2--;
		}
		while (number2 >10);

	}
}

循环结构综合练习

题目:
    从键盘读入个数不确定的整数,并判断读入的正数和负数的个数,输入为0时结束程序。

说明:
1.“无限循环”:不在循环条件部分限制次数的结构:for(;;)或while(ture)。
2.结束循环有几种方式?
    方式一:循环条件部分返回false
    方式二:在循环体中,执行break

import java.util.Scanner;
class ForWhileExer1 {
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);

		int positiveNumber = 0;//记录正数的个数
		int negativeNumber = 0;//记录负数的个数

		for(;;){//while (true){
			System.out.println("请输入任意的整数:");
			int number = scan.nextInt();
			//判断number的正负情况
			if (number > 0){
				positiveNumber++;
			}else if (number < 0){
				negativeNumber++;
			}else{
				//一旦执行break,跳出循环
				break;
			}
		}

		System.out.println("输入正数的个数为:" + positiveNumber);
		System.out.println("输入负数的个数为:" + negativeNumber);
	}
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

MoerPerfect

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

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

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

打赏作者

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

抵扣说明:

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

余额充值