|
package xiangmu;
|
|
|
|
public class Ceshi {
|
|
public static void main(String args[]) {
|
|
// 三种循环计算从1连加到100的正整数和
|
|
// while 循环
|
|
int i = 1;
|
|
int he = 0;
|
|
while(i <= 100) {
|
|
he = he + i;
|
|
i = i + 1;
|
|
}
|
|
System.out.println("1 + 2 + 3 + ... + 99 + 100 = " + he);
|
|
// do...while 循环
|
|
int j = 1;
|
|
int he1 = 0;
|
|
do {
|
|
he1 = he1 + j;
|
|
j = j + 1;
|
|
}while(j <= 100);
|
|
System.out.println("1 + 2 + 3 + ... + 99 + 100 = " + he1);
|
|
// for 循环
|
|
int he2 = 0;
|
|
for(int k = 1; k <= 100; k = k + 1) {
|
|
he2 = he2 + k;
|
|
}
|
|
System.out.println("1 + 2 + 3 + ... + 99 + 100 = " + he2);
|
|
}
|
|
}
|
1 + 2 + 3 + ... + 99 + 100 = 5050
1 + 2 + 3 + ... + 99 + 100 = 5050
1 + 2 + 3 + ... + 99 + 100 = 5050