2015-07-12 348 views
1

我是java的初學者,我正在研究華氏和攝氏轉換器之間的問題。但是,如果有人選擇第一個選項,那麼當該代碼運行時,第二個選項將自動運行,如下所示。我究竟做錯了什麼?如何在java中結束if語句?

import java.util.Scanner; 

public class FahrenheittoCelsius { 

    public static void main(String[] args) { 
     Scanner scan = new Scanner(System.in); 
     System.out.println("If you would like to convert a temperature from Fahrenheit to Celcius, enter 1."); 
     System.out.println("If you would like to convert a temperature from Celcius to Fahrenheit, enter 2."); 
     int mode = scan.nextInt(); 
     if (mode == 1); 
     { 
      System.out.println("Enter the temperature in Fahrenheit"); 
      int Ftemp = scan.nextInt(); 
      int Cnewtemp = (Ftemp - 32) * 5/9; 
      System.out.println("The temperature in Celcius is " + Cnewtemp + " degrees."); 
     } 
     if (mode == 2); 
     { 
      System.out.println("Enter the temperature in Celcius"); 
      int Ctemp = scan.nextInt(); 
      int Fnewtemp = Ctemp * 9/5 + 32; 
      System.out.println("The temperature in Fahrenheit is " + Fnewtemp + " degrees."); 
     }  
    } 

} 
+4

問題用分號是很常見的。投票結束爲錯字。 – dasblinkenlight

回答

2

刪除if語句後面的分號。你在他們真正開始之前就結束了,所以所有的代碼都會運行。

在Java中,括號應緊跟在右括號之後。 (後該生產線還直接後計數)

代碼應該是這樣的:

import java.util.Scanner; 

public class FahrenheittoCelsius { 

    public static void main(String[] args) { 
     Scanner scan = new Scanner(System.in); 
     System.out.println("If you would like to convert a temperature from Fahrenheit to Celcius, enter 1."); 
     System.out.println("If you would like to convert a temperature from Celcius to Fahrenheit, enter 2."); 
     int mode = scan.nextInt(); 
     if (mode == 1) 
     { 
      System.out.println("Enter the temperature in Fahrenheit"); 
      int Ftemp = scan.nextInt(); 
      int Cnewtemp = (Ftemp - 32) * 5/9; 
      System.out.println("The temperature in Celcius is " + Cnewtemp + " degrees."); 
     } 
     if (mode == 2) 
     { 
      System.out.println("Enter the temperature in Celcius"); 
      int Ctemp = scan.nextInt(); 
      int Fnewtemp = Ctemp * 9/5 + 32; 
      System.out.println("The temperature in Fahrenheit is " + Fnewtemp + " degrees."); 
     }  
    } 

} 
+0

爲了給答案增加一點清晰度,你能否包含代碼的外觀? – sstan

+0

@sstan sure。沒問題。 – Cob

+0

感謝您的幫助。 – Human

-1

變化的if語句:

public class FahrenheittoCelsius { 

    public static void main(String[] args) { 
     Scanner scan = new Scanner(System.in); 
     System.out.println("If you would like to convert a temperature from Fahrenheit to Celcius, enter 1."); 
     System.out.println("If you would like to convert a temperature from Celcius to Fahrenheit, enter 2."); 
     int mode = scan.nextInt(); 
     if (mode == 1) 
     { 
      System.out.println("Enter the temperature in Fahrenheit"); 
      int Ftemp = scan.nextInt(); 
      int Cnewtemp = (Ftemp - 32) * 5/9; 
      System.out.println("The temperature in Celcius is " + Cnewtemp + " degrees."); 
     } 
     else if (mode == 2) 
     { 
      System.out.println("Enter the temperature in Celcius"); 
      int Ctemp = scan.nextInt(); 
      int Fnewtemp = Ctemp * 9/5 + 32; 
      System.out.println("The temperature in Fahrenheit is " + Fnewtemp + " degrees."); 
     }  
    } 

}