循环

一、 循环

顺序结构的程序语句只能被执行一次。如果您想要同样的操作执行多次,,就需要使用循环结构。

   Java中有三种主要的循环结构:

  • while循环
  • do...while循环
  • for循环
1)for循环

格式:for(条件初始化;条件判断;条件变化){

            重复执行的语句;

            }

执行流程:

  1. 条件初始化:声明一个变量,并且第一次赋值-->这个变量就是条件i
  2. 条件判断,结果如果为false,直接结束for循环,如果结果为true,满足条件执行{}中的语句体
  3. 条件变化: i自身的值的变化
  4. 从第二次开始重复执行第2,3步
public class ForDemo03{
	public static void main(String[] args){
		//i决定执行语句体的次数  i只能在循环中使用
		for(int i=10;i<=100;i++)
			System.out.println("安红,我想你想的睡不着觉!!!--有话好好说"+i);
		
		//System.out.println(i);
		
		//1~4之间的和
		int sum=0;
		for(int i=1;i<=4;i++){
			sum+=i;
		}
		System.out.println(sum);
		
		//当循环体语句只有一句的时候,前后的{}可以省略
	}	
}
2)while循环

格式:while( 布尔表达式 ) {

            //循环内容

            }

执行流程:

  1. 条件初始化:声明一个变量,并且第一次赋值-->这个变量就是条件i
  2. 条件判断,结果如果为true,循环就会一直执行下去。
public class WhileDemo05{
	public static void main(String[] args){
		int i=0;
		while(i<=100){
			System.out.println(i);
			i++;
		}
		//死循环  死循环后面的代码无法执行
		while(true){
			
		}
		
		//死循环
		for(;;){
			System.out.println("哈哈");
		}
		
	}
	
}
3)do...while循环

格式: do{

            //循环内容

            }while( 布尔表达式 )

注: 与while类似,但do…while 循环至少会执行一次。

public class DoWhileDemo06{
	
	public static void main(String[] args){
		int i=0;
		int sum=0;
		while(i<=10){
			sum+=i;
			i++;
		}
		System.out.println(sum);
		
		i=0;
		sum=0;
		do{
			sum+=i;
			i++;
		}while(i<=10);
		System.out.println(sum);
		
		boolean flag=false;
		while(flag){
			System.out.println("我是while---false");
		}
		
		do{
			System.out.println("我是do..while---false");
		}while(flag);
	}
	
}
4)break关键字

作用:终止当前循环

应用场景:循环语句或者 switch 语句中,用来跳出整个语句块。

public class Demo07{
	
	public static void main(String[] args){
		//模拟击鼓传花
		//模拟敲鼓的人  决定学生的编号   [1,39]
		int studentNum=(int)(Math.random()*(39-1+1)+1);  
		
		int i=0;  //模拟学生的编号
		while(true){
			i++;
			//System.out.println(i);
			if(i==studentNum){  //结束循环的条件  
				System.out.println("游戏结束,表演节目的同学编号为:"+i);
				break;
			}
		}
	}
}
5)continue关键字

作用:结束本次循环,直接进入新循环

应用场景:适用于任何循环控制结构中。作用是让程序立刻跳转到下一次循环的迭代。

在 for 循环中,continue 语句使程序立即跳转到更新语句。

在 while 或者 do…while 循环中,程序立即跳转到布尔表达式的判断语句。

public class Demo07{
	
	public static void main(String[] args){
		//1~10之间,遇到3或者3的倍数就跳过,其他打印
		for(int i=1;i<=10;i++){
			if(i%3==0){
				continue;  
			}
			System.out.println(i);
		}		
	}
}
6)Math类

不需要导包,java.lang包下的Math.random() 产生随机数 [0,1) 随机小数

    [0,max) 随机小数 Math.random()*max

    [0,max) 随机整数 (int)(Math.random()*max)

    [0,max] 随机整数 (int)(Math.random()*(max+1))

    [min,max] 随机整数 (int)(Math.random()*(max-min+1)+min)

     Math.round(参数)-->四舍五入

    Math.abs(参数)-->绝对值

     Math.abs(参数)-->绝对值

     Math.sqrt(参数)-->平方根

public class Math02{
	
	public static void main(String[] args){
		double d=Math.random();
		System.out.println(d);
		//[4,7]
		int i=(int)(Math.random()*(7-4+1)+4);
		System.out.println(i);
		
		System.out.println(Math.round(1.55));
		System.out.println(Math.abs(-1.55));
		System.out.println(Math.sqrt(4));
		
	}
	
}
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值