2013-05-28 74 views
-1
import javabook.*; 


class Triangle 
{ 
    // DATA 
    //............................................................ 

    //Private Variables 
     private double theBase; 
     private double theHeight; 
     private double theArea;          //base and height = Area is enough calculation 

    // CONSTRUCTORS 
    //............................................................ // same name as the class and the file name 
    public Triangle()            
    { 
     this.theBase=0; 
     this.theHeight=0; 
     this.theArea=0; 
    } 

    public Triangle(OutputBox oBox, MainWindow mWindow)   
    { 
     this.theBase=0; 
     this.theHeight=0;           this.theArea=0;            //This is a proof that the area of any triangle is 1/2 b x h’ of a base and height and the ‘area of a triangle is half of the base times the height’. 
    }           


    public void calculateArea()   
    {                        
     InputBox iBox = new InputBox(mWindow); 
     this.theBase = iBox.getDouble("Please enter the length of the base of the triangle "); 
     this.theHeight = iBox.getDouble("Please enter the height of the triangle"); 
     computeArea(); 
     oBox.println(" The area of a triangle of base : " + this.theBase+ " and height : " + this.theHeight +" is equal to : "+ this.theArea); 
    } 

    // METHODS - behaviours 
    //............................................................ 
    public void computeArea() 
    { 
     this.theArea = (this.theBase/2) * this.theHeight;   
                  }                

    // METHODS - gets (accessors) and sets (mutators) 
    //............................................................ 

    //length 
    public void setThebase(double base) 
    { 
      this.theBase= base; 
    } 

    public double getTheBase() 
    { 
      return(this.theBase); 
    } 

    //breath 
    public void setTheHeight(double height) 
    { 
      this.theHeight= height; 
    } 

    public double getTheHeight() 
    { 
      return(this.theHeight); 
    } 

    //area 
    public double getTheArea() 
    { 
      return(this.theArea); 
    } 
} 

我已經丟失的東西在上面this.theBase = iBox.getDouble(「請輸入該三角形的底邊的長度」);任何人都可以指出,我已經離開了它?當我學習java並且會犯錯誤時要溫柔。上缺少計算關鍵點的三角形的面積

這是固定假{字符之後的訂正錯誤消息:

]./Triangle.java:42: cannot find symbol 
symbol : variable mWindow 
location: class Triangle 
     InputBox iBox = new InputBox(mWindow); 
            ^
./Triangle.java:46: cannot find symbol 
symbol : variable oBox 
location: class Triangle 
     oBox.println(" The area of a triangle of base : " + this.theBase+ " and height : " + this.theHeight +" is equal to : "+ this.theArea); 

更新4個錯誤:

./Triangle.java:36: cannot find symbol 
symbol : variable oBox 
location: class Triangle 
     this.oBox = oBox; 
      ^
./Triangle.java:37: cannot find symbol 
symbol : variable mWindow 
location: class Triangle 
     this.mWindow = mWindow;          //This is a proof that the area of any triangle is 1/2 b x h’ of a base and height and the ‘area of a triangle is half of the base times the height’. 
      ^
./Triangle.java:47: cannot find symbol 
symbol : variable mwindow 
location: class Triangle 
     InputBox iBox = new InputBox(mwindow); 
            ^
./Triangle.java:51: cannot find symbol 
symbol : variable oBox 
location: class Triangle 
     oBox.println(" The area of a triangle of base : " + this.theBase+ " and height : " + this.theHeight +" is equal to : "+ this.theArea); 
     ^
Note: App.java uses or overrides a deprecated API. 
Note: Recompile with -Xlint:deprecation for details. 
4 errors 
      ^
+0

「若有所失」 - 你收到任何錯誤訊息?如果是這樣,請發佈它們。 – Sirko

+0

我已經添加了上面的錯誤消息。 – Irishgirl

+0

'this.theHeight = 0'後面缺少分號(除了提到的其他錯誤)。 – iamnotmaynard

