[英]need help understand, java: <identifier> expected in the below code
下面是一個查找矩形區域的簡單代碼。 它運行到編譯錯誤,不確定是什么問題,請幫助。
我收到此錯誤 - java:預期:16
public class test {
public static void main(String[] args) {
float l = 7.33f;
float b = 4.22f;
Rectangle R1 = new Rectangle( l,b);
float area = R1.area();
System.out.println("The area of the Rectangle R1 is " + area);
}
}
class Rectangle {
float l;
float b;
public void Rectangle(l,b){
this.l= l;
this.b= b;
}
public float area(){
return this.l*this.b;
}
}
Rectangle class 中的構造函數寫錯了。 它應該如下所示 -
class Rectangle {
float l;
float b;
public Rectangle(float l, float b){ // need to declare the variable types
this.l= l;
this.b= b;
}
public float area(){
return this.l*this.b;
}
}
那是因為你的構造函數public void Rectangle(l,b)
,你把它變成了一個方法
其次,您缺少兩個float
,因此:
public static void main(String[] args) {
float l = 7.33f;
float b = 4.22f;
Rectangle R1 = new Rectangle(l,b);
float area = R1.area();
System.out.println("The area of the Rectangle R1 is " + area);
}
}
class Rectangle {
float l;
float b;
public Rectangle(float l, float b){//changed here
this.l= l;
this.b= b;
}
public float area(){
return this.l*this.b;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.