while循环
//语法结构:
while(表达式){
...代码块/循环体...
}
理解:
表达式的结果必须是boolean类型
true - 循环
false- 跳出循环
死循环:
while(true){
System.out.println("死循环");
}
while变形记:模拟for循环输出5遍"爱笑的小孩,运气不会差"
int i = 0;
while(i<5){
System.out.println("爱笑的小孩,运气不会差");
i++;
}
运行结果:
案例:我有个梦想,每月存3000,每年递增1000元,多少个月后存满20万
int money = 3000;
int allMoney = 0;
int month = 0;
while(allMoney < 200000){
allMoney+=money;
month++;
if(month % 12 == 0){
money+=1000;
}
}
System.out.println(month + "个月后存满20万");
System.out.println(money);
运行结果: