我的程序是 'crashing' 当它在 BlueJ 中的文本 "Gross Pay : " 后遇到“%.2f”
my program is 'crashing' when it encounters "%.2f" after the text "Gross Pay : " in BlueJ
此程序用于计算员工扣除后的净工资,但我 运行 在员工工资汇总中显示总工资时遇到问题。
我改变了对话框的类型等,是否还有解决方案可以让我在工资单摘要中使用非输入对话框?
import javax.swing.JOptionPane; // Dialog boxes are created to allow user inputs
public class Payslipgenerator
{
public static void main(String[] args)
{
String employeeName,grossPay,payslipFinal; //Declaring String variables
double PRSI=.025, PAYE=.2, USC=.04, Pension=.05, healthInsurance= 75; //Declaring consants used in the Application
double prsiCalc, payeCalc, uscCalc,pensionCalc, netValue; //Double variables that will store calculations
employeeName = JOptionPane.showInputDialog(null, "Enter Employee name : ", "User Info"); //Asks Employee to input their name, which is then stored
grossPay = JOptionPane.showInputDialog(null, "Enter Gross Pay (€) : ", "User Info"); //Prompts employee to enter Gross Pay for deduction calculation
double employeeGrossPay = Double.parseDouble(grossPay); //Stores input in double Variable
prsiCalc = employeeGrossPay * PRSI;
payeCalc = employeeGrossPay * PAYE;
uscCalc = employeeGrossPay * USC ;
pensionCalc = employeeGrossPay * Pension ;
netValue = employeeGrossPay - (prsiCalc + payeCalc + uscCalc + pensionCalc + healthInsurance);
payslipFinal = String.format("Gross Pay :€%.2f \nPRSI : €%.2f \nUSC : €%.2f \nPension Plan : €%.2f \nHealth Insurance : €75.00 \nNet Earnings: €%.2f"
+ employeeGrossPay, prsiCalc, uscCalc, pensionCalc, netValue);
JOptionPane.showInputDialog(null, "Employee Name : " + employeeName + "\n\n" + payslipFinal, "Payslip Breakdown");
}
}
改变
payslipFinal = String.format("Gross Pay :€%.2f \nPRSI : €%.2f \nUSC : €%.2f \nPension Plan : €%.2f \nHealth Insurance : €75.00 \nNet Earnings: €%.2f"
+ employeeGrossPay, prsiCalc, uscCalc, pensionCalc, netValue);
到
payslipFinal = String.format("Gross Pay :€%.2f \nPRSI : €%.2f \nUSC : €%.2f \nPension Plan : €%.2f \nHealth Insurance : €75.00 \nNet Earnings: €%.2f"
, employeeGrossPay, prsiCalc, uscCalc, pensionCalc, netValue);
测试:
public static void main(String[] args) {
String payslipFinal = String.format("Gross Pay :€%.2f \nPRSI : €%.2f \nUSC : €%.2f \nPension Plan : €%.2f \nHealth Insurance : €75.00 \nNet Earnings: €%.2f"
, 1f, 2f, 3f, 4f, 5f);
System.out.println(payslipFinal);
}
输出:
Gross Pay :€1,00
PRSI : €2,00
USC : €3,00
Pension Plan : €4,00
Health Insurance : €75.00
Net Earnings: €5,00
此程序用于计算员工扣除后的净工资,但我 运行 在员工工资汇总中显示总工资时遇到问题。
我改变了对话框的类型等,是否还有解决方案可以让我在工资单摘要中使用非输入对话框?
import javax.swing.JOptionPane; // Dialog boxes are created to allow user inputs
public class Payslipgenerator
{
public static void main(String[] args)
{
String employeeName,grossPay,payslipFinal; //Declaring String variables
double PRSI=.025, PAYE=.2, USC=.04, Pension=.05, healthInsurance= 75; //Declaring consants used in the Application
double prsiCalc, payeCalc, uscCalc,pensionCalc, netValue; //Double variables that will store calculations
employeeName = JOptionPane.showInputDialog(null, "Enter Employee name : ", "User Info"); //Asks Employee to input their name, which is then stored
grossPay = JOptionPane.showInputDialog(null, "Enter Gross Pay (€) : ", "User Info"); //Prompts employee to enter Gross Pay for deduction calculation
double employeeGrossPay = Double.parseDouble(grossPay); //Stores input in double Variable
prsiCalc = employeeGrossPay * PRSI;
payeCalc = employeeGrossPay * PAYE;
uscCalc = employeeGrossPay * USC ;
pensionCalc = employeeGrossPay * Pension ;
netValue = employeeGrossPay - (prsiCalc + payeCalc + uscCalc + pensionCalc + healthInsurance);
payslipFinal = String.format("Gross Pay :€%.2f \nPRSI : €%.2f \nUSC : €%.2f \nPension Plan : €%.2f \nHealth Insurance : €75.00 \nNet Earnings: €%.2f"
+ employeeGrossPay, prsiCalc, uscCalc, pensionCalc, netValue);
JOptionPane.showInputDialog(null, "Employee Name : " + employeeName + "\n\n" + payslipFinal, "Payslip Breakdown");
}
}
改变
payslipFinal = String.format("Gross Pay :€%.2f \nPRSI : €%.2f \nUSC : €%.2f \nPension Plan : €%.2f \nHealth Insurance : €75.00 \nNet Earnings: €%.2f"
+ employeeGrossPay, prsiCalc, uscCalc, pensionCalc, netValue);
到
payslipFinal = String.format("Gross Pay :€%.2f \nPRSI : €%.2f \nUSC : €%.2f \nPension Plan : €%.2f \nHealth Insurance : €75.00 \nNet Earnings: €%.2f"
, employeeGrossPay, prsiCalc, uscCalc, pensionCalc, netValue);
测试:
public static void main(String[] args) {
String payslipFinal = String.format("Gross Pay :€%.2f \nPRSI : €%.2f \nUSC : €%.2f \nPension Plan : €%.2f \nHealth Insurance : €75.00 \nNet Earnings: €%.2f"
, 1f, 2f, 3f, 4f, 5f);
System.out.println(payslipFinal);
}
输出:
Gross Pay :€1,00
PRSI : €2,00
USC : €3,00
Pension Plan : €4,00
Health Insurance : €75.00
Net Earnings: €5,00