1.编写应用程序求1!+2!+3!+4+!...+10!
public class jiecheng {
public static void main(String[] args) {
long s;
s=0;
for(int i=1;i<=10;i++)
{
s+=f(i);
System.out.print(f(i)+"\r");
}
System.out.print("s=\r"+s);
}
public static int f(int x){
if(x<=1){
return 1;
}
else{
return x*f(x-1);
}
}
}
2.编写一个应用程序求100以内的全部素数
public class thx{public static void main(String[] args){
boolean isSuShu ;
for (int i=2 ; i<100 ; i++){
isSuShu = true ;
for (int j=2 ;j <= i/2 ; j++){
if (i%j == 0 ) {
isSuShu = false ;
}
}
if (isSuShu == true )
System.out.print(i+"\t");
}
}
}
3.分别用do-while和for循环计算1+1/2!+1/3!+1/4!+.....前20项和
public
class
Test3 {
Map<Integer, Double> map =
new
HashMap<Integer, Double>();
public
static
void
main(String[] args) {
Test3 test =
new
Test3();
double
sum =
0
;
int
index =
20
;
for
(
int
i =
1
; i <= index; i++) {
sum += test.cal(i);
}
System.out.println(
"前"
+ index +
"项之和:"
+ sum);
}
public
double
cal(
int
num) {
if
(num ==
1
) {
map.put(
1
, (
double
)
1
);
return
1
;
}
Double value = map.get(num -
1
);
double
result = value / num;
map.put(num, result);
return
result;
}
}
前
20
项之和:
1.7182818284590455
5.编写应用程序,使用for循环语句计算8+88+888+....前10项之和。
public class Test3 {
public static void main(String[] args) {
String a="";
long b;
int sum=0;
for(int i=0;i<10;i++){
a+="8";
b=Long.parseLong(a);
sum+=b;
System.out.println("结果为"+sum);
}
}
}