public class Triangle
{
float length; //declared length as float
float breadth; //declared breadth as float
public static void main(String[] args)
{
Triangle Triangle1 = new Triangle();
Triangle Triangle2 = new Triangle();
float area; //declared area as float
Triangle1.length = 11;
Triangle1.breadth = 22;
Triangle2.breadth = 15;
Triangle2.length = 20;
area = 0.5 * Triangle1.length * Triangle1.breadth;
System.out.println("The area of Triangle is" + " " + area);
area = 0.5 * Triangle2.length * Triangle2.breadth;
System.out.println("The area of Triangle is" + " " + area);
}
}
我之前宣布该区域为浮动,并且我得到了complie时间错误,当我将其更改为double时,我可以得到以下输出。 需要知道为什么...... ??
浮动区域;
Error:(24, 49) java: incompatible types: possible lossy conversion from double to float
Error:(27, 49) java: incompatible types: possible lossy conversion from double to float
答案 0 :(得分:1)
在Java中,默认情况下,如2.553的数字是 double ,所以当你这样做时
area = 0.5 * Triangle1.length * Triangle1.breadth;
你正在做 double * float * float 。如果您想使用0.5作为浮点数,则需要在数字后面添加字母 f ,这样您的代码将如下所示:
area = 0.5f * Triangle1.length * Triangle1.breadth;
答案 1 :(得分:1)
您的代码有几个错误。
请保留命名约定。类以大写字母开头,变量不大写。所以.. Triangle triangle1 = new Triangle()
。
为了保留float
中的所有数据,您还需要将float
中的所有数字相乘。默认情况下,0,5是double
,具有比float
更大的小数精度。有两种方法可以解决这个问题:
使用F
或f
指示符(我建议大写的指示符)表示您使用float
。否则Java会将其作为double
:
area = 0.5F * triangle1.length * triangle1.breadth;
或将所有结果转换为float
:
area = (float) 0.5 * triangle1.length * triangle1.breadth;
答案 2 :(得分:0)
您有两种选择:
f
,例如Triangle1.length = 11f;
因此代码变为:
public class Triangle {
float length; // declared length as float
float breadth; // declared breadth as float
public static void main(String[] args) {
Triangle Triangle1 = new Triangle();
Triangle Triangle2 = new Triangle();
float area; // declared area as float
Triangle1.length = 11f;
Triangle1.breadth = 22f;
Triangle2.breadth = 15f;
Triangle2.length = 20f;
area = 0.5f * Triangle1.length * Triangle1.breadth;
System.out.println("The area of Triangle is" + " " + area);
area = 0.5f * Triangle2.length * Triangle2.breadth;
System.out.println("The area of Triangle is" + " " + area);
}
}
area
变量double
类型:您的代码因此变为:
public class Triangle {
float length; // declared length as float
float breadth; // declared breadth as float
public static void main(String[] args) {
Triangle Triangle1 = new Triangle();
Triangle Triangle2 = new Triangle();
double area; // declared area as float
Triangle1.length = 11;
Triangle1.breadth = 22;
Triangle2.breadth = 15;
Triangle2.length = 20;
area = 0.5 * Triangle1.length * Triangle1.breadth;
System.out.println("The area of Triangle is" + " " + area);
area = 0.5 * Triangle2.length * Triangle2.breadth;
System.out.println("The area of Triangle is" + " " + area);
}
}
希望它有所帮助!
答案 3 :(得分:-1)
这样做。
public class Triangle
{
float length; //declared length as float
float breadth; //declared breadth as float
public static void main(String[] args)
{
Triangle Triangle1 = new Triangle();
Triangle Triangle2 = new Triangle();
float area; //declared area as float
Triangle1.length = 11;
Triangle1.breadth = 22;
Triangle2.breadth = 15;
Triangle2.length = 20;
area = (float)0.5 * Triangle1.length * Triangle1.breadth;
System.out.println("The area of Triangle is" + " " + area);
area = (float)0.5 * Triangle2.length * Triangle2.breadth;
System.out.println("The area of Triangle is" + " " + area);
}
}