我搜索,找不到像我的例子一樣簡單。任何人都可以告訴我爲什麼我得到這個錯誤?運行時,倍增倍數
import java.util.Scanner;
public class Shopping {
public static void main(String[] args) {
Scanner keyboard = new java.util.Scanner(System.in);
System.out.println("What is the item name?");
String itemName = keyboard.nextLine();
System.out.println("What is the item price?");
double itemPrice = keyboard.nextFloat();
System.out.println("What is the item quantity?");
double itemQuantity = keyboard.nextFloat();
double total = itemQuantity * itemPrice;
System.out.printf("%d x %s @ $%d Total $%d",itemQuantity,itemName,itemPrice,total);
} //main
} // class Shopping
錯誤:
java.util.IllegalFormatConversionException: d != java.lang.Double
at java.util.Formatter$FormatSpecifier.failConversion(Formatter.java:4302)
at java.util.Formatter$FormatSpecifier.printInteger(Formatter.java:2793)
at java.util.Formatter$FormatSpecifier.print(Formatter.java:2747)
at java.util.Formatter.format(Formatter.java:2520)
at java.io.PrintStream.format(PrintStream.java:970)
at java.io.PrintStream.printf(PrintStream.java:871)
at Shopping.main(Shopping.java:13)
我原先設置itemQuantity如int,但這都吐出來了錯誤。我認爲這是因爲不匹配的類型,但是在我糾正之後,錯誤仍然存在。 任何幫助將不勝感激。
您試圖使用'%d'格式化一個double,這是數字的格式鍵。使用'%f'作爲浮點值。 – Vulcan