控制台:
1 import java.util.Scanner; 2 3 public class Solution 4 { 5 public static void main(String[] args) 6 { 7 Scanner input = new Scanner(System.in); 8 9 System.out.print("Enter employee's name: ");10 String name = input.nextLine();11 12 System.out.print("Enter number of hours worked in a week: ");13 int hoursWorked = input.nextInt();14 15 System.out.print("Enter hourly pay rate: ");16 double hourlyPayRate = input.nextDouble();17 18 System.out.print("Enter federal tax withholding rate: ");19 double federalTax = input.nextDouble();20 21 System.out.print("Enter state tax withholding rate: ");22 double stateTax = input.nextDouble();23 24 input.close();25 26 double grossPay = hoursWorked * hourlyPayRate;27 double federalTaxPay = grossPay * federalTax;28 double stateTaxPay = grossPay * stateTax;29 double totalDeduction = federalTaxPay + stateTaxPay;30 double netPay = grossPay - totalDeduction;31 32 System.out.println("Employee Name: " + name);33 System.out.println("Hours Worked: " + hoursWorked);34 System.out.println("Pay Rate: " + "$" + hourlyPayRate);35 System.out.println("Gross Pay: " + "$" + grossPay);36 System.out.println("Deductions: " + "\n" + 37 "\t" + "Federal Withholding (" + federalTax + "): " + "$" + federalTaxPay +38 "\t" + "State Withholding(" + stateTax + "): " + "$" + stateTaxPay + 39 "\t" + "Total Deduction: " + "$" + totalDeduction);40 System.out.println("Net Pay: " + "$" + netPay);41 }42 }
对话框:
1 import javax.swing.JOptionPane; 2 3 public class Solution 4 { 5 public static void main(String[] args) 6 { 7 String name = JOptionPane.showInputDialog(null, "Enter employee's name: ", 8 "Employee Name", JOptionPane.QUESTION_MESSAGE); 9 10 String hoursWorkedString = JOptionPane.showInputDialog(null, "Enter number of hours worked in a week: ", 11 "Work Hour", JOptionPane.QUESTION_MESSAGE);12 int hoursWorked = Integer.parseInt(hoursWorkedString);13 14 String hourlyPayRateString = JOptionPane.showInputDialog(null, "Enter hourly pay rate: ", 15 "Hourly Pay Rate", JOptionPane.QUESTION_MESSAGE);16 double hourlyPayRate = Double.parseDouble(hourlyPayRateString);17 18 String federalTaxString = JOptionPane.showInputDialog(null, "Enter federal tax withholding rate: ", 19 "Federal Tax", JOptionPane.QUESTION_MESSAGE);20 double federalTax = Double.parseDouble(federalTaxString);21 22 String stateTaxString = JOptionPane.showInputDialog(null, "Enter state tax withholding rate: ", 23 "State Tax", JOptionPane.QUESTION_MESSAGE);24 double stateTax = Double.parseDouble(stateTaxString);25 26 double grossPay = hoursWorked * hourlyPayRate;27 double federalTaxPay = grossPay * federalTax;28 double stateTaxPay = grossPay * stateTax;29 double totalDeduction = federalTaxPay + stateTaxPay;30 double netPay = grossPay - totalDeduction;31 32 String output = "Employee Name: " + name + "\n" +33 "Hours Worked: " + hoursWorked + "\n" +34 "Pay Rate: " + "$" + hourlyPayRate + "\n" + 35 "Gross Pay: " + "$" + grossPay + "\n" + 36 "Deductions: " + "\n" + 37 "\t" + "Federal Withholding (" + federalTax + "): " + "$" + federalTaxPay + "\n" +38 "\t" + "State Withholding(" + stateTax + "): " + "$" + stateTaxPay + "\n" + 39 "\t" + "Total Deduction: " + "$" + totalDeduction + "\n" +40 "Net Pay: " + "$" + netPay;41 42 JOptionPane.showMessageDialog(null, output);43 }44 }