小编典典

错误表示尝试将字符串转换为double时main中已定义的变量,然后表示未知符号

java

我已经寻找了40分钟以上,试图在此站点上找到可能与我的问题无关的答案。

我很沮丧。我正在尝试将程序转换为GUI。我正在使用Textpad,它告诉我何时编译该变量已在main方法中定义。然后,当转换为int或double时,它告诉我找不到符号,并指向我要转换的变量。然后在计算时,它告诉我二进制运算符’*’的操作数类型错误。

import javax.swing.JOptionPane;

public class PayrollGUI {

// calculates payroll
public static void main (String [] args) {

String name = JOptionPane.showInputDialog ("Employee's Name: ");
int name = Integer.parseInt(nameString);

String hours = JOptionPane.showInputDialog ("Number of hours worked in a week (e.g., 10: ");
int hours = Integer.parseInt(hoursString);

String payRate = JOptionPane.showInputDialog ("Hourly pay rate (e.g., .6.75: ");
double payRate = Double.parseDouble(payRateString);

String federalTaxRate = JOptionPane.showInputDialog ("Federal tax witholding rate (e.g., .20: ");
double federalTaxRate = Double.parseDouble(federalTaxRateString);

String stateTaxRate = JOptionPane.showInputDialog (" State tax witholding rate (e.g., .09: ");
double stateTaxRate = Double.parseDouble(stateTaxRateString);

//calculate witholdings, grosspay and netpay
double federalWitholding = federalTaxRate * (hours * payRate);
double stateWitholding = stateTaxRate * (hours * payRate);
double grossPay = hours * payRate;
double netPay = grossPay - withholdings;
double witholdings = federalWithodling + stateWitholding;

//format to keep two digit decimal
witholdings = (int) (witholdings * 100) /100.0;
netPay = (int) (netPay * 100) / 100.0;
grossPay = (int) (grossPay * 100) / 100.0;
federalWitholding = (int) (federalWitholding * 100) / 100.0;
stateWitholding = (int) (stateWitholding *100) / 100.0;

/*String output = (null);
String output = (null);*/
String output = "Employee Name: " + name +
"/nHours Worked: "  + hours +
"/nPay Rate: $" + payRate +
"/nGross Pay: $" + grossPay +
"/nDeductions:" +
"/n     Federal Witholding: $" + federalWitholding +
"/n     State Witholding : $" + stateWitholding +
"/n     Total Deductions : $" + witholdings +
"/n     Net Pay: $";
JOptionPane.showMessageDialog(null, output);
}//end main
}//end Payroll

阅读 209

收藏
2020-11-26

共1个答案

小编典典

String name = ...;
int name = ...;

做不到

替换为String nameString nameString因为这是您在使用时要解析的变量Integer.parseInt!(也不要忘记所有其他变量!)

例如

String hoursString hoursString
String payRatewith String payRateString
String federalTaxRateString federalTaxRateString
String stateTaxRatewithString stateTaxRateString

2020-11-26