在使用方法时无法将输出显示在终端中

时间:2014-02-06 03:46:56

标签: java

处理方法时遇到问题。这是一个简单的程序,但我似乎无法将我的数据打印到终端

这是我的代码:

public class method
{
    public static void main( String args[] )
    {
        double result = 0;
        int i;
        System.out.println("i" + "\t\t" +  "m(i)");
        System.out.println("-------------------");
        //columns and header

    }
    public double theSum(int i, double result)
    {
        for(i=0; i < 21; i++)
        {
            System.out.print(i + "\t\t" + result + "\n");
            result +=(double) (i + 1)/(i+2);   
            // computing the data
        }
        return result;
    }    
}

所有内容都会编译,但我的输出只打印我的标题...我认为我的底部方法可能有误,但不确定在哪里。

Sample output:
1        0.5
2        1.16
...
19       16.40
20       17.35

2 个答案:

答案 0 :(得分:0)

您需要调用方法来打印输出

    public static void main( String args[] )
        {     
           double result = 0;
            int i=0;
            System.out.println("i" + "\t\t" +  "m(i)");
            System.out.println("-------------------");
            //columns and header
           method d=new method();
            d.theSum(i,result);

}

<强>输出

i       m(i)
-------------------
0       0.0
1       0.5
2       1.1666666666666665
3       1.9166666666666665
4       2.716666666666667
5       3.5500000000000003
6       4.4071428571428575

答案 1 :(得分:0)

你根本没有打电话给你的方法。

public static void main( String args[] )
{
    double result = 0;
    int i;
    System.out.println("i" + "\t\t" +  "m(i)");
    System.out.println("-------------------");
    //columns and header
    double result = theSum(5,result); //Call the method
}

为什么你需要传递变量 i ?您正在循环内将其重置为零。