Java流程控制之循环结构(附案例说明)超详细

循环结构:根据循环条件,重复性执行某段代码

for循环 while 循环 do-while 循环

凡是循环 就有4个要素: 1、初始化要素2、循环条件(一定是boolean类型的变量或表达式) 3、循环体 4、迭代部分

for 循环格式

for(1;2;4){

3;

}执行过程 1-2-3-4-2-3-4......

输出结果:abcbc

案例:求水仙花数,水仙花数是一个3位数,其各个上数字立方和等于其本身

153=1*1*1+3*3*3+5*5*5

class  ForTest{
	public static void main(String[] agrs){
		for(int i=100;i<=999;i++){
			int ge =i%10;
 			int shi =i/10%10;
			int bai =i/100;
			
			if(i==ge*ge*ge+shi*shi*shi+bai*bai*bai){
				System.out.println(i);

			}
		}
        }
}

while 循环结构

1

while (2){

3

4

}

执行过程1-2-3-4-2-3-4-........

案例:随机生成一个100以内的数,猜这个随机数是多少

从键盘输入数,如果大了,提示大了,如果小了,提示小了,对了就不再猜了,并统计一个材料多少次。

import java.util.Scanner;
class  WhileTest{
	public static void main(String[] agrs){
		int random =(int)(Math.random()*100+1);
		Scanner scan =new Scanner(System.in);
		System.out.println("请输入1-100范围的一个整数\n");
		int guess = scan.nextInt();
		int count=1;//初始应该是1
		while(random!=guess){
			if(guess>random){
				System.out.println("输入大了\n");

			}else{
				System.out.println("输入小了\n");

			}
			System.out.println("请输入1-100范围的一个整数\n");
			guess=scan.nextInt();
			count++;
		}
		System.out.println("猜中了!!!\n");
		System.out.println("猜了"+count+"次");
		scan.close();
	}
}		 

do-while循环结构

1

do{

3

4

}while(2);

执行过程 1-3-4-2-3-4-2-......

do-while循环结构在开发中相对于while来说,使用的较少

案例:题目:模拟ATM取款

声明变量balance并初始化0,用以表示银行账户余额,通过ATM实现存款,取款功能

1、存款

2、取款

3显示余额

4退出

import java.util.Scanner;
class  DoWhileTest{
	public static void main(String[] agrs){
	double balance=0.0;
	boolean flag=true;
	Scanner scan =new Scanner(System.in);
	do{
		System.out.println("===========ATM==========");
		System.out.println("1、存款");
		System.out.println("2、取款");
		System.out.println("3、显示余额");
		System.out.println("4、退出");
		System.out.println("请选择(1-4)");
		
		
		int selection = scan.nextInt();
		switch(selection){
			case 1:
				System.out.println("输入存款金额");
				double money1=scan.nextDouble();
				if(money1>0){
					balance+=money1;
				}
				break;
			case 2:
				System.out.println("输入取款金额");
				double money2=scan.nextDouble();
				if(money2>0&&money2<=balance){
					balance-=money2;
				}else{
					System.out.println("输入数据有误,金额不足");

				}

				break;
			case 3:
				System.out.println("账户余额"+balance);

				break;
			case 4:
				flag=false;
				break;
			default:
				System.out.println("输入有误,重新输入");
		}
		
	}while(flag);
	scan.close();
	}
}		

无限循环 :while(ture) for(;;)

死循环后面不能有执行语句

嵌套循环

打印菱形:

class ForForTest{
	public static void main(String[] agrs){
		for(int i=1;i<=5;i++){
			for(int j=1;j<=10-2*i;j++){
				System.out.print(" ");
			}
			for(int k=1;k<=2*i-1;k++){
				System.out.print("* ");
			}
			System.out.println();
		}
		for(int i=1;i<=4;i++){
			for(int j=1;j<=2*i;j++){
				System.out.print(" ");
			}
			for(int k=1;k<=9-2*i;k++){
				System.out.print("* ");
			}
			System.out.println();

		}
	}
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值