day03-.while循环的语法&练习
This commit is contained in:
@@ -13,6 +13,20 @@ while(循环条件②){
|
|||||||
public class Demo04_while {
|
public class Demo04_while {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
//使用while的语法,计算出1~100的偶数累和
|
//使用while的语法,计算出1~100的偶数累和
|
||||||
|
//定义一个变量记录总和
|
||||||
|
int sum = 0;
|
||||||
|
//循环变量的定义
|
||||||
|
int i = 1;
|
||||||
|
while (i <= 100) {
|
||||||
|
//判断是否是偶数
|
||||||
|
if (i % 2 == 0) {
|
||||||
|
sum += i;
|
||||||
|
}
|
||||||
|
//修改循环变量
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
System.out.println("结果为:"+sum);
|
||||||
|
System.out.println("程序结束");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user