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
品目名を入力してください: カメラ
品目番号を入力してください: 12345在庫
数を入力してください: 8
品目価格を入力してください: 100.50
部門名: エレクトロニクス
品目番号: 12345
製品名: カメラ
数量: 8
単位あたりの価格 100.5
合計値:
$Exception in thread "main" java.util.MissingFormatArgumentException:
書式指定子 '.2f'
at java.util.Formatter.format(Unknown Source)
at java.io.PrintStream.format( Inventory.main(Inventory.java:82)
の java.io.PrintStream.printf(不明なソース)

このフォーマット エラーが発生したようで、その理由がわかりません。

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);

つまり、文字列の連結を使用してフォーマット文字列の送信に値を追加するのではなく、プレースホルダーを置き換える値を指定します。

ただし、一般的に、理解できない例外が発生した場合は、そのドキュメントを参照する必要があります。この場合、ドキュメントはかなり明確です:

対応する引数を持たないフォーマット指定子がある場合、または引数インデックスが存在しない引数を参照している場合にスローされるチェックされていない例外。

したがって、コードをチェックインする必要があるのは2つあります。

  • 対応する引数のないフォーマット指定子はありますか?
  • 存在しない引数を参照する引数インデックスはありますか?

フォーマット文字列に引数インデックスを指定していないので、最初のケースである必要があります-実際にそうです。

于 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 に答える