编写了一两个简单的程序:
(1)编写应用程序 :1!+2!+3!+4!+5!+6!+7!+8!+9!+10!。
public class Example { public static void main(String args[]) { int i; int j; int n; int sum = 0; for(i=1;i<=10;i++) { n = 1; for(j=1;j<=i;j++) { n = n * j; }sum = sum + n; } System.out.println("sum="+sum); } }
(2)编写一个应用程序求100以内的全部素数。
public class Prime1 { public static void main(String args[]) { int i; int n; for(i=3;i<100;i++) { n = 2; while(i>n) { if(i%n==0)break; n++; } if(i==n) { System.out.println(n); } } } }
这是我第一次使用Java语言编写的程序,如有错误希望能指出。