gpt4 book ai didi

Java printf 语句给出错误

转载 作者:太空宇宙 更新时间:2023-11-04 12:08:27 27 4
gpt4 key购买 nike

我在一项家庭作业中使用 Eclipse,但我真的很挣扎。目标是编写一个工资单程序,让用户输入他们的姓名、工作时间、工资率、联邦和州预扣税,然后输出计算出的预扣金额以及净工资信息。

我使用了我熟悉的 println 语句来显示输出,但老师希望我们使用 System.out.printf 函数,但我根本无法让它工作。如果我使用 println 语句,所有值都会被填充,但由于某种原因,我无法让 printf 执行相同的操作。我缺少什么?如果我使用 printf 函数,它会给我错误

"Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
The method printf(Locale, String, Object[]) in the type PrintStream is not applicable for the arguments (String, String, String)
at Payrolls.main(Payrolls.java:31)"

这是我的代码:

import java.util.Scanner;

public class Payrolls
{
public static void main(String []args)
{


Scanner input = new Scanner(System.in);

System.out.print("Enter employee first name: ");
String employeeFirst = input.nextLine();

System.out.print("Enter employee last name: ");
String employeeLast = input.nextLine();

System.out.print("Enter number of hours worked this week: ");
int hoursWorked = input.nextInt();

System.out.print("Enter hourly pay rate of the employee: ");
double payRate = input.nextDouble();

System.out.print("Enter federal tax withholding rate: ");
double fedTax = input.nextDouble();

System.out.print("Enter state tax withholding rate: ");
double stateTax = input.nextDouble();

System.out.println(" ");
System.out.println(" ");
//System.out.printf("%-20s %s\n", employeeFirst, employeeLast);
System.out.println("Name: " + employeeFirst +" " +employeeLast);

System.out.println("Hours Worked:" + hoursWorked);

System.out.println("Pay Rate:" + payRate);



double grossPay;
grossPay = payRate * hoursWorked;

System.out.println("Gross Pay:" + grossPay);

double deductions;
deductions = grossPay * fedTax;

System.out.println("\tDeductions:");
System.out.println("\t\tFederal Witholding %: $" + deductions);

double statTax;
statTax = grossPay * stateTax;

System.out.println("\t\tState Witholding %: $" + statTax);

double totalDeduction;
totalDeduction = deductions + statTax;

System.out.println("\t\tTotal Deduction: $" + totalDeduction);


double netPay;
netPay = grossPay - totalDeduction;

System.out.println("Net Pay:" + netPay);
}
}

最佳答案

  • Printf 方法可从 java 版本 1.5 开始使用。确保您的jdk版本大于或等于1.5。
  • 您的错误告诉您 printf 方法的第一个参数必须是 Locale 类型,请参阅示例 here

关于Java printf 语句给出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40105050/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com