教学视频链接:https://www.bilibili.com/video/BV12J41137hu?p=38&vd_source=b5775c3a4ea16a5306db9c7c1c1486b5https://www.bilibili.com/video/BV12J41137hu?p=38&vd_source=b5775c3a4ea16a5306db9c7c1c1486b5 在Java中,while
结构是一个循环结构,它会在满足条件的情况下重复执行代码块。while
循环的基本语法如下:
while (条件) {
// 需要重复执行的代码
}
代码举例1:打印1-100;
public class WhileDemo01 {
public static void main(String[] args) {
int i = 0;
while (i<100){
i++;
System.out.println(i);
}
}
}
代码举例:计算1+2+3...+100=?
public class WhileDemo02 {
public static void main(String[] args) {
int i =1;
int sum = 0;
while (i<=100){
sum = sum + i;
i++;
}
System.out.println(sum);
}
}