永发信息网

java编程时找不到符号是什么问题,代码如下:public class Rectangle{ float length; float width; public

答案:5  悬赏:20  手机版
解决时间 2021-03-02 13:48
  • 提问者网友:戎马万世
  • 2021-03-01 22:11
public class Rectangle{
float length;
float width;
public void Rectangle(float length,float width){
this.length=length;
this.width=width;
}

public float getLength(){
return length;

}

public float getWidth(){
return width;
}

public static void main(String[]args){
Rectangle s=new Rectangle(5.0,6.0);
System.out.println("长方形的面积为:"+s.getLength()*s.getWidth());
}
}
最佳答案
  • 五星知识达人网友:千杯敬自由
  • 2021-03-01 23:49
先附上正确代码
public class Rectangle{
float length;
float width;
public Rectangle(float length,float width){
this.length=length;
this.width=width;
}

public float getLength(){
return length;

}

public float getWidth(){
return width;
}

public static void main(String[]args){
Rectangle s=new Rectangle(5.0f,6.0f);
System.out.println("长方形的面积为:"+s.getLength()*s.getWidth());
}
}

首先构造函数前面不应该有void来修饰,构造函数是一个特殊函数,方法名与类名相同,没有返回值,其次float类型的值后面要加上f以便和double型区分,这是你代码里的两个错误
全部回答
  • 1楼网友:十年萤火照君眠
  • 2021-03-02 03:44
public void Rectangle(float length,float width){ this.length=length; this.width=width; } 明显错误构造函数不带返回值的 其次float类型的值后面要加上f以便和double型区分
  • 2楼网友:老鼠爱大米
  • 2021-03-02 02:24
public void Rectangle(float length,float width){ 构造方法不要写返回值 new Rectangle(5.0,6.0); 5.0 6.0后面记得加f
  • 3楼网友:枭雄戏美人
  • 2021-03-02 01:52
你写的有两个地方有问题: 第一个: public void Rectangle(float length,float width) 构造方法不是这样写的,应该是 public Rectangle(float length,float width){ 第二个: Rectangle s=new Rectangle(5.0,6.0);这里的参数默认是double类型的,所以应该是这样写 Rectangle s=new Rectangle(5.0f,6.0f);
  • 4楼网友:封刀令
  • 2021-03-02 00:31
首先 你的构造函数是float型的
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