Files
javaSE251223/day03/src/com/inmind/Demo04_while.java

58 lines
1.3 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package com.inmind;
/*
while循环的格式和使用
循环变量初始化①
while(循环条件②){
循环体语句③
修改循环变量④
}
执行顺序:①②③④>②③④>②③④…②不满足为止
*/
public class Demo04_while {
public static void main(String[] args) {
//使用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("程序结束");
}
//while循环的语法学习
public static void whileMethod(String[] args) {
//打印1~10
int i = 1;
while(i<=10){
System.out.println(i);
i++;
}
System.out.println("------------------");
//打印10~1
int j = 10;
while (j>=1) {
System.out.println(j);
j--;
}
System.out.println("------------------");
//打印A~Z
int k = 'A';
while (k <= 'Z') {
System.out.println((char)k);
k++;
}
}
}