2015-07-12 353 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."); 
     }  
    } 

}