我已经寻找了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
参考方案
String name = ...;
int name = ...;
做不到
将String name
替换为String nameString
,因为这是使用Integer.parseInt
时要解析的变量! (也不要忘记所有其他变量!)
例如
String hours
与String hoursString
String payRate
与String payRateString
String federalTaxRate
与String federalTaxRateString
String stateTaxRate
与String stateTaxRateString
我正在查看一些使用双精度变量来存储(360-359.9998779296875)结果为0.0001220703125的代码。 double变量将其存储为-1.220703125E-4。当我使用BigDecimal时,其存储为0.0001220703125。为什么将它双重存储为-1.220703125E-4? 参考方案 我不会在这里提及精度问题,而只会提及数字…
Java:找到特定字符并获取子字符串 - java我有一个字符串4.9.14_05_29_16_21,我只需要获取4.9。数字各不相同,所以我不能简单地获得此char数组的前三个元素。我必须找到最正确的.并将其子字符串化直到那里。我来自Python,因此我将展示Python的实现方法。def foobar(some_string): location = some_string.rfind('.&…
java:继承 - java有哪些替代继承的方法? java大神给出的解决方案 有效的Java:偏重于继承而不是继承。 (这实际上也来自“四人帮”)。他提出的理由是,如果扩展类未明确设计为继承,则继承会引起很多不正常的副作用。例如,对super.someMethod()的任何调用都可以引导您通过未知代码的意外路径。取而代之的是,持有对本来应该扩展的类的引用,然后委托给它。这是与Eric…
Java:BigInteger,如何通过OutputStream编写它 - java我想将BigInteger写入文件。做这个的最好方式是什么。当然,我想从输入流中读取(使用程序,而不是人工)。我必须使用ObjectOutputStream还是有更好的方法?目的是使用尽可能少的字节。谢谢马丁 参考方案 Java序列化(ObjectOutputStream / ObjectInputStream)是将对象序列化为八位字节序列的一种通用方法。但…
Java-如何将此字符串转换为日期? - java我从服务器收到此消息,我不明白T和Z的含义,2012-08-24T09:59:59Z将此字符串转换为Date对象的正确SimpleDateFormat模式是什么? java大神给出的解决方案 这是ISO 8601标准。您可以使用SimpleDateFormat simpleFormat = new SimpleDateFormat("yyyy-MM…