2014-04-13 154 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()); 
}