#include
public class Calc {
public static void main(String[] args) {
int n=Integer.parseInt(args[0]);
int sum=0;
//for
for(int i=1;i<=n;i++){
sum+=i;
}
System.out.println(sum);
//while
int i=1;
sum=0;
while(i<=n){
sum+=i;
i++;
}
System.out.println(sum);
//do-while
i=1;
sum=0;
do{
sum+=i;
i++;
}while(i<=n);
System.out.println(sum);
}
} n的值在执行时动态传入,例如java Calc 100
int sum=0;for(int i=1;i
public static void main(String[] args) {
int n=100;
int sum=0;
for(int i=1;i<=n;i++){
sum+=i;
}
System.out.println(sum);
}----------------------------------------- public static void main(String[] args) {
int n = 100;
int sum = 0;
int i = 0;
while (i <= n) {
sum+=i++;
}
System.out.println(sum);
} --------------------------------- public static void main(String[] args) {
int n = 100;
int sum = 0;
int i = 0;
do {
sum += i++;
} while (i <= n);
System.out.println(sum);
}