2016-02-16 99 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); 
相关问题