编写程序,计算1+2+3+…..+100的值

  1. int a1=1;
  2. int a2=2;
  3. int a3=3;
  4. int a100=100;
  5. a100=a1+a2+....+a100;

语法

  1. while(条件表达式){
  2. 循环体
  3. }
  1. class Demo{
  2. public static void main(String[] args){
  3. int counter = 1;
  4. int sum = 0;
  5. while(counter <= 100){
  6. //sum = sum + counter;
  7. sum += counter;
  8. counter++;
  9. }
  10. System.out.println("1+2+....+100=" + sum);
  11. }
  12. }