/*计算1!+2!+……+10!*/ public class FactorialAdd{ public static void main(String[] args){ long sum = 0; long Factorial = 1; for(int i = 1; i <= 10; i ++){ Factorial = Factorial * i; sum = sum + Factorial; } System.out.println(sum); } }
/*求1+3+5+……+99的和*/ public class OddSum{ public static void main(String[] args){ int sum = 0; for(int i = 1; i < 100; i += 2){ sum = sum + i; } System.out.println(sum); } }
/*输出1~100内前5个可以被3整除的数*/ public class Calculate{ public static void main(String[] args){ int sum = 0; int i = 1; while(i <= 100){ if(i % 3 == 0){ System.out.print(i + " "); sum ++; } if(sum == 5){ break; } i ++; } } }
/*输出101~200内的质数*/ public class Prime{ public static void main(String[] args){ for (int i = 101; i <= 200; i += 2) { boolean isPrime = true; for ( int j = 2; j < i; j ++) { if ( i % j == 0) { isPrime = false; break; } } /* if (isPrime) { System.out.print(i + " "); } */ if (! isPrime) { continue; } System.out.print(i + " "); } } }