非法转换异常?
Illegal conversion exception?
我在我的代码中收到一条错误消息,用于查找支付 200 美元的员工的总工资 + 佣金金额。一旦输入了所有员工的总销售额,就应该打印出属于每个不同薪酬类别的员工的数量。这是代码:
public static void main(String[]args){
Scanner input = new Scanner(System.in);
int basePay = 200; //0 starting amount of pay
int totalPay = 0; //total amount the employee will receive
System.out.print("Enter the amount of employees: ");
int employee = input.nextInt();
int[]sales = new int[9]; //four different categories
for(int i = 0; i < employee; i++){
System.out.println("Enter the current employee's gross sales: ");
int grossSales = input.nextInt();
totalPay = (int) (basePay + (grossSales * 0.09)); //uses the formula for finding pay, and then casts it to an int
if(totalPay <= 299.99){
sales[0]++;
}
else if(totalPay <= 399.99){
sales[1]++;
}
else if(totalPay <= 499.99){
sales[2]++;
}
else if(totalPay <= 599.99){
sales[3]++;
}
else if(totalPay <= 699.99){
sales[4]++;
}
else if(totalPay <= 799.99){
sales[5]++;
}
else if(totalPay <= 899.99){
sales[6]++;
}
else if(totalPay <= 999.99){
sales[7]++;
}
else if(totalPay >= 1000){
sales[8]++;
}
}
System.out.println("Total Sales\tAmount of employees");
for(int i = 0; i < 11; i++){
System.out.printf("%02d-%02d: ", i * 100, i * 100 + 99.99);
System.out.print("\t" + sales[i]);
}
System.out.println("Checking array: " + Arrays.toString(sales));
}
这是我收到的确切错误消息:
00-Exception in thread "main" java.util.IllegalFormatConversionException: d != java.lang.Double
at java.util.Formatter$FormatSpecifier.failConversion(Unknown Source)
at java.util.Formatter$FormatSpecifier.printInteger(Unknown Source)
at java.util.Formatter$FormatSpecifier.print(Unknown Source)
at java.util.Formatter.format(Unknown Source)
at java.io.PrintStream.format(Unknown Source)
at java.io.PrintStream.printf(Unknown Source)
at chapter7.Sales_comission.main(Sales_comission.java:54)
我相信这与双重转换有关,但我不确定它有什么问题?谁能帮我找出问题所在(编译没有错误)?我也试过只有双打(包括数组),但这并没有解决问题。
'd' 用于整数,将其更改为 'f' 或将第二个参数转换为整数。
System.out.printf("%02d-%02d: ", i * 100, (int)(i * 100 + 99.99));
我在我的代码中收到一条错误消息,用于查找支付 200 美元的员工的总工资 + 佣金金额。一旦输入了所有员工的总销售额,就应该打印出属于每个不同薪酬类别的员工的数量。这是代码:
public static void main(String[]args){
Scanner input = new Scanner(System.in);
int basePay = 200; //0 starting amount of pay
int totalPay = 0; //total amount the employee will receive
System.out.print("Enter the amount of employees: ");
int employee = input.nextInt();
int[]sales = new int[9]; //four different categories
for(int i = 0; i < employee; i++){
System.out.println("Enter the current employee's gross sales: ");
int grossSales = input.nextInt();
totalPay = (int) (basePay + (grossSales * 0.09)); //uses the formula for finding pay, and then casts it to an int
if(totalPay <= 299.99){
sales[0]++;
}
else if(totalPay <= 399.99){
sales[1]++;
}
else if(totalPay <= 499.99){
sales[2]++;
}
else if(totalPay <= 599.99){
sales[3]++;
}
else if(totalPay <= 699.99){
sales[4]++;
}
else if(totalPay <= 799.99){
sales[5]++;
}
else if(totalPay <= 899.99){
sales[6]++;
}
else if(totalPay <= 999.99){
sales[7]++;
}
else if(totalPay >= 1000){
sales[8]++;
}
}
System.out.println("Total Sales\tAmount of employees");
for(int i = 0; i < 11; i++){
System.out.printf("%02d-%02d: ", i * 100, i * 100 + 99.99);
System.out.print("\t" + sales[i]);
}
System.out.println("Checking array: " + Arrays.toString(sales));
}
这是我收到的确切错误消息:
00-Exception in thread "main" java.util.IllegalFormatConversionException: d != java.lang.Double
at java.util.Formatter$FormatSpecifier.failConversion(Unknown Source)
at java.util.Formatter$FormatSpecifier.printInteger(Unknown Source)
at java.util.Formatter$FormatSpecifier.print(Unknown Source)
at java.util.Formatter.format(Unknown Source)
at java.io.PrintStream.format(Unknown Source)
at java.io.PrintStream.printf(Unknown Source)
at chapter7.Sales_comission.main(Sales_comission.java:54)
我相信这与双重转换有关,但我不确定它有什么问题?谁能帮我找出问题所在(编译没有错误)?我也试过只有双打(包括数组),但这并没有解决问题。
'd' 用于整数,将其更改为 'f' 或将第二个参数转换为整数。
System.out.printf("%02d-%02d: ", i * 100, (int)(i * 100 + 99.99));