2016-10-22 25 views
-2

我對這些東西都很陌生,我試圖將Celsius轉換爲jGRASP Java中的Fahrenheit。我使用的代碼附加在圖片中,錯誤也可以在其他圖片中看到。在將攝氏溫度轉換爲華氏度時找不到符號錯誤Java

thats the code i am using

錯誤消息

the following is the error

+1

[爲什麼可能我不SO問問題時上傳的代碼圖像](http://meta.stackoverflow.com/questions/285551/爲什麼我可能不會上傳圖像的代碼的時候提出問題) –

+1

請閱讀http://stackoverflow.com/help/how-to-ask並改革你的問題根據它。 –

+1

請發表相關的代碼,不要將其作爲圖片添加 – DarkBee

回答

0

有消息稱一切。你還沒有聲明F,因此編譯器找不到符號。使用它像

int F = 0; 

編輯之前聲明它:你可能是指與字符串文字"F"比較input。您必須聲明inputstring,讀string變量到它,然後使用if條款喜歡

if (input == "F") {//... 
0
if (input == F) 

在您提供的代碼,你永遠不聲明F.

通過評審您想要查看的用戶是否輸入了「F」的代碼,但您可以如此分配輸入變量:

int input = scan.nextInt(); 

這將是更好的做這樣的事情:

String input = scan.nextLine(); 

if(input.equals("F")){ 
// rest of code 
0

與您的代碼的問題是你告訴掃描器來讀取一個int數據和你期待一個文本或字符。使用scanner.next()將返回空格之前的字符串。然後你可以檢查它的價值。這是一個例子。

public static void main(String args[]) { 
     Scanner scanner = new Scanner(System.in); 
     String tempScale = ""; 
     System.out.print("Enter the current outside temperature: "); 
     double temps = scanner.nextDouble(); 

     System.out.println("Celsius or Farenheit (C or F): "); 
     String input = scanner.next(); 
     if ("F".equalsIgnoreCase(input)) { 
      temps = (temps-32) * 5/9.0; 
      tempScale = "Celsius."; 
     } else if ("C".equalsIgnoreCase(input)) { 
      temps = (temps * 9/5.0) + 32; 
      tempScale = "Farenheit."; 
     } 
     System.out.println("The answer = " + temps + " degrees " + tempScale); 
     scanner.close(); 
    } 

和一個例證:

enter image description here

相關問題