我正在开发一个餐厅计算器程序,它依赖于数组。
我在控制台中收到有关格式化的错误,但它会阻止显示结果。有问题的行是:
System.out.printf("\n%-10s%4d%10.2f\n", "Person#", "Age", "TotalBill");
...
System.out.printf("Grand total = $ %.2f\n" + grandTotal);
完整的异常堆栈跟踪是:
Exception in thread "main" java.util.IllegalFormatConversionException: d != java.lang.String
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 BillRestaurant.main(BillRestaurant.java:191)
以下是代码:
import java.util.*;
public class BillRestaurant {
//Hardcode the prices of the meals & tax & discount
private final double Soup = 2.50, Wings = .15, Burger = 4.95, ChickenSandwich = 5.95, Fries = 1.99, Pie = 2.95, IceCream = 2.99,
SoftDrink = 1.50, Coffee = 1.00, Discount = 0.25, Tax = 0.05;
//Declare variables
private int age, soup, wings, burger, chickensandwich, fries, pie, icecream, softdrink, coffee;
private double totalbill;
public BillRestaurant()
{
//Sets every thing to zero
this.age = 0;
this.soup = 0;
this.wings = 0;
this.burger = 0;
this.chickensandwich = 0;
this.fries = 0;
this.pie = 0;
this.icecream = 0;
this.softdrink = 0;
this.coffee = 0;
this.totalbill = 0;
}
public BillRestaurant(int age, int soup, int wings, int burger, int chickensandwich, int fries, int pie, int icecream, int softdrink, int coffee)
{
//Assigns the value of the parameter a to the field of the same name
this.age = age;
this.soup = soup;
this.wings = wings;
this.burger = burger;
this.chickensandwich = chickensandwich;
this.fries = fries;
this.pie = pie;
this.icecream = icecream;
this.softdrink = softdrink;
this.coffee = coffee;
calcualteTotalBill();
}
private void calcualteTotalBill()
{
//Apply discount to teens and seniors, tax, and kids eat free to the Total bill
if(age <= 12)
totalbill = 0;
else
totalbill = soup * Soup + wings * Wings + burger + Burger + chickensandwich * ChickenSandwich + fries * Fries + pie * Pie + icecream * IceCream +
softdrink * SoftDrink + coffee * Coffee;
if((age >= 13 && age >= 18) || age >= 65)
totalbill = totalbill - totalbill * Discount;
if(age <=12 || (age > 18 && age <65))
totalbill = totalbill + totalbill * Tax;
}
public double getTotalbill()
{
return totalbill;
}
public int getAge()
{
return age;
}
//Main Method
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
//Declare variables
int number, age, soup, wings, burger, chickensandwich, fries, pie, icecream, softdrink, coffee, count;
int more = 0;
//Ask how many people will eat
do
{
System.out.print("Number of people: ");
number = input.nextInt();
BillRestaurant[] orders = new BillRestaurant[number];
System.out.println("Every person who eats at this restaurant orders exactly three items. Never greater, never fewer.");
//Starts Placing the order. User needs to choose 3 items.
for(int i = 0; i < number; i++)
{
soup = 0;
wings = 0;
burger = 0;
chickensandwich = 0;
fries = 0;
pie = 0;
icecream = 0;
softdrink = 0;
coffee = 0;
count = 0;
System.out.println("Enter order for person " + (i + 1));
System.out.print("Age ");
age = input.nextInt();
System.out.print("Soup ");
soup = input.nextInt();
if(soup > 0)
count++;
System.out.print("Wings ");
wings = input.nextInt();
if(wings > 0)
count++;
System.out.print("Burger ");
burger = input.nextInt();
if(burger > 0)
count++;
if(count ==3)
{
System.out.println("Order complete. ");
orders[i] = new BillRestaurant(age, soup, wings, burger, chickensandwich, fries, pie, icecream, softdrink, coffee);
}
else
{
System.out.print("ChickenSandwich ");
chickensandwich = input.nextInt();
if(chickensandwich > 0)
count++;
if(count ==3)
{
System.out.println("Order complete. ");
orders[i] = new BillRestaurant(age, soup, wings, burger, chickensandwich, fries, pie, icecream, softdrink, coffee);
}
else
{
System.out.print("Fries ");
fries = input.nextInt();
if(fries > 0)
count++;
if(count ==3)
{
System.out.println("Order complete. ");
orders[i] = new BillRestaurant(age, soup, wings, burger, chickensandwich, fries, pie, icecream, softdrink, coffee);
}
else
{
System.out.print("Pie ");
pie = input.nextInt();
if(pie > 0)
count++;
if(count ==3)
{
System.out.println("Order complete. ");
orders[i] = new BillRestaurant(age, soup, wings, burger, chickensandwich, fries, pie, icecream, softdrink, coffee);
}
else
{
System.out.print("IceCream ");
icecream = input.nextInt();
if(icecream > 0)
count++;
if(count ==3)
{
System.out.println("Order complete. ");
orders[i] = new BillRestaurant(age, soup, wings, burger, chickensandwich, fries, pie, icecream, softdrink, coffee);
}
else
{
System.out.print("SoftDrink ");
softdrink = input.nextInt();
if(softdrink > 0)
count++;
if(count ==3)
{
System.out.println("Order complete. ");
orders[i] = new BillRestaurant(age, soup, wings, burger, chickensandwich, fries, pie, icecream, softdrink, coffee);
}
else
{
System.out.print("Cofee ");
coffee = input.nextInt();
if(coffee > 0)
count++;
if(count ==3)
{
System.out.println("Order complete. ");
orders[i] = new BillRestaurant(age, soup, wings, burger, chickensandwich, fries, pie, icecream, softdrink, coffee);
}
else
{
System.out.println("You order less than 3 items. Please try again. ");
i--;
}
}
}
}
}
}
}
}
System.out.printf("\n%-10s%4d%10.2f\n", "Person#", "Age", "TotalBill");
double grandTotal = 0;
for(int i = 0; i < number; i++)
{
System.out.printf("%-10d%4d%10.2f\n", (i + 1), orders[i].getAge(), orders[i].getTotalbill());
grandTotal = grandTotal + orders[i].getTotalbill();
}
System.out.printf("Grand total = $ %.2f\n" + grandTotal);
System.out.print("\nEnter 1 if someone else needs to order: ");
more = input.nextInt();
System.out.println();
}while(more == 1);
}
}
答案 0 :(得分:0)
错误信息非常清楚。 &#39; d&#39;格式不适用于字符串。在标题中,您正在尝试打印3个字符串:
&#34;人#&#34;,&#34;年龄&#34;和&#34; TotalBill&#34;。这些是字符串。
因此,将标题输出更改为仅使用字符串:
System.out.printf("\n%-10s%4d%10.2f\n", "Person#", "Age", "TotalBill");
如,
System.out.printf("\n%-10s%4s%10s\n", "Person#", "Age", "TotalBill");
然后这条线看起来也很可疑:
System.out.printf("Grand total = $ %.2f\n" + grandTotal);
我怀疑你的意思是:
System.out.printf("Grand total = $ %.2f", grandTotal);