编译错误:找不到符号

来源:百度知道 编辑:UC知道 时间:2024/05/10 11:19:09
class Rectangle
{
float width,height;
Rectangle(float width,float height)
{
this.width=width;
this.height=height;
}
float computerPerimeter()
{
float circle;
circle=width+height;
return circle;
}
float computerArea()
{
float area;
area=width*height;
return area;
}
}
public class TestRectangle
{
public static void main (String[] args)
{
Rectangle r1=new Rectangle(5.5,12.6);
Rectangle r2=new Rectangle(2.5,3.6);
System.out.println("r1's perimeter is "+r1.computerPerimeter());
System.out.println("r1's area is "+r1.computerArea());
System.out.println("r2's perimeter is "+r2.computerPerimeter());
System.out.println("r2's area is "+r2.computerArea());
}
}
==================================================================
---------

Rectangle r1=new Rectangle(5.5f,12.6f);
Rectangle r2=new Rectangle(2.5f,3.6f);
数字后要加f才是float型,否则默认为double型,其实double型更常用.

论坛升级成功。发帖时包含代码格式请使用 {code}代码内容{code} ,其中每个{code}各占一行。注,原来的ubb代码[code]代码[/code]继续支持。
发表主题时如果选上“标记为问题”,在得到“有帮助的”或“正确的”答案后,请在回帖旁边点“有帮助的”或“正确的”按钮,系统将奖励积分给该回帖的作者。主题作者的信誉值也会增加。除此之外,还可以在发表主题后自已设定奖励积分。
欢迎访问JAVA中文博客频道:http://blog.chinajavaworld.com/
欢迎, 游客 登录 / 注册
游客设置
论坛主页 ? Java 2 Platform, Standard Edition (J2SE) ? Java语言*初级版
主题: JAVA编译错误!提示找不到符号
回复主题 搜索论坛 返回主题列表
回复: 13......

Rectangle r1=new Rectangle(5.5f,12.6f);
Rectangle r2=new Rectangle(2.5f,3.6f);

类型不匹配,
Rectangle(float width,float height)
{
this.width=width;
this.height=height;
}
此处你定义的是FLOAT类型

在你调用的地方你写的是 DOUBLE类型

要么你统一类型,要么强制转换一下