while结构
/*
while语句
基本格式:
while(条件判断语句){循环体语句;
}
完整格式:
初始化语句;
while(条件判断语句){
循环体语句;
条件控制语句;
}
执行流程:
1:执行初始化语句
2:执行条件判断语句,看其结果是true还是false
如果是false,循环结东
如果是true,继续执行
3:执行循环体语句
4:执行条件控制语句
5:回到2继续
*/
public class WhileDemo {
public static void main(String[] args) {
//需求:在控制台输出五个“HelloWorld”
//for循环实现
for (int i=1;i<6;i++){
System.out.println("HelloWorld");
}
System.out.println("-------");
//while循环实现
int j=1;
while (j<6){
System.out.println("HelloWorld");
j++;
}
}
}
案例
/*
珠移朗玛峰
需求:
世界最高山峰是珠穆朗玛峰(8844.43米=8844430毫米),假如我有一张足够大的纸,它的厚度是o.1毫米。
请问,我折叠多少次,可以折成珠移朗玛峰的高度?
*/
public class ScannerDemo {
public static void main(String[] args) {
//定义一个计数器,初始值为0
int count=0;
//定义纸张厚度
double paper=0.1;
//定义珠穆朗玛峰的高度
int zf=8844430;
//因为要反复折叠,所以要使用循环,但是不知道折叠多少次,这种情况下更适合使用while循环
// 折叠的过程中当纸张厚度大于珠峰就停止了,因此继续执行的要求是纸张厚度小于珠峰高度
while (paper<=zf){
//循环的执行过程中每次纸张折叠,纸张的厚度要加倍
paper*=2;
//在循环中执行累加,对应折叠了多少次
count++;
}
//打印计数器的值
System.out.println("需要折叠:"+count+"次");
}
}
dowhile结构
/*
do . ..while语句
基本格式:
do {
循环体语句;
}while(条件判斲语句);
完整格式:
初始化语句;
do {
循环体语句;
条件控制语句;
}while(条件判斲语句);
执行流程:
1:执行初始化语句
2:执行循环体语句
3:执行条件控制语句
4:执行条件判断语句,看其结果是true还是false
如果是false,循环结束
如果是true,继续执行
5:回到2继续
*/
public class DoWhileDemo {
public static void main(String[] args) {
//需求:在控制台输出5次“HelloWorld”
int j=1;
do {
System.out.println("HelloWorld");
j++;
}while (j<6);
}
}
三种循环
/*
三种循环的区别
*/
public class ScannerDemo {
public static void main(String[] args) {
/*
//for循环
for (int i=3;i<3;i++){
System.out.println("爱意随风起");
}
System.out.println("-------");
//while循环
int j=3;
while (j<3){
System.out.println("爱意随风起");
j++;
}
System.out.println("-------");
//do...while循环
int k=3;
do {
System.out.println("爱意随风起");
k++;
}while (k<3);
*/
/*
//for循环
for (int i=1;i<3;i++){
System.out.println("爱意随风起");
}
//System.out.println(i);
System.out.println("-------");
//while循环
int j=1;
while (j<3){
System.out.println("爱意随风起");
j++;
}
System.out.println(j);
System.out.println("-------");
*/
//死循环
/*
for(;;){
System.out.println("for");
}
*/
/*
while (true){
System.out.println("while");
}
*/
/*
do {
System.out.println("do...whlie");
}while (true);
*/
}
}