Java错误输出(华氏温度到摄氏温度转换程序)!

时间:2014-03-15 14:24:32

标签: java

我是初学者程序员

我的作业是 编写一个程序,使用公式将华氏度转换为摄氏度: 摄氏度=(5/9)(华氏度--32)

问题是无论我在输入中给出什么值,我总是得到相同的值-17.78。

这里有我的代码!!!

package com.temperatureconversion;

import java.util.Scanner;


public class TemperatureConversion {

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);

    double F = 0.0;    // Temperature in Fahrenheit
    double C = 0.0;          // Temperature in celsius
    C = 5.0 /9 * (F - 32);

    System.out.print("Enter temperature in fahrenheit: ");
    F = input.nextDouble();

    System.out.printf("The celsius value of %10.2f is %2.2f", F, C);




}

}

上述代码有什么问题?

2 个答案:

答案 0 :(得分:2)

您的F值始终相同0.0,因为您在计算后要求其值,因此您需要在计算之前移动F值。

double F = 0.0;    // Temperature in Fahrenheit
double C = 0.0;          // Temperature in celsius

//ASK for value.
System.out.print("Enter temperature in fahrenheit: ");
F = input.nextDouble();

// Do your calculations.
C = 5.0 /9 * (F - 32);

答案 1 :(得分:0)

试试这个,先问问然后再计算

public static void main(String [] args){

Scanner input = new Scanner(System.in);

double F = 0.0;    // Temperature in Fahrenheit
double C = 0.0;          // Temperature in celsius


System.out.print("Enter temperature in fahrenheit: ");
F = input.nextDouble();
C = 5.0 /9 * (F - 32);
System.out.printf("The celsius value of %10.2f is %2.2f", F, C);

}

相关问题