我需要创建一个面向对象的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所说的,即使在编译之后,这个代码也存在根本性的问题。 –