我需要創建一個面向對象的C-F轉換器程序。我需要有2種方法將c轉換爲f,將f轉換爲c。該方法應該有一個返回類型和一個參數。這是我所擁有的,並且在編譯時遇到一些錯誤。任何人都可以幫我找出我做錯了什麼嗎?將不勝感激:)。JAVA需要OOP幫助
import java.io.*;
class Celsius {
private int value, convertedValue;
int setValue(int c) {
value = c;
convertedValue = (int) ((9.0/5.0) * value + 32);
}
int getValue() {
return value;
}
int getConvertedValue() {
return convertedValue;
}
}
class Fahrenheit {
private int value, convertedValue;
int setValue(int f) {
value = f;
convertedValue = (int) ((9.0/5.0) * value - 32);
}
int getValue() {
return value;
}
int getConvertedValue() {
return convertedValue;
}
}
class FCconvert {
public static void main(String [] args) throws IOException {
InputStreamReader inStream = new InputStreamReader(System.in);
BufferedReader stdin = new BufferedReader(inStream);
String inData, choice1, choice2;
int input, c, f;
choice1 = "cel";
choice2 = "fah";
System.out.println("Please choose Celsius or Fahrenheit (c for celcius and f for fahrenheit)");
inData = stdin.readLine();
if (/*choice1 equals inData*/) {
System.out.println("Enter a value.");
inData = stdin.readLine();
c = Integer.parseInt(inData);
Celsius cel = new Celsius();
cel.setValue(c);
System.out.println("The converted value is: " + cel.getConvertedValue());
}
if (/*choice2 equals inData*/) {
System.out.println("Enter a value.");
inData = stdin.readLine();
f = Integer.parseInt(inData);
Fahrenheit fah = new Fahrenheit();
fah.setvalue(f);
System.out.println("The converted value is: " + fah.getConvertedValue());
}
}
}
問題是什麼?這不會編譯,原因很多,主要是變量在不被聲明的情況下使用,以及不返回聲明的方法。 –
'c'== inData <=將一個字符與一個字符串進行比較。 –
正如@MarcoAcierno所說的,即使在編譯之後,這個代碼也存在根本性的問題。 –