2014-04-13 109 views
-1

我需要创建一个面向对象的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()); 
     } 
    } 
} 
+0

问题是什么?这不会编译,原因很多,主要是变量在不被声明的情况下使用,以及不返回声明的方法。 –

+1

'c'== inData <=将一个字符与一个字符串进行比较。 –

+0

正如@MarcoAcierno所说的,即使在编译之后,这个代码也存在根本性的问题。 –

回答

1
if (inData.equals("c")) 
{ 
    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: " + c.getConvertedValue()); 
} 

您需要使用方法之前实际创建Celsius类的对象。 fahrenheit类同样做。话虽如此,请重新考虑您的班级设计。

还要注意字符串比较方式的变化。

if语句添加摄氏创建对象的
+0

谢谢!我在看到你的答案后修复了一些东西。 但我现在得到2错误,缺少返回语句和无法解决符号错误。我会在原帖中发布我的代码,如果你可以看看它,会很感激:) – user3529827

+0

在你的'if'子句中使用'equals'而不是'=='来进行字符串比较。 C - > F和F - > C的逻辑是相同的。改变。 – GoldRoger

+0

感谢您的回答!真的很感激它!但是,我仍然收到两个错误,FCconvert.java:59:')'预计和FCconvert.java:78:表达式的非法开始。我再次编辑我的主帖,如果你可以帮助:) – user3529827

0

如下:

if ('c' == 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 ('f' == inData) 
{ 
System.out.println("Enter a value."); 
inData = stdin.readLine (); 
f = Integer.parseInt (inData); 
Fahrenheit fahr = new Fahrenheit(); 
fahr.setvalue(f); 
System.out.println("The converted value is: " + fahr.getConvertedValue()); 
}