2014-02-06 41 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 
} 

而且爲什麼你需要傳遞變量?您正在將其重置爲循環內零。