2015-06-17 69 views
0

我正在創建一個程序,我可以使用直接從國家氣象服務網站獲取的數據繪製特定城市的最高日溫度。我有數據,但是當我嘗試創建實際圖形時出現程序錯誤。我的錯誤說我需要返回類型,但如果你看我的代碼,我有一個。它的OOP格式,所以我會告訴你的代碼是實現類的一部分。有人能告訴我如何解決我的錯誤?來自網站的圖表數據

我的錯誤:

invalid method declaration; return type required 

我的代碼:

public class createGraph 
{ 
    Picture canvas = null; 
    Graphics g = null; 
    Graphics2D g2 = null; 

    DrawingGraph(int length, int height, Color color) 
    { 
     canvas = new Picture(length, height); 
     canvas.setAllPixelsToAColor(color); 
     g = canvas.getGraphics(); 
     g2 = (Graphics2D)g; 
     g2.setColor(color);  
    } 

    public void drawARectangle(Color color, int x1, int y1, int width, int height) 
    { 
     g2.setColor(color); 
     g2.drawRect(x1, y1, width, height); 
    } 

    public Picture getGraph() 
    { 
     return canvas; 
    } 

    g.drawString("Maximum Temperature Readings", 196, 65); 
    g.drawString("Pensacola, FL - August, 2009", 205, 80); 

    int xPos = 97; 
    for(int day = 1; day <= 31; day++) 
    { 
     xPos = xPos + 13; 
     g.drawLine(xPos, 500, xPos, 510); 

    } 

    for(int yPos = 100; yPos <= 500; yPos +=40) 
    { 
     g.drawLine(93,yPos, 100, yPos); 

    } 

    g.drawString("100", 70, 105); 
    g.drawString("90", 75, 145); 
    g.drawString("80", 75, 185); 
    g.drawString("70", 75, 225); 
} 

最終圖表應該結束這樣看:

graph

回答

1

這個代碼在這裏:

DrawingGraph(int length, int height, Color color) 
{ 
    canvas = new Picture(length, height); 
    canvas.setAllPixelsToAColor(color); 
    g = canvas.getGraphics(); 
    g2 = (Graphics2D)g; 
    g2.setColor(color);  
} 

這應該是一個方法或構造?如果這是一種常規方法,則需要void,或者如果它是構造函數,則它應該與該類的名稱相同。

你也有很多代碼不在方法中。

0

DrawingGraph不是一個函數也不是一個構造函數,如果它是一個函數,它需要返回類型,如果它應該是一個構造函數,它必須與該類具有相同的名稱。

此外,您的課程的最後一部分getGraph()必須位於函數中,但直接位於課程正文中。 只需聲明一個新的函數,並將其移入它的一個調用它。

0

類名以lowerCase開頭。 你確定你沒有交換「lowerCase」和「DrawingGraph」嗎?