我无法弄清楚第二种方法无法执行的原因。它编译并接受输入,但之后没有任何反应。有人能指出我正确的方向吗?
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));
}
}
更新:我使用了该示例并修复了一些其他错误并运行。阅读评论后,我认为它不会起作用。我只知道我的代码出了点问题。
感谢大家的意见。
答案 0 :(得分:2)
那是因为你已经定义了你的功能,但你还没有调用它。在main
方法的末尾插入一个电话,例如
double costOfMaterials = Double.parseDouble(costOfMaterialsString);
// Here...
CalulatePrice(costOfMaterials, hoursOfWork, nameOfProductString);
,我认为您意味着 计算。并且, 请 遵循Java命名约定,它应该类似于calculatePrice
。