利用for(while)(do while)循环求1-100的和

//for循环

public static void main(String[] args) {
        // TODO Auto-generated method stub
//      1. (for 循环)计算1+2+3+...+100 的和
        int sum = 0;
        for(int i=0;i<=100;i++){
            sum+=i;
        }
        System.out.println(sum);
    }
//while循环
public static void main(String[] args) {
        // TODO Auto-generated method stub
//      (while 循环)计算1+2+3+...+100 
        int x=0;
        int sum =0;
        while(x<=100 ){
            sum+=x;
            x++;
        }
        System.out.println(sum);
    }
//do while 循环
public static void main(String[] args) {
        // TODO Auto-generated method stub
        int x=0;
        int sum=0;
        do{
            sum+=x;
            x++;
        }while(x<=100);
        System.out.println(sum);
    }

你可能感兴趣的:(javase,基础题)