2013-04-13 157 views
0

我對Java相對來說比較新,而且我決定製作這個程序,因爲開始學習如何玩方法似乎是一個足夠簡單的任務。無論如何,這是我的程序,因爲它現在。我收到錯誤錯誤:'.class'預計

import javax.swing.JOptionPane; 
    public class smallAverage{ 
    public smallAverage getsmall() { 

      String sd1 = JOptionPane.showInputDialog("What is the first number?"); 
    double sdx = Double.parseDouble (sd1); 
      String sd2 = JOptionPane.showInputDialog("What is the second number?"); 
    double sdy = Double.parseDouble (sd2); 
      String sd3 = JOptionPane.showInputDialog("What is the third number?"); 
    double sdz = Double.parseDouble (sd3); 

      return double sdx; 
    return double sdy; 
    return double sdz; 
    } 

    public smallAverage getavg() { 

      String ad1 = JOptionPane.showInputDialog("What is the first number"); 
    double adx = Double.parseDouble (ad1); 
      String ad2 = JOptionPane.showInputDialog("What is the second number"); 
    double ady = Double.parseDouble (ad2); 
      String ad3 = JOptionPane.showInputDialog("What is the third number"); 
    double adz = Double.parseDouble (ad3); 


      return double adx; 
    return double ady; 
    return double adz; 
    } 

    public smallAverage work() { 
    double small = Math.min(sdx, sdy, sdz); 
    double avg = (adx + ady + adz)/3; 

      System.out.println("The smallest of "+ sdx+ sdy+ sdz+ " is " + small + ", and the average of"+ adx+ ady+ adz+ " is "+ avg+ "."); 
    } 
    } 

在這裏進行的實際數學對我來說確實沒有意義,它代表我想學習的代碼的工作方式。我曾嘗試以許多不同的方式重寫此代碼,但這似乎是最接近正確的。 我只是不知道爲什麼它不會工作。 當我嘗試通過終端來編譯我得到這個:

smallAverage.java:12: error: '.class' expected 
        return double sdx; 
           ^
    smallAverage.java:13: error: '.class' expected 
      return double sdy; 
         ^
    smallAverage.java:14: error: '.class' expected 
      return double sdz; 
         ^
    smallAverage.java:27: error: '.class' expected 
        return double adx; 
           ^
    smallAverage.java:28: error: '.class' expected 
      return double ady; 
         ^
    smallAverage.java:29: error: '.class' expected 
      return double adz; 
         ^
    6 errors 

任何幫助,將不勝感激

+0

您的代碼在很多方面都是錯誤的,包括您定義的方法會返回smallAverage對象。重新閱讀關於如何創建方法的章節。 –

回答

3

當您返回時,你不需要再類型說明:

return double adx; 
return double ady; 
return double adz; 

應該是

return adx; 
return ady; 
return adz; 

同樣的錯誤其他三個。

同時。

public smallAverage getsmall() 

錯誤,因爲您沒有從該函數返回類對象。您不能將一個函數中的3個return語句放在一起,而沒有任何不同的數據路徑。例如:

return adx; 
return ady; //two return statements below will never be reached. 
return adz; 

這些都是根本性的錯誤,你可能需要查看一些基本的Java教程:Java Tutorial

+0

@TiredAndWorking歡迎您。 – taocp

+0

感謝您的幫助。我遵循鏈接,開始了我的正確道路。 如果任何人從搜索引擎中發現此內容,請點擊鏈接。 [如果你需要提示,這裏是3; 1:)我重新編寫了整個代碼以使其工作。 2 :)你必須提供一些指導參考方法。 3 :)注意你的返回類型。] – TiredAndWorking

0

你只需要做即

return sdx; 

,因爲類型不要求你在聲明sdx時已經定義了。

建議使用Eclipse之類的IDE,因爲它指向編譯錯誤,甚至不運行程序。它對開發非常有幫助