-2

我正在尝试为我的一类将两个简单的字符串相乘。该程序本身将根据用户输入的小时费率和工作时间显示总工资。我已经进行了一些挖掘,但我发现的结果对于我想要实现的目标来说似乎过于复杂。当然,我是从 C# 到 Java,所以我承认存在差异。以下是我到目前为止的代码。

    String RoPString, hrswrkdString;

    double grosspay, netpay, whtax;

    //Rate of pay input dialog box.
    RoPString = JOptionPane.showInputDialog(null, "Enter employee's hourly rate", "Rate of Pay", JOptionPane.INFORMATION_MESSAGE);

    //Hours worked input dialog box.
    hrswrkdString = JOptionPane.showInputDialog(null, "Enter hours worked", "Hours Worked", JOptionPane.INFORMATION_MESSAGE);

    //Calculations
    grosspay = RoPString*hrswrkdString;

    //Summary of pay
    JOptionPane.showMessageDialog(null, "Gross Pay: $" + grosspay);
4

2 回答 2

4
grosspay = RoPString*hrswrkdString;

您不能*String类型应用运算符。转换Stringdouble然后执行*.

例子:

  double ropdouble = Double.valueOf(RoPString);
  double hrswrkddouble = Double.valueOf(hrswrkdString);

注意:在命名变量时遵循 Java 命名约定(变量名以小写开头)。

于 2013-01-28T18:57:11.667 回答
1

我认为您需要使用Double.parseDouble()String对象变成可以与乘法一起使用的东西。

String RoPString, hrswrkdString;
double grosspay, netpay, whtax;

//Rate of pay input dialog box.
RoPString = JOptionPane.showInputDialog(null, "Enter employee's hourly rate", "Rate of Pay", JOptionPane.INFORMATION_MESSAGE);

//Hours worked input dialog box.
hrswrkdString = JOptionPane.showInputDialog(null, "Enter hours worked", "Hours Worked", JOptionPane.INFORMATION_MESSAGE);

//Calculations
grosspay = Double.parseDouble(RoPString)*Double.parseDouble(hrswrkdString);

//Summary of pay
JOptionPane.showMessageDialog(null, "Gross Pay: $" + grosspay.toString());

}

于 2013-01-28T18:58:53.687 回答