码迷,mamicode.com
首页 > 其他好文 > 详细

用递归求分数的和

时间:2014-08-14 20:11:09      阅读:206      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   java   io   for   ar   div   

import java.io.IOException;
import java.util.Scanner;

public class Sum {
    public static void main(String[] args) throws IOException {
        try {
            System.out.println("请输入的i的值!");
            Scanner input = new Scanner(System.in);
            int i = input.nextInt();
            System.out.println("求和的值为:" + sum(i));
        } catch (Exception ex) {
            System.out.println("输入的i为整数!");
        }
    }

    public static double sum(int i) {
        double result = 0;
        int j;
        for (j = i; j > 0; j--) {
            result = result + ((double) j / (2 * (double) j + 1));
        }
        System.out.println("输入的整数要求为正!");
        return result;
    }
}

递归求和m(i)=i/(i*2+1)

用递归求分数的和,布布扣,bubuko.com

用递归求分数的和

标签:style   blog   color   java   io   for   ar   div   

原文地址:http://www.cnblogs.com/Amoxicil/p/3913091.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!