2013-04-17 107 views
0

我正在編寫一個程序,它調用來自同一文件夾中不同類的「發票」方法。我不斷收到以下錯誤:Java - 錯誤:找不到符號

error: cannot find symbol 

strInvoice = invoice(); 
      ^
symbol: method invoice() 

這裏是我如何在我的程序的方法調用:

strInvoice= invoice(); 
JOptionPane.showInputDialog(null, strInvoice, "*Name*'s Party Store", -1); 

這是該方法看起來像在位於類文件相同的文件夾:

public String invoice() 
{ 
    String strInfo=""; //string returned containing all the information 
    double dBalloonNoDiscount; //balloon total without discount 
    double dCandleNoDiscount; //candle total without discount 
    double dBannerNoDiscount; //banner total without discount 
    double dSubtotal;  //subtotal 
    double dTax;   //tax 
    double dShippingCost;  //shipping cost 
    double dTotal;   //total 
    String strShippingType;  //shipping type 

    dBalloonNoDiscount = balloons * 2.50; 
    dCandleNoDiscount = candles * 6.00; 
    dBannerNoDiscount = banners * 2.00; 

    dSubtotal = subtotal(); 
    dTax = tax(); 
    dShippingCost = shippingCost(); 
    strShippingType = shippingType(); 
    dTotal = orderTotal(); 

    strInfo += "\nQuantity" 
     + "\n\nBalloons: " + balloons + " @ $2.50 = " 
      + df.format(dBalloonNoDiscount) + "* Discount Rate: " 
      + df.format(discount('B')) + " = " 
      + df.format(subtotal('B')) 
     + "\n\nCandles: " + candles + " @ 6.00 = " 
      + df.format(dCandleNoDiscount) + "* Discount Rate: " 
      + df.format(discount('C')) + " = " 
      + df.format(subtotal('C')) 
     + "\n\nBanners: " + banners + " @ $2.50 = " 
      + df.format(dBannerNoDiscount) + "* Discount Rate: " 
      + df.format(discount('N')) + " = " 
      + df.format(subtotal('N')) 
     + "\nSubtotal: " + df.format(dSubtotal) 
     + "\n Tax: " + df.format(dTax) 
     + "\nShipping: " + df.format(dShippingCost) + " - " 
      + strShippingType 
     + "\n Total: " + dTotal; 

    return strInfo; 
} 

我希望這是足夠的信息。我似乎無法找到問題。

+0

我建議你回顧一些OO和Java的基礎知識,嘗試做幾個簡單的例子並嘗試這個。從長遠來看,這肯定會有所幫助。 – Krishna

回答

0

如果您希望能夠從另一個類中調用某個方法,則需要將該方法設置爲靜態或您需要獲取定義該類的實例。

在你的情況,你可能需要一個實例,是這樣的:

YourClass instance = new YourClass(); 

再後來:

strInvoice = instance.invoice(); 
2
I am writing a program that calls upon the "invoice" method from a different 
class in the same folder. 

然後你調用它像:

strInvoice= invoice(); 

這不能工作,因爲你需要調用這個方法爲:

strInvoice = obj.invoice(); 

其中obj是其他類的實例。

或者,如果invoice()方法是public static那麼你也可以這樣調用它:

strInvoice = SomeClass.invoice(); 
+0

非常感謝您的幫助! – Logan94

1

如果要調用這是它被稱爲類外的方法,那麼你調用之前需要參考方法。

與您的情況一樣,invoice()方法在其他類中可用,並且您在其他類中調用此方法,因此您需要invoice()方法可用的類(對象)的引用。例如: 例如:

ClassA object = new ClassA(); 
    object.invoice(); // assume invoice method is available in ClassA. 
+0

感謝您的幫助! – Logan94