Java printf语句给出错误

时间:2016-10-18 09:53:57

标签: java eclipse printf println

我正在使用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);
   }
}

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

请确保您使用的是JDK 1.5或更高版本,因为该版本中添加了varargs,更具体地说是printf。仅使用JDK 1.5进行编译是不够的,您还需要使用JRE 1.5 +运行程序。

此外,请参阅printf String:其中一个需要Locale作为其第一个参数,另一个需要System.out.printf("%s\n", employeeFirst, employeeLast);个实例。

将其称为以下内容:

UPDATE table1 INNER join table2 on table1.id=table2.tab1_id INNER join table3 on table1.id=table3.tab1_id SET table1.status=1,table2.status=1,table3.status=1,table1.name='Premjith' WHERE table1.id=1

应该有用(这是你评论的代码所做的)。