控制台:
import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter employee's name: "); String name = input.nextLine(); System.out.print("Enter number of hours worked in a week: "); int hoursWorked = input.nextInt(); System.out.print("Enter hourly pay rate: "); double hourlyPayRate = input.nextDouble(); System.out.print("Enter federal tax withholding rate: "); double federalTax = input.nextDouble(); System.out.print("Enter state tax withholding rate: "); double stateTax = input.nextDouble(); input.close(); double grossPay = hoursWorked * hourlyPayRate; double federalTaxPay = grossPay * federalTax; double stateTaxPay = grossPay * stateTax; double totalDeduction = federalTaxPay + stateTaxPay; double netPay = grossPay - totalDeduction; System.out.println("Employee Name: " + name); System.out.println("Hours Worked: " + hoursWorked); System.out.println("Pay Rate: " + "$" + hourlyPayRate); System.out.println("Gross Pay: " + "$" + grossPay); System.out.println("Deductions: " + "\n" + "\t" + "Federal Withholding (" + federalTax + "): " + "$" + federalTaxPay + "\t" + "State Withholding(" + stateTax + "): " + "$" + stateTaxPay + "\t" + "Total Deduction: " + "$" + totalDeduction); System.out.println("Net Pay: " + "$" + netPay); } }
对话框:
import javax.swing.JOptionPane; public class Solution { public static void main(String[] args) { String name = JOptionPane.showInputDialog(null, "Enter employee's name: ", "Employee Name", JOptionPane.QUESTION_MESSAGE); String hoursWorkedString = JOptionPane.showInputDialog(null, "Enter number of hours worked in a week: ", "Work Hour", JOptionPane.QUESTION_MESSAGE); int hoursWorked = Integer.parseInt(hoursWorkedString); String hourlyPayRateString = JOptionPane.showInputDialog(null, "Enter hourly pay rate: ", "Hourly Pay Rate", JOptionPane.QUESTION_MESSAGE); double hourlyPayRate = Double.parseDouble(hourlyPayRateString); String federalTaxString = JOptionPane.showInputDialog(null, "Enter federal tax withholding rate: ", "Federal Tax", JOptionPane.QUESTION_MESSAGE); double federalTax = Double.parseDouble(federalTaxString); String stateTaxString = JOptionPane.showInputDialog(null, "Enter state tax withholding rate: ", "State Tax", JOptionPane.QUESTION_MESSAGE); double stateTax = Double.parseDouble(stateTaxString); double grossPay = hoursWorked * hourlyPayRate; double federalTaxPay = grossPay * federalTax; double stateTaxPay = grossPay * stateTax; double totalDeduction = federalTaxPay + stateTaxPay; double netPay = grossPay - totalDeduction; String output = "Employee Name: " + name + "\n" + "Hours Worked: " + hoursWorked + "\n" + "Pay Rate: " + "$" + hourlyPayRate + "\n" + "Gross Pay: " + "$" + grossPay + "\n" + "Deductions: " + "\n" + "\t" + "Federal Withholding (" + federalTax + "): " + "$" + federalTaxPay + "\n" + "\t" + "State Withholding(" + stateTax + "): " + "$" + stateTaxPay + "\n" + "\t" + "Total Deduction: " + "$" + totalDeduction + "\n" + "Net Pay: " + "$" + netPay; JOptionPane.showMessageDialog(null, output); } }