JAVA中的循环

一、什么是循环

三要素:

1、循环变量初始化

2、循环的判断条件

3、循环体中改变循环变量的值

二、语法结构

int i=0; //循环变量

while (i<10){ //循环条件判断

System.out.println("好好学习");

i++; //改变循环变量的值

}

三、如何使用循环

1、找循环结束的地方

2、找循环变量的初始值

3、找循环变量改变的规律

<strong><span style="font-size:24px;">//do while
import java.util.*;
public class dowhile{
	public static void main(String[] args){
	Scanner a=new Scanner(System.in);
	int b;
	Random c=new Random();
	int d=c.nextInt(101);
	do{
		System.out.println("猜猜电脑随机生成的数是多少?");
		b=a.nextInt();
		if(b<d){
			System.out.println("小了");
			}else if(b>d){
			System.out.println("大了");
			}
		}while(b!=d);
		System.out.println("恭喜答对了");
	}
}</span></strong>

<span style="font-size:24px;"><strong>//for
import java.util.*;
public class text2{
	public static void main(String[]args){
		Scanner a=new Scanner(System.in);
		double sum=0;
		double ave=0;
		for(int i=1;i<=5;i++){
			System.out.println("请输入你的第"+i+"门成绩");
			int score=a.nextInt();
			sum=sum+score;
			ave=sum/5;
		}
		System.out.println("你的平均成绩是:"+ave);
	}
}</strong></span>

<span style="font-size:24px;"><strong>//while
import java.util.*;
public class text5{
	public static void main(String[]args){
		Scanner a=new Scanner(System.in);
		System.out.println("请输入你的密码");
		while(true){
			
			int password=a.nextInt();
			System.out.println("密码不正确请再输入一次");
			if(password!=123456){
				continue;
			}
			break;
		}
		System.out.println("密码正确!");
	}
}</strong></span>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值