我是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