我成功编制了库存计划:
// Inventory.java part 1
// this program is to calculate the value of the inventory of the Electronics Department's cameras
import java.util.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
public class Inventory
{
public static void main(String[] args)
{
// create Scanner to obtain input from the command window
Scanner input = new Scanner (System.in);
String name;
int itemNumber; // first number to multiply
int itemStock; // second number to multiply
double itemPrice; //
double totalValue; // product of number1 and number2
while(true){ // infinite loop
// make new Camera object
System.out.print("Enter Department name: "); //prompt
String itemDept = input.nextLine(); // read name from user
if(itemDept.equals("stop")) // exit the loop
break;
{
System.out.print("Enter item name: "); // prompt
name = input.nextLine(); // read first number from user
input.nextLine();
}
System.out.print("Enter the item number: "); // prompt
itemNumber = input.nextInt(); // read first number from user
input.nextLine();
while( itemNumber <= -1){
System.out.print("Enter valid number:"); // prompt
itemNumber = input.nextInt(); // read first number from user input.nextLine();
} /* while statement with the condition that negative numbers are entered
user is prompted to enter a positive number */
System.out.print("Enter number of items on hand: "); // prompt
itemStock = input.nextInt(); // read first number from user
input.nextLine();
while( itemStock <= -1){
System.out.print("Enter positive number of items on hand:"); // prompt
itemStock = input.nextInt(); // read first number from user
input.nextLine();
} /* while statement with the condition that negative numbers are entered
user is prompted to enter a positive number */
System.out.print("Enter item Price: "); // prompt
itemPrice = input.nextDouble(); // read second number from user
input.nextLine();
while( itemPrice <= -1){
System.out.print("Enter positive number for item price:"); // prompt
itemPrice = input.nextDouble(); // read first number from user
input.nextLine();
} /* while statement with the condition that negative numbers are entered
user is prompted to enter a positive number */
Cam camera = new Cam(name, itemNumber, itemStock, itemPrice);
totalValue = itemStock * itemPrice; // multiply numbers
System.out.println("Department name:" + itemDept); // display Department name
System.out.println("Item number: " + camera.getItemNumber()); //display Item number
System.out.println("Product name:" + camera.getName()); // display the item
System.out.println("Quantity: " + camera.getItemStock());
System.out.println("Price per unit" + camera.getItemPrice());
System.out.printf("Total value is: $%.2f\n" + totalValue); // display product
} // end while method
} // end method main
}/* end class Inventory */
class Cam{
private String name;
private int itemNumber;
private int itemStock;
private double itemPrice;
private String deptName;
public Cam(String name, int itemNumber, int itemStock, double itemPrice) {
this.name = name;
this.itemNumber = itemNumber;
this.itemStock = itemStock;
this.itemPrice = itemPrice;
}
public String getName(){
return name;
}
public int getItemNumber(){
return itemNumber;
}
public int getItemStock(){
return itemStock;
}
public double getItemPrice(){
return itemPrice;
}
}
C:\ Java&gt; java库存
输入部门名称:电子产品 输入项目名称:相机
输入项目编号:12345
输入现有物品数量:8
输入项目价格:100.50
部门名称:电子产品 产品编号:12345
产品名称:相机
数量:8
每单位价格100.5
总价值是:
$线程“main”中的异常java.util.MissingFormatArgumentException:
格式说明符'.2f'
在java.util.Formatter.format(未知来源)
在java.io.PrintStream.format(未知来源)
在java.io.PrintStream.printf(未知来源)
在Inventory.main(Inventory.java:82)
我似乎遇到了这种格式错误,看不出原因。
答案 0 :(得分:21)
如果您使用printf
,则需要将占位符指定为printf
参数以及格式字符串。在你的情况下,你通过附加金额来传递一个字符串,因此错误。
System.out.printf("Total value is: $%.2f\n" + totalValue)
应替换为
System.out.printf("Total value is: $%.2f\n", totalValue)
答案 1 :(得分:6)
这是问题所在:
System.out.printf("Total value is: $%.2f\n" + totalValue);
我认为你的意思是:
System.out.printf("Total value is: $%.2f\n", totalValue);
换句话说,指定替换占位符的值,而不是仅使用字符串连接将值添加到格式字符串的发送中。
一般情况下,当你得到一个你不明白的例外时,你应该看看它的文档。在这种情况下,docs are reasonably clear:
如果格式说明符没有相应的参数,或者参数索引引用了不存在的参数,则抛出未经检查的异常。
因此,您需要在代码中检查两件事:
你没有在格式字符串中指定任何参数索引,所以它必须是第一种情况 - 实际上它是。
答案 2 :(得分:2)
System.out.printf("Total value is: $%.2f\n", totalValue); // display product
- &GT; http://www.java2s.com/Code/JavaAPI/java.lang/Systemoutprintf2ffloatf.htm