2012-08-01 93 views
0

我是一個Java新手,這是一個真正的基本問題,所以請不要 錯過樹林。 我只需要知道如何訪問從方法如何從Java方法返回後訪問返回數據

private static int toInt(String number) 
    { 
     int multiplicand = 0; 
     // do stuff to figure out a multiplicand 
     return multiplicand; 
    } 

    public static void main(String[ ] args) 
    { 
     int anotherVariable = 53; 
     toInt(args[ 0 ]); 

所以回來的數據,有什麼事我在這裏說,我回來後從toInt方法,以便訪問是什麼被乘數?
可以說我想乘以multiplicand * anotherVariable中返回的內容。 我不能使用被乘數,因爲編輯器只是說「不能將多個乘法器解析爲變量」。

非常感謝所有偉大的程序員耐心地容忍我們的新手。

回答

4

您將函數調用的結果分配給一個變量,然後使用它。

int result = toInt ("1");

您現在可以使用result您認爲合適的;它包含您在方法中返回的任何值,這是multiplicand的值。所以你的情況,你現在可以做

int anotherResult = result * anotehrVariable;

注意,你也可以做

int anotherResult = toInt(args[0]) * anotherVariable;

不分配您的toInt調用的結果的中介變量。

我寧願將函數調用分配給變量;使調試更容易。如果你想多次使用結果,使用變量幾乎總是更好。

+0

+1:但有另一個樣子,他傳遞了args [0]這是一個String。 – 2012-08-01 01:21:34

+0

@DonRoby thanx ... – hvgotcodes 2012-08-01 01:22:02

+0

@hvgotcodes - 非常感謝你 – 2012-08-01 02:31:36