标签:bsp 算法 while color [] 代码 sys nbsp ++
1.递归算法
1 public static void main(String[] args) { 2 System.out.println(add(1)); 3 } 4 5 private static int add(int i) { 6 if (i < 100 && i > 0) { 7 return i + add(i + 1); 8 } 9 else { 10 return i; 11 } 12 }
2..for循环
1 public static void main(String[] args) { 2 int sum = 0; 3 for (int i = 1; i <= 100; i++) { 4 sum += i; 5 } 6 System.out.println("1到100累加的和为:" + sum); 7 }
3.while循环
1 public static void main(String[] args) { 2 int sum = 0; 3 int i = 1; 4 while (i <= 100) { 5 sum += i; 6 i++; 7 } 8 System.out.println("1到100累加的和为:" + sum); 9 }
4.do.....while循环
1 public static void main(String[] args) { 2 int sum = 0; 3 int i = 1; 4 do { 5 sum += i; 6 i++; 7 } while (i <= 100); 8 System.out.println("1到100累加的和为:" + sum); 9 }
标签:bsp 算法 while color [] 代码 sys nbsp ++
原文地址:http://www.cnblogs.com/limengchen/p/6759987.html