标签:als main code bool result 质数 turn return public
//方法一:
public class Demo { public static void main(String[] args) { int result = 0; for (int i=0; i<101; i++) { result += i; } System.out.println(result); } }
//方法二: public class Demo { public static void main(String[] args) { int sum = sum(100); System.out.println(sum); } static int sum(int n) { if (n==0) { return 0; } return n+sum(n-1); } }
//方法一: public class Demo { public static void main(String[] args) { int result = 0; for (int i=0; i<101; i=i+2) { result += i; } System.out.println(result); } }
//方法二: public class Demo { public static void main(String[] args) { int sum = sum(100); System.out.println(sum); } static int sum(int n) { if (n==0) { return 0; } return n+sum(n-2); } }
public class Demo {
public static void main(String[] args) {
for (int i = 1; i < 101; i++) {
boolean flag = true;
if (i == 1) {
continue;
}
for (int j = 2; j < i; j++) {
if (i % j == 0) {
flag = false;
break;
}
}
if (flag) {
System.out.println(i);
}
}
}
}
1——100 的合数
public class Demo { public static void main(String[] args) { for (int i = 1; i < 101; i++) { boolean flag = true; if (i == 1) { continue; } for (int j = 2; j < i; j++) { if (i % j == 0) { flag = false; break; } } if (!flag) {//只改这一个地方就好了 System.out.println(i); } } } }
标签:als main code bool result 质数 turn return public
原文地址:http://www.cnblogs.com/BaiLaowu/p/6752636.html