我不明白为什么我会收到数据类型不兼容的错误,我使用 afloat
作为财务值,我不想超过小数点后两位!
我的印象是你可以用 a 来做到这一点float
,但是我收到一个错误返回给我说,
构造函数杂志不能应用于给定类型。
当我只制作float
7 而不是 7.99 时,它工作正常!
我是否误解了 afloat
是什么,我需要使用 adouble
代替吗?
我只会展示我的杂志课和一些我的测试课来演示。
测试类:
以下是我的测试类中尝试使用float
小数点后两位的片段:
public static void main()
{
Magazine magazine1 = new Magazine("SanYonic Publishing", "Ayup Magazine", 7.99, "Yeshumenku Suni", "12/09/2011");
System.out.println();
magazine1.getEditor();
magazine1.getDate();
magazine1.getPublisher();
magazine1.getPublicationTitle();
magazine1.getPrice();
System.out.println();
…
}
Magazine
班级:
/**
* Magazine Class - This class represents Magazine Objects
*/
public class Magazine extends Publication
{
private String editor;
private String date;
public Magazine(String publisherIn , String publicationTitleIn, float priceIn, String editorIn, String dateIn)
{
super (publisherIn , publicationTitleIn, priceIn);
editor = editorIn;
date = dateIn;
}
public void setPublication(String publisherIn, String publicationTitleIn, float priceIn)
{
publisherIn = publisher;
publicationTitleIn = publicationTitle;
priceIn = price;
}
public String getEditor()
{
System.out.println("The editor of this magazine is " + editor);
return (editor);
}
public String getDate()
{
System.out.println("The publication date of this magazine is " + date);
return (date);
}
public String getPublisher()
{
System.out.println("The publisher of this magazine is " + publisher);
return (publisher);
}
public String getPublicationTitle()
{
System.out.println("The publication title of this magazine is " + publicationTitle);
return (publicationTitle);
}
public float getPrice()
{
System.out.println("The price of this magazine is £" + price);
return (price);
}
}