求大神指教,很急
import java.util.Scanner;
public class Rectangle {
double width;
double length;
double graph(double w,double l){
width=w;
length=l;
return width*length;
}
double graph(){System.out.println("請輸入長方形的長和寬");
? ? ? ? ? ? ? ? ? ?Scanner input=new Scanner(System.in);
width=input.nextDouble();
length=input.nextDouble();
return width*length; }
Rectangle(double w,double l){ ? ? ? ? ? ? ? //在這里我不是定義了嗎?
System.out.println("請輸入長方形的長和寬");
Scanner input=new Scanner(System.in);
w=input.nextDouble();
l=input.nextDouble();
System.out.println("長方形的面積為"+(w*l));
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Rectangle wlh=new Rectangle(); ? ? ? ? ? ? //系統(tǒng)提示這行沒有定義,為什么啊,前面我不是定義了嗎?
System.out.println("長方形的面積為"+wlh.graph(2, 1));
System.out.println("長方形的面積為"+wlh.graph());
}
}
2015-04-23
好像還要補充空參數(shù)的構(gòu)造方法,Rectangle(),你定義的是帶參數(shù)的?Rectangle(double w,double l)
2015-04-23
在類中加一個構(gòu)造方法就可以了
public Rectangle() {
}