第三次java作业

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);
}
}
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值