循环语句
使用场景:重复执行,指定的一段代码,比如我们想要输出N次:"Hello Java"
1.while循环
2.for 循环
while循环
while : while循环,就是在满足条件期间,重复执行某些代码。
循环体
}
while实现0.1毫米的纸张折叠几次才能达到珠穆朗玛峰的高度
int mountainHeight = 8848860;
//纸张
double paper = 0.1;
//
int count = 0;
//while循环
while (paper < mountainHeight){
//纸张折叠
paper = paper*2;
//
count++;
System.out.println("目前折叠了【" + count + "】次,纸张的厚度为:" + paper);
}
while的无限循环
while(true){
System.out.prrintln("hello")
}
for循环
普通for循环int A = 3; for (int i = 0; i < A; i++) { System.out.println("Hello Java"); }
嵌套for循环for (int i = 0; i < 4; i++) { for (int j = 0; j < 50; j++) { System.out.print("*"); } System.out.println();
}
增强for循环
int A = 3;
for (int i = 0; i < A; i++) {
System.out.println("Hello Java");
}
List<String> numList = new ArrayList<>();
numList.add("apple");
numList.add("header");
numList.add("baby");
for (String s : numList) {
System.out.println("增强for:"+s);
}