Java温度转换器使用决赛

时间:2018-03-24 11:22:40

标签: java

我是java的新手,我开始编写一个必须包含最终变量的温度转换器。

正如您将看到的,我正在使用公式来计算F,C和K之间的转换。这些公式是最终的,它们包含“度”变量,例如 - final double F_TO_C = Math.round((5/9.0)*(degree - 32) * 100.0) / 100.0;

度变量将取自用户输入。我的问题是,如果有任何方法可以使用这个最终公式而不使用学位变量,因为我的老师不允许使用包含变量的final。

这是程序 -

    /* This program is a temperature converter, based on user input (scale and degree), 
it converts to Celcius, Farenheit and Kelvin. */

import java.util.Scanner;
public class test

{
    public static void main (String[]args)
    {

        Scanner scan=new Scanner(System.in);
        char tempSymbol;
        double degree;

        System.out.println("Please enter a scale: ");
        String scale = scan.next();
        tempSymbol = scale.charAt(0); // Get the first char to know the scale (c/k/f)   

        System.out.println("Please enter the degree"); // Get the degree from user
        degree = scan.nextInt();

        // declaring converting formulas as finals and rounding them 
        final double F_TO_C = Math.round((5/9.0)*(degree - 32) * 100.0) / 100.0; 
        final double F_TO_K = Math.round((degree + 459.67) / 1.8 * 100.0) / 100.0;
        final double C_TO_F = Math.round(((9/5.0) * degree + 32) * 100.0) / 100.0;
        final double C_TO_K = Math.round((degree + 273.15) * 100.0) / 100.0;
        final double K_TO_F = Math.round(((9/5.0) * (degree - 273.15) + 32) * 100.0) / 100.0;
        final double K_TO_C = Math.round((degree - 273) * 100.0) / 100.0;

        switch (tempSymbol) { 
            case 'F': 
            case 'f': System.out.println("C " + F_TO_C);
                      System.out.println("F " + degree);
                      System.out.println("K " + F_TO_K);
                      break;
            case 'C':
            case 'c': System.out.println("C " + degree);
                      System.out.println("F " + C_TO_F);
                      System.out.println("K " + C_TO_K);  
                      break;
            case 'K': 
            case 'k': System.out.println("C " + K_TO_C);
                      System.out.println("F " + K_TO_F);
                      System.out.println("K " + degree);
                      break;
            default: System.out.println("No such temperature");
                     break;
        }
    }

由于

2 个答案:

答案 0 :(得分:0)

您可以将所有内容声明为未重新分配的final。例如,您可以这样做:

//declaring constants
final static Char F = 'F'
final static Char C = 'C'
final static Char K = 'K'

public static void main (String[]args)
{

    final Scanner scan=new Scanner(System.in);
    System.out.println("Please enter a scale: ");
    final String scale = scan.next();
    final char tempSymbol = scale.charAt(0); // Get the first char to know the scale (c/k/f)   

    System.out.println("Please enter the degree"); // Get the degree from user
    final double degree = scan.nextInt(); //you should think about actually allowing a double here -> nextDouble()

 ...

    switch (Character.toUpperCase(tempSymbol)) { 
        case F:  System.out.println(C + " " + F_TO_C(degree));
                 System.out.println(F + " " + degree);
                 System.out.println(K + " " + F_TO_K);
                 break;
        ...
    }
}

最后你还可以使用这样的最终参数声明一个final函数(我添加了静态以获得良好的度量,对转换公式有意义):

final static double F_TO_C(final double degrees){
    //consider decaring all constants i.e. 5, 9.0, 32 and 100 like F,C and K
    return Math.round((5/9.0)*(degrees - 32) * 100.0) / 100.0;
}

答案 1 :(得分:0)

公式本身并不是最终的,因为它们不是"值"。我认为你误解了这些要求。你的要求可能只是做leoderprofi在答案中所做的事。

然而,Java确实有能力制作"功能"变量,具有功能接口。假设您没有误解要求,这就是您所需要的:

// These are DoubleUnaryOperators that "stores" how to convert from one scale to another
final DoubleUnaryOperator F_TO_C = d -> Math.round((5/9.0)*(d - 32) * 100.0) / 100.0;
final DoubleUnaryOperator F_TO_K = d -> Math.round((d + 459.67) / 1.8 * 100.0) / 100.0;
final DoubleUnaryOperator C_TO_F = d -> Math.round(((9/5.0) * d + 32) * 100.0) / 100.0;
final DoubleUnaryOperator C_TO_K = d -> Math.round((d + 273.15) * 100.0) / 100.0;
final DoubleUnaryOperator K_TO_F = d -> Math.round(((9/5.0) * (d - 273.15) + 32) * 100.0) / 100.0;
final DoubleUnaryOperator K_TO_C = d -> Math.round((d - 273) * 100.0) / 100.0;

switch (tempSymbol) {
    case 'F':
    case 'f': 
        // Here you pass "degree" into the function to calculate the temperature in the corresponding scale.
        System.out.println("C " + F_TO_C.applyAsDouble(degree));
        System.out.println("F " + degree);
        System.out.println("K " + F_TO_K.applyAsDouble(degree));
        break;
    case 'C':
    case 'c': System.out.println("C " + degree);
        System.out.println("F " + C_TO_F.applyAsDouble(degree));
        System.out.println("K " + C_TO_K.applyAsDouble(degree));
        break;
    case 'K':
    case 'k': System.out.println("C " + K_TO_C.applyAsDouble(degree));
        System.out.println("F " + K_TO_F.applyAsDouble(degree));
        System.out.println("K " + degree);
        break;
    default: System.out.println("No such temperature");
        break;
}