当前位置: 代码迷 >> Java相关 >> 怎么结果是0
  详细解决方案

怎么结果是0

热度:247   发布时间:2006-03-26 16:59:00.0
怎么结果是0

public class Square{
private double height;
private double weight;
private double surfaceArea;

public Square(double height,double weigth){
this.height=height;
this.weight=weight;
}
public void computeSurfaceArea(){
surfaceArea=getHeight() * getWeight();
}
public double getHeight(){
return height;
}
public double getWeight(){
return weight;
}
public double getSurfaceArea(){
return surfaceArea;
}

}
public class DemoSquare{
public static void main(String args[]){
Square square=new Square(3,4);

square.computeSurfaceArea();

System.out.println(square.getSurfaceArea());
}
}

搜索更多相关的解决方案: 结果  

----------------解决方案--------------------------------------------------------
public Square(double height,double weigth){
this.height=height;
this.weight=weight;
}
你打错字了,按你的程序应该是weight
否则weight就是0了,那结果当然是0;

----------------解决方案--------------------------------------------------------

呵呵,楼上的真是高明啊,这么细节的东西都看出来了,佩服。我找了半天啊;


----------------解决方案--------------------------------------------------------


强!


----------------解决方案--------------------------------------------------------
这样也不对 还应该去掉第一个public
因为一个程序中只能有一个主类 程序如下:
class Square{
private double height;
private double weight;
private double surfaceArea;

public Square(double height,double weight){
this.height=height;
this.weight=weight;
}
public void computeSurfaceArea(){
surfaceArea=getHeight() * getWeight();
}
public double getHeight(){
return height;
}
public double getWeight(){
return weight;
}
public double getSurfaceArea(){
return surfaceArea;
}

}
public class DemoSquare{
public static void main(String args[]){
Square square=new Square(3,4);

square.computeSurfaceArea();

System.out.println(square.getSurfaceArea());
}
}


----------------解决方案--------------------------------------------------------
这两个类不在一个文件不就可以都是pubilc 了
----------------解决方案--------------------------------------------------------
  相关解决方案