Net*_*ham 6 java format inventory
我成功编制了库存计划:
// 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;
}
}
Run Code Online (Sandbox Code Playgroud)
C:\ Java> java Inventory
输入部门名称:Electronics
输入项目名称:camera
输入项目编号:12345
输入项目数量:8
输入项目价格:100.50
部门名称:Electronics
项目编号:12345
产品名称:camera
数量: 8单位
价格100.5
总价值为:
$线程"main"中的异常java.util.MissingFormatArgumentException: java.io.PrintStream.format 中java.util.Formatter.format(未知来源)的
格式说明符'.2f' (来源不明) 在java.io.PrintStream.printf(来源不明) 在Inventory.main(Inventory.java:82)
我似乎遇到这种格式错误,看不出原因.
San*_*rma 21
如果您正在使用printf,则需要将占位符指定为printf参数以及格式字符串.在您的情况下,您通过附加金额传递单个字符串,因此错误.
System.out.printf("Total value is: $%.2f\n" + totalValue)
Run Code Online (Sandbox Code Playgroud)
应该被替换
System.out.printf("Total value is: $%.2f\n", totalValue)
Run Code Online (Sandbox Code Playgroud)
这就是问题:
System.out.printf("Total value is: $%.2f\n" + totalValue);
Run Code Online (Sandbox Code Playgroud)
我想你的意思是:
System.out.printf("Total value is: $%.2f\n", totalValue);
Run Code Online (Sandbox Code Playgroud)
换句话说,指定替换占位符的值,而不是仅使用字符串连接将值添加到格式字符串的发送.
一般来说,当你得到一个你不理解的例外时,你应该看看它的文档.在这种情况下,文档相当清楚:
当存在没有相应参数的格式说明符或参数索引引用不存在的参数时,抛出未经检查的异常.
因此,您需要在代码中检查两件事:
您没有在格式字符串中指定任何参数索引,因此它必须是第一种情况 - 事实上它确实如此.
| 归档时间: |
|
| 查看次数: |
32488 次 |
| 最近记录: |