Java-使用浮点-构造函数错误
我不明白为什么会收到不兼容数据类型的错误,我使用 float
来表示财务值,我不想超过小数点后两位!
我的印象是你可以用 float
来做到这一点,但是我收到一条错误消息,说:
Constructor Magazine 无法应用于给定类型。
当我将 float
设为 7 而不是 7.99 时,效果很好!
我是否误解了 float
是什么,我需要使用 double
来代替吗?
我将只展示我的杂志课程和一些测试课程来演示。
测试类:
以下是我的测试类中尝试使用 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 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);
}
}
I don't understand why I am getting an error of an incompatible data type, I am using a float
for a financial value, I do not want to go past two decimal places!
I was under the impression that you could do this with a float
, however I am getting an error returned to me saying,
Constructor Magazine cannot be applied to given types.
When I just make the float
7 as opposed to 7.99, it works fine!
Have I misunderstood what a float
is, do I need to use a double
instead?
I'll just show my magazine class and a bit of my test class to demonstrate.
Test class:
The following is a segment from my test class trying to use a float
to two decimal places:
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
class:
/**
* 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);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要
Magazine magazine1 = new Magazine ("SanYonic Publishing", "Ayup Magazine", 7.99f, "Yeshumenku Suni", "12/09/2011");
请注意
7.99f< /code> 修复编译问题。
请注意,浮点数和双精度数都不适合货币计算(如果您关心准确性),因为它们只能表示一组离散值。所有货币计算都应使用 BigDecimal 完成。
you need
Magazine magazine1 = new Magazine ("SanYonic Publishing", "Ayup Magazine", 7.99f, "Yeshumenku Suni", "12/09/2011");
note the
7.99f
to fix the compile issue.Note that both floats and doubles are not suitable for monetary calculations (if you care about accuracy), since they can only represent a discrete set of values. All currency calculations should be done with BigDecimal.
在 Java 中,带有小数点的数字默认为
double
。尝试7.99f
。另外,如果您要使用货币进行计算,您应该查看
BigDecimal
以避免以后出现奇怪的舍入错误。In Java, a number with a decimal point in it is a
double
by default. Try7.99f
.Also, if you're doing calculations with currency, you should take a look at
BigDecimal
to avoid weird rounding errors later down the road.