2017-02-17 41 views
-2

*(同樣這個問題的假設重複並不真正幫助我) 我想編譯這個相當簡單的程序,它顯示並移動一個足跡來演示運動。這是我與現在的工作:(Java)編譯時出現實例變量時出錯

// Represents a foot, used for displaying walking creatures. 

import java.awt.Image; 
import java.awt.Graphics; 

public class Foot 
{ 
    private Image picture; 
    private CoordinateSystem coordinates; 

    // Constructor 
    public Foot(int x, int y, Image pic) 
    { 
    picture = pic; 
    coordinates = new CoordinateSystem(x, y, pic); 
    } 

    // Moves this foot forward by distance pixels 
    // (or backward if distance < 0). 
    public void moveForward(int distance) 
    { 
    coordinates.shift(distance, 0); 
    } 

    // Moves this foot sideways by distance pixels 
    // (to the right if distance > 0 or to the left 
    // if distance < 0). 
    public void moveSideways(int distance) 
    { 
    coordinates.shift(0, distance); 
    } 

    // Turns this foot (clockwise for degrees > 0). 
    public void turn(int degrees) 
    { 
    coordinates.rotate(Math.PI * degrees/180.0); 
    } 

    // Draws this foot in the appropriate coordinate system. 
    public void draw(Graphics g) 
    { 
    coordinates.drawImage(g, picture); 
    } 
} 

然而,當我試圖編譯程序我得到的錯誤:

cannot find symbol 
    private CoordinateSystem coordinates; 
     ^
     symbol: class CoordinateSystem 
     location: class Foot 
    Foot.java:15: error: cannot find symbol 
     coordinates = new CoordinateSystem(x, y, pic); 
        ^
     symbol: class CoordinateSystem 
    location: class Foot 
2 errors 

我相信,這是一個簡單的解決,但我是新來的Java和解釋爲什麼我得到這個錯誤消息將不勝感激。

+0

這是一個重複的問題並沒有真正幫助我理解爲什麼我得到這個錯誤。有人會介意給我一個更具體的解釋嗎? –

回答

0

根據編譯錯誤輸出的問題是類Foot找不到類CoordinateSystem。是CoordinateSystem類在與Foot相同的包中嗎?如果不是,則需要添加一條導入語句:import your.package.name.CoordinateSystem。您還可以檢查CoordinateSystem是否有適當的訪問修飾符(即它是否公開?)。

是否CoordinateSystem在同一個庫/ jar?您可能需要確保您的類路徑已正確配置。

問題還有可能是關於CoordinateSystem的構造函數。被調用的構造函數是否存在?它是否具有Foot的適當可見性來訪問它?

+0

感謝您的意見,我不小心將Foot和CoordinateSystem放在不同的目錄中!但是,現在我收到一條錯誤消息,告訴我在Main類中找不到Main方法,對此有何建議? –

+0

在你的配置中的某個地方,你已經聲明你的Main方法在類'Foot'中。 Main方法是:'public static void main(String [] args)'。根據您發佈的代碼,我在'Foot'中看不到Main方法。該配置在部署時將位於jar清單文件中。如果你是從命令行啓動這個應用程序,並且它沒有打包成jar,那麼你將java引導到了錯誤的入口點(具有Main方法的類)。但是,如果您使用的是IDE,則可能會設置錯誤地運行您的項目。 – Slaw

+0

好的,我將如何去將這個文件打包到jar中? –