【发布时间】:2017-09-22 02:57:06
【问题描述】:
我不断收到此错误,提示类型不兼容:java.io.PrintStream cannot be converted to java.lang.String,但我不知道如何让它适用于我的 toString 方法。我已经尝试将它分配给一个变量然后返回它,然后只是计划将它作为返回语句打印出来,我看不出我的 printf 格式有什么问题。帮助表示赞赏。
import java.text.NumberFormat;
public class Item
{
private String name;
private double price;
private int quantity;
// -------------------------------------------------------
// Create a new item with the given attributes.
// -------------------------------------------------------
public Item (String itemName, double itemPrice, int numPurchased)
{
name = itemName;
price = itemPrice;
quantity = numPurchased;
}
// -------------------------------------------------------
// Return a string with the information about the item
// -------------------------------------------------------
public String toString ()
{
return System.out.printf("%-15s $%-8.2f %-11d $%-8.2f", name, price,
quantity, price*quantity);
}
// -------------------------------------------------
// Returns the unit price of the item
// -------------------------------------------------
public double getPrice()
{
return price;
}
// -------------------------------------------------
// Returns the name of the item
// -------------------------------------------------
public String getName()
{
return name;
}
// -------------------------------------------------
// Returns the quantity of the item
// -------------------------------------------------
public int getQuantity()
{
return quantity;
}
}
【问题讨论】:
-
您正在尝试将双精度打印为浮点数,使用
d进行双精度,f用于浮点数
标签: java printf bluej incompatibletypeerror