2016-09-19 40 views
-7

我找不出爲什麼第二種方法不能執行。它編譯並接受輸入,但之後沒有任何反應。有人能指引我朝着正確的方向嗎?Java第二種靜態方法沒有執行

import javax.swing.JOptionPane; 

public class Test1 { 
    public static void main(String[] arg) { 
     String hoursOfWorkString, costOfMaterialsString, nameOfProductString; 

     nameOfProductString = JOptionPane.showInputDialog(null, "Enter the name of the product. ", JOptionPane.QUESTION_MESSAGE); 
     hoursOfWorkString = JOptionPane.showInputDialog(null, "Enter the number of hours worked. ", JOptionPane.QUESTION_MESSAGE); 

     double hoursOfWork = Double.parseDouble(hoursOfWorkString); 

     costOfMaterialsString = JOptionPane.showInputDialog(null, "Enter the cost of the product. ", JOptionPane.QUESTION_MESSAGE);   
     double costOfMaterials = Double.parseDouble(costOfMaterialsString); 
    } 



    public static void CalulatePrice(double costOfMaterials, double hoursOfWork, String nameOfProductString) { 
     double salePercentage = 0.75, shipping = 6, markup = 14, retailPrice; 

     retailPrice = salePercentage * (costOfMaterials + (markup * hoursOfWork)) + shipping; 

     JOptionPane.showMessageDialog(null, String.format("The retaill price of the %. product is $ %.02f.", nameOfProductString, retailPrice)); 
    } 
} 

更新:我使用了該示例並修復了一些其他錯誤,並運行它。看完評論後,我認爲它不會起作用。我只知道我的代碼裏有些東西是關閉的。

謝謝大家的意見。

+4

這可能是因爲你不叫它? – Li357

+0

我很想知道你爲什麼認爲它會執行。 – Andreas

回答

2

這是因爲你已經定義了你的函數,但是你沒有調用它。在你main方法的末尾插入一個電話,像

double costOfMaterials = Double.parseDouble(costOfMaterialsString); 
// Here... 
CalulatePrice(costOfMaterials, hoursOfWork, nameOfProductString); 

此外,我想你意味着計算。並且,遵循Java命名約定,它應該是類似於calculatePrice

相關問題