我是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.");
}
}
}
问题用分号是很常见的。投票结束为错字。 – dasblinkenlight