6

我已经成功编译了我的库存程序:

// 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>java Inventory
输入部门名称:Electronics
输入商品名称:camera
输入商品编号:12345
输入现有商品数量:8
输入商品价格:100.50
部门名称:Electronics
商品编号:12345
商品名称:camera
数量: 8
每单位价格 100.5
总值为:
线程“main”中的 $Exception java.util.MissingFormatArgumentException: java.util.Formatter.format(未知来源) 处 java.io.PrintStream.format处
的格式说明符“.2f” ( Unknown Source) at java.io.PrintStream.printf(Unknown Source) at Inventory.main(Inventory.java:82)



我似乎遇到了这种格式错误,不明白为什么。

4

3 回答 3

22

如果您使用printf,则需要将占位符printf与格式字符串一起指定为参数。在您的情况下,您通过附加金额来传递单个字符串,因此会出现错误。

System.out.printf("Total value is: $%.2f\n" + totalValue)

应该替换为

System.out.printf("Total value is: $%.2f\n", totalValue)
于 2011-10-02T08:09:38.323 回答
6

这就是问题:

System.out.printf("Total value is: $%.2f\n" + totalValue);

我想你的意思是:

System.out.printf("Total value is: $%.2f\n", totalValue);

换句话说,指定要替换占位符的值,而不是仅使用字符串连接将值添加到格式字符串的发送中。

不过,一般来说,当您遇到不理解的异常时,您应该查看它的文档。在这种情况下,文档相当清楚

当存在没有对应参数的格式说明符或参数索引引用不存在的参数时引发未经检查的异常。

因此,您需要在代码中检查两件事:

  • 你有没有相应参数的格式说明符吗?
  • 您是否有一个引用不存在的参数的参数索引?

您没有在格式字符串中指定任何参数索引,因此它必须是第一种情况 - 确实如此。

于 2011-10-02T08:11:16.847 回答
2
System.out.printf("Total value is: $%.2f\n", totalValue); // display product

-> http://www.java2s.com/Code/JavaAPI/java.lang/Systemoutprintf2ffloatf.htm

于 2011-10-02T08:10:33.210 回答