2012-03-13 53 views
1

好了,所以我的任務我應該寫存儲溫度,用戶給出了與設定參數的檢查,看是否乙基/氧/水要麼凍結或沸騰類然後在結束時顯示哪些將在它們進入的溫度下凍結/沸騰。我已經完成了班級和測試人員的大部分工作,但我在代碼中收到了幾處錯誤。我不是要求任何人給我答案,但如果你能告訴我我做錯了什麼,我將不勝感激。這裏是我的類代碼:與Java類方法有麻煩

public class FreezingBoilingPoints { 

    private int temperature; 

    public FreezingBoilingPoints(int temp) { 
     temperature = temp; 
    } 

    public void setTemperature(int temp) { 
     temperature = temp; 
    } 

    public int getTemperature() { 
     return temperature; 
    } 

    private Boolean isEthylFreezing(int temperature) { 
     if (temperature <= -173) { 
      return true; 
     } else { 
      return false; 
     } 
    } 

    private Boolean isEthylBoiling(int temperature) { 
     if (temperature >= 172) { 
      return true; 
     } else { 
      return false; 
     } 
    } 

    private Boolean isOxygenFreezing(int temperature) { 
     if (temperature <= -362) { 
      return true; 
     } else { 
      return false; 
     } 
    } 

    private Boolean isOxygenBoiling(int temperature) { 
     if (temperature >= -306) { 
      return true; 
     } else { 
      return false; 
     } 
    } 

    private Boolean isWaterFreezing(int temperature) { 
     if (temperature <= 32) { 
      return true; 
     } else { 
      return false; 
     } 
    } 

    private Boolean isWaterBoiling(int temperature) { 
     if (temperature >= 212) { 
      return true; 
     } else { 
      return false; 
     } 
    } 

    public String showTempinfo() { 
     if (isEthylFreezing()) { 
      System.out.println("Ethyl will freeze"); 
     } 

     if (isEthylBoiling()) { 
      System.out.println("Etheyl will boil"); 
     } 

     if (isOxygenFreezing()) { 
      System.out.println("Oxygen will freeze"); 
     } 

     if (isOxygenBoiling()) { 
      System.out.println("Oxygen will Boil"); 
     } 

     if (isWaterFreezing()) { 
      System.out.println("Water will freeze"); 
     } 

     if (isWaterBoiling()) { 
      System.out.println("Water will boil"); 
     } 
    } 
} 

和我的測試代碼如下:

import java.util.Scanner; 

public class FreezingBoilingTester { 
    public static void main(String[] args) { 
     int temperature; 

     FreezingBoilingPoints temp1 = new FreezingBoilingPoints(0); 

     Scanner scan = new Scanner(System.in); 
     System.out.println("Please enter a temperature: "); 
     temperature = scan.nextInt(); 

     System.out.println(showTempinfo()); 
    } 
} 

回答

1

1)不要傳遞temp中的方法,因爲你已經在成員變量中有這個值。 2)你可以改變if(condition)then true else false into return(condition),它會是相同的結果,只是爲了便於閱讀。 3)你應該返回boolean而不是布爾包裝,直到你需要包裝器。

public final class FreezingBoilingPoints { 

    private int temperature; 

    public FreezingBoilingPoints(int temp) { 
     temperature = temp; 
    } 

    public void setTemperature(int temp) { 
     temperature = temp; 
    } 

    public int getTemperature() { 
     return temperature; 
    } 

    private boolean isEthylFreezing() { 
     return (temperature <= -173); 
    } 

    private boolean isEthylBoiling() { 
     return (temperature >= 172); 
    } 

    private boolean isOxygenFreezing() { 
     return (temperature <= -362); 
    } 

    private boolean isOxygenBoiling() { 
     return (temperature >= -306); 
    } 

    private boolean isWaterFreezing() { 
     return (temperature <= 32) ; 
    } 

    private boolean isWaterBoiling() { 
     return (temperature >= 212); 
    } 

    public String showTempinfo() { 
     StringBuilder result = new StringBuilder(); 

     if (isEthylFreezing()) { 
      result.append("Ethyl will freeze"); 
      result.append("\n"); 
     } 

     if (isEthylBoiling()) { 
      result.append("Etheyl will boil"); 
      result.append("\n"); 
     } 

     if (isOxygenFreezing()) { 
      result.append("Oxygen will freeze"); 
      result.append("\n"); 
     } 

     if (isOxygenBoiling()) { 
      result.append("Oxygen will Boil"); 
      result.append("\n"); 
     } 

     if (isWaterFreezing()) { 
      result.append("Water will freeze"); 
      result.append("\n"); 
     } 

     if (isWaterBoiling()) { 
      result.append("Water will boil"); 
      result.append("\n"); 
     } 

     return result.toString(); 
    } 
} 

主營:

import java.util.Scanner; 

public class FreezingBoilingTester 
{ 
    public static void main(String[] args) 
    { 

    Scanner scan = new Scanner(System.in); 
    System.out.println("Please enter a temperature: "); 
    int temperature = scan.nextInt(); 

    FreezingBoilingPoints temp1 = new FreezingBoilingPoints(temperature); 
    System.out.println(temp1.showTempinfo()); 
    } 
} 

更新: 您可以使用字符串連接:

String result = ""; 

if (condition) { 
    result += "new result"; 
    result += "\n"; 
} 

但這不是在性能方面推薦的,因爲每個+ =操作在保存新結果的內存中創建另一個String對象。

+0

確定你的確幫助了我,但我無法在程序中使用StringBuilder方法,因爲我們還沒有學習它。如何在不使用字符串生成器的情況下返回它? – Andrew 2012-03-13 21:28:24

+0

檢查更新的答案,反正不要在現實世界中做到這一點:) – 2012-03-13 21:44:36

0

的問題是,你的私有方法正在以temperature,但,你是不是路過一人爲您的showTempinfo()方法。嘗試刪除輸入參數並使用該類中設置的temp。此外,在撥打showTempinfo()之前,您需要設置temp

希望這會有所幫助。

0

在你showTempinfo(),你嘗試做isEthylFreezing()

但它不能正常工作... isEthylFreezing正在等待一個int ...但它得到什麼......

+0

公共字符串showTempinfo()它說,「表達式的非法開始」,它需要一個分號。這些是我現在得到的唯一的錯誤 – Andrew 2012-03-13 21:01:18

0

你沒有通過輸入用戶是給你到構造函數你的FreezingBoilingPoints類。您正在使用0初始化該類,然後向用戶請求溫度。用戶提供的溫度與用於測試的類別之間沒有任何關係。

0

你需要建立你的FreezingBoilingPoints在你的主要方法對象,然後在其上調用showTempinfo()。另外,你的私有calc方法應該使用成員變量;沒有必要把它作爲參數。

0

您需要用戶輸入,temperature,傳遞到你的FreezingBoilingPoints構造。另外,方法showTempInfo()是特定於實例的。例如,你需要通過將用戶輸入與構造函數來實例對象,temp1,然後調用temp1.showTempInfo()

0

這裏,我們去:

1)所有你的「是......」法正期待一個int參數,但是當你調用它們時,你沒有傳遞任何東西。從方法實現或方法調用中刪除int參數

2)缺少方法isWaterBoiling的右括號;

3)您將方法「showTempinfo」標記爲返回String,但您沒有爲該方法返回任何內容。可以添加返回命令或從方法簽名中刪除「字符串」。