回答

3

第一}之後的所有碼不屬於的方法,所以...你需要製作一個。事情是這樣的:

public Triangle(OutputBox oBox, MainWindow mWindow) { 
    this.theBase=0; 
    this.theHeight=0 
    this.theArea=0;           
    } 
    public void calculateArea() {           
    InputBox iBox = new InputBox(mWindow); 
    this.theBase = iBox.getDouble("Please enter the length of the base of the triangle "); 
    this.theHeight = iBox.getDouble("Please enter the height of the triangle"); 
    computeArea(); 
    oBox.println(" The area of a triangle of base : " + this.theBase+ " and height : " + this.theHeight +" is equal to : "+ this.theArea ); 
    } 
} 

然後,你可以調用calculateArea()你犯了一個Triangle對象之後。

如果您有任何疑問或問題,請告知我。


關於你提到的其他錯誤,他們是因爲你沒有存儲mWindowoBox值。更改:

public Triangle(OutputBox oBox, MainWindow mWindow)   
    { 
     this.theBase=0; 
     this.theHeight=0; 
     this.theArea=0; 
     //This is a proof that the area of any triangle is 1/2 b x h’ of a base and height and the ‘area of a triangle is half of the base times the height’. 
    } 

public Triangle(OutputBox oBox, MainWindow mWindow)   
    { 
     this.theBase=0; 
     this.theHeight=0; 
     this.theArea=0; 
     this.oBox = oBox; 
     this.mWindow = mWindow; 
     //This is a proof that the area of any triangle is 1/2 b x h’ of a base and height and the ‘area of a triangle is half of the base times the height’. 
    } 

,並與您的私有變量添加oBoxmWindow

請注意,如果不使用上述構造函數,您將收到運行時錯誤。


import javabook.*; 


class Triangle 
{ 
    // DATA 
    //............................................................ 

    //Private Variables 
     private double theBase; 
     private double theHeight; 
     private double theArea;          //base and height = Area is enough calculation 
     private OutputBox oBox; 
     private MainWindow mWindow; 

    // CONSTRUCTORS 
    //............................................................ // same name as the class and the file name 

    public Triangle(OutputBox oBox, MainWindow mWindow)   
    { 
     this.theBase=0; 
     this.theHeight=0; 
     this.theArea=0;            //This is a proof that the area of any triangle is 1/2 b x h’ of a base and height and the ‘area of a triangle is half of the base times the height’. 
     this.oBox = oBox; 
     this.mWindow = mWindow; 
    }           


    public void calculateArea()   
    {                        
     InputBox iBox = new InputBox(this.mWindow); 
     this.theBase = iBox.getDouble("Please enter the length of the base of the triangle "); 
     this.theHeight = iBox.getDouble("Please enter the height of the triangle"); 
     computeArea(); 
     this.oBox.println(" The area of a triangle of base : " + this.theBase+ " and height : " + this.theHeight +" is equal to : "+ this.theArea); 
    } 

    // METHODS - behaviours 
    //............................................................ 
    public void computeArea() 
    { 
     this.theArea = (this.theBase/2) * this.theHeight;   
                  }                

    // METHODS - gets (accessors) and sets (mutators) 
    //............................................................ 

    //length 
    public void setThebase(double base) 
    { 
      this.theBase = base; 
    } 

    public double getTheBase() 
    { 
      return(this.theBase); 
    } 

    //breath 
    public void setTheHeight(double height) 
    { 
      this.theHeight= height; 
    } 

    public double getTheHeight() 
    { 
      return(this.theHeight); 
    } 

    //area 
    public double getTheArea() 
    { 
      return(this.theArea); 
    } 
} 
+0

我調用了getTheArea。調用getTheArea而不是calculateArea可以嗎? – Irishgirl

+0

當然,這很好,只要它是一致的。 – tsm

+0

computeArea()已經在三角形中定義了public void computeArea() – Irishgirl