Java 错误表示在尝试将字符串转换为双精度时已在main中定义变量,然后表示未知符号
我已经找了40多分钟,试图在这个网站上找到一个可能与我的问题有关的答案,但没有结果 我被难住了。我正在尝试将程序转换为GUI。我正在使用Textpad,它在编译时告诉我该变量已经在main方法中定义。然后当转换为int或double时,它告诉我它找不到符号并指向我试图转换的变量。然后在我的计算过程中,它告诉我二进制运算符“*”的操作数类型不正确Java 错误表示在尝试将字符串转换为双精度时已在main中定义变量,然后表示未知符号,java,user-interface,typeconverter,Java,User Interface,Typeconverter,我已经找了40多分钟,试图在这个网站上找到一个可能与我的问题有关的答案,但没有结果 我被难住了。我正在尝试将程序转换为GUI。我正在使用Textpad,它在编译时告诉我该变量已经在main方法中定义。然后当转换为int或double时,它告诉我它找不到符号并指向我试图转换的变量。然后在我的计算过程中,它告诉我二进制运算符“*”的操作数类型不正确 import javax.swing.JOptionPane; public class PayrollGUI { // calculates pa
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
不行
将字符串名称
替换为字符串名称字符串
,因为这是您在使用Integer.parseInt时试图解析的变量!(不要忘记所有其他变量!)
e、 g
字符串小时数
与字符串小时数字符串
String-payRate
与String-payRate-String
String federalTaxRate
与String federalTaxRateString
String statetasrate
与String statetasratestring
您无法编译,因为名称
,小时数
,工资率
。。。。变量声明两次。将每个名称更改为另一个名称
您的parseInt
,parseDouble
语句不好,因为可能存在输入格式不正确的情况(字母表或其他)。因此,您需要使用try/catch
来验证您的输入
witholdings=(int)(witholdings*100)/100.0代码>这不是保持2位数格式的方法。阅读
尝试使用更好的IDE。如果您是java新手,请查看或
一个方法中不能有两个相同的变量名。在代码中有许多重复的变量,就像下面的一样。同一方法中不能有两个name
变量
String name = JOptionPane.showInputDialog ("Employee's Name: ");
int name = Integer.parseInt(nameString);
如果您使用任何类似Java的IDE,您可以轻松解决此类编译问题,而不是使用Textpad。您不能使用以前在同一范围内使用过的名称声明变量<代码>姓名
、小时数
、工资率
等都是重复声明。非常感谢您的帮助
String name = JOptionPane.showInputDialog ("Employee's Name: ");
int name = Integer.parseInt(nameString);