2014-02-06 39 views
0

遇到与处理方法有关的问题。这是一个简单的程序,但我似乎无法让我的数据打印到终端无法在使用方法时在我的终端中显示我的输出

这里是我的代码:

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 
+5

你打电话给你的方法吗?或者你期望它会自称? Java只会做你告诉它做的事情。 –

回答

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 
+0

好的谢谢。是否有替代方法可以打印到输出。我问,因为我从来没有见过方法D =新方法(); d。theSum(i,result); – Tanner10

+0

您需要创建类对象来调用实例方法。 – Nambi

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 
} 

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