好了,所以我的任務我應該寫存儲溫度,用戶給出了與設定參數的檢查,看是否乙基/氧/水要麼凍結或沸騰類然後在結束時顯示哪些將在它們進入的溫度下凍結/沸騰。我已經完成了班級和測試人員的大部分工作,但我在代碼中收到了幾處錯誤。我不是要求任何人給我答案,但如果你能告訴我我做錯了什麼,我將不勝感激。這裏是我的類代碼:與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());
}
}
確定你的確幫助了我,但我無法在程序中使用StringBuilder方法,因爲我們還沒有學習它。如何在不使用字符串生成器的情況下返回它? – Andrew 2012-03-13 21:28:24
檢查更新的答案,反正不要在現實世界中做到這一點:) – 2012-03-13 21:44:36