线程" main"中的例外情况java.util.UnknownFormatConversionException:Conversion =' r'

时间:2015-09-22 03:49:08

标签: java

您好我是编程新手并且卡住了,无法想出这个转换异常。我看了,但至少从我所知的搜索中找不到类似的。这是我的代码。有谁看到我做错了什么?

import java.util.Scanner;

public class MonthlySalary
{

   public static void main(String[] args)
   {
    double regularSalary, hours, payRate, totalSalary;
    double overTime, hourlyPay;
    Scanner in = new Scanner (System.in);   

    //regular hours
    System.out.print("How regular hours did you work this month? ");
    hours = in.nextDouble();

    System.out.print("How much do you get paid per hour? ");
    payRate = in.nextDouble();

    regularSalary = (payRate * hours);
    System.out.printf("Your monthly salary is $ %.2f\n", regularSalary);

    //overtime
    System.out.print("How many overtime hours did you work this month? ");
    overTime = in.nextDouble();

    System.out.print("How much is your hourly pay? ");
    hourlyPay = in.nextDouble();

    double tempoverTimePay = overTimeCalculator(hourlyPay, overTime);

    //total salary for the year
    totalSalary = ((tempoverTimePay + regularSalary) * 12);
    System.out.printf("This is how much you will make for the year if it continues at this rate: $ %.2f ", totalSalary);

    //salary plus raise
    System.out.printf("This is how much you will make if you get a 10% raise $ %.2f", ((totalSalary * .10) + totalSalary));

    //the man gets his cut
    if(totalSalary <= 13150)
      {
       System.out.printf("You will pay $ %.2f\n", (totalSalary * .10) + "in taxes.");
      }
    else
      if(totalSalary <= 50200)
      {   
       System.out.printf("You will pay $ %.2f\n", (totalSalary * .15) + "in taxes.");
      }
    else   
      if(totalSalary <= 129600)
         {
          System.out.printf("You will pay $ %.2f\n", (totalSalary * .25) + "in taxes.");
         }
    else
      if(totalSalary <= 209850)
      {   
       System.out.printf("You will pay $ %.2f\n", (totalSalary * .28) + "in taxes.");
      }
    else
      if(totalSalary <= 411500)
      {
       System.out.printf("You will pay $ %.2f\n", (totalSalary * .33) + "in taxes.");
      }
    else   
      if(totalSalary <= 439000)
      { 
       System.out.printf("You will pay $ %.2f\n", (totalSalary * .35) + "in taxes.");
      }
    else  
      if(totalSalary <= 439001)
        { 
         System.out.printf("You will pay $ %.2f\n", (totalSalary * .40) + "in taxes.");        
        }
   }

   //overtime method
   public static double overTimeCalculator(double declaredPayRate, double declaredOverTime)   
   {
    double overTimePay = ((declaredPayRate * 1.5) * declaredOverTime);
    System.out.printf("Your over time pay is $ %.2f\n", overTimePay);
    return overTimePay;
   }
}

1 个答案:

答案 0 :(得分:0)

更改10% raise

至:10%% raise

printf假设%中的10%是格式说明符,而实际上它不是,所以&#34;转义&#34;在printf语句中%,你必须加倍。

例如:

System.out.printf("This is how much you will make if you get a 10%% raise $ %.2f", 
       ((totalSalary * .10) + totalSalary));

我刚刚运行了您的代码,您的异常就在我指示的行上,并且是由于我上面指出的问题。由您决定如何从这里开始,但我建议修复我提到的错误。然后,一旦你解决了这个问题,你就会遇到其他问题,包括这一行:

System.out.printf("You will pay $ %.2f\n", (totalSalary * .15)
                + "in taxes.");

这是不正确的。税收中的&#34;应采用字符串格式,\n应更改为%n

System.out.printf("You will pay $ %.2f%n in taxes.", (totalSalary * .15));

后面的类似代码行相同。