2016-02-16 261 views
0

該程序假設將轉換輸入溫度。從華氏溫度到攝氏溫度。因此,如果輸入是212F,那麼它應該顯示:Java,將華氏溫度轉換爲攝氏溫度

212 deg. F = 100 deg. C 

除了當我運行該程序的輸出始終爲「0攝氏度。」。有人能告訴我我在這裏做錯了嗎?我猜如果有什麼需要做的:

double fahrenheit = keyboard.nextDouble(); 

但即使如此,我不太清楚爲什麼。謝謝你的幫助!

import java.util.Scanner;  //keyboard input 
import java.text.DecimalFormat; //formatting 

public class FahrenheitToCelsius { 

    public static void main(String[] args) { 

     Scanner keyboard = new Scanner(System.in); 

     //User Input 
     System.out.println("Please enter temperature (Fahrenheit): "); 
     double fahrenheit = keyboard.nextDouble(); 

     //Calculations 
     double celsius = (5/9) * (fahrenheit - 32); 

     //Formatting 
     DecimalFormat myFormatter = new DecimalFormat("#,###.##"); 

     //Output 
     System.out.println("\n" + myFormatter.format(fahrenheit) 
     + " deg. F = " + myFormatter.format(celsius) + " deg. C"); 

    } 

} 
+0

'(5/9)'使這個'(5.0/9)' – MartinS

回答

1

導致整數除法的一個問題

嘗試

double celsius = (5.0/9.0) * (fahrenheit - 32.0); 
相關問題