我的编译器没有它。 :(现在呢?我必须完全重写整个应用程序吗?

要查看编​​译器拒绝的行,请按Ctrl + F搜索System.out.println(celsiusOutput +“ C”);

尝试编译时,编译器告诉我,“变量celsiusOutput可能尚未初始化。”编译器对其他两个输出项中的任何一个都没有说相同的话:fahrenheitOutput和kelvinOutput。

/**
 * The Temperature class prints the conversion of an inputted temperature value
 * from one of the three temperature scales -- C, F or K -- to all three,
 * unless the input is illegal.
 */

import java.util.Scanner;
public class Temperature
{
    public static void main(String[] args)
    {
        // Declaration of output terms;
        double celsiusOutput;
        double fahrenheitOutput;
        double kelvinOutput;

        // Printing request for input terms from the user + reception of said terms
        Scanner scan = new Scanner(System.in);
        System.out.println("Welcome to the temperature scale converter.");
        System.out.println("Please enter your input temperature scale and degree value in the following format:");
        System.out.println("\"A n∈ℝ\", where A is the first letter of your scale (C, F or K) and n∈ℝ is the degrees.");
        String input = scan.next().toUpperCase();
            char inputScale = input.charAt(0);
        double inputDegrees = scan.nextDouble();

        // Declaration of final terms, i.e. the conversion formulae:
        final double C_DEGREES_IN_F = (inputDegrees - 32.00) / 1.80;
            final double C_DEGREES_IN_K = inputDegrees + 273.15;
        final double F_DEGREES_IN_C = (inputDegrees - 32.00) / 1.80;
            final double F_DEGREES_IN_K = (inputDegrees + 459.67) / 1.80;
        final double K_DEGREES_IN_C = inputDegrees - 273.15;
            final double K_DEGREES_IN_F = (inputDegrees - 273.15) * 1.80 + 32.00;

        // Conditional assignment of output terms, as conditioned by the user's input terms
        if(inputScale == 'C')
            celsiusOutput = inputDegrees;
            fahrenheitOutput = F_DEGREES_IN_C;
            kelvinOutput = K_DEGREES_IN_C;
        if(inputScale == 'F')
            celsiusOutput = C_DEGREES_IN_F;
            fahrenheitOutput = inputDegrees;
            kelvinOutput = K_DEGREES_IN_F;
        if(inputScale == 'K')
            celsiusOutput = C_DEGREES_IN_K;
            fahrenheitOutput = F_DEGREES_IN_K;
            kelvinOutput = inputDegrees;

        // Printing of output terms + legality check
        switch(inputScale)
        {
            case 'C':
            case 'F':
            case 'K':
                System.out.println(celsiusOutput + " C");
                System.out.println(fahrenheitOutput + " F");
                System.out.println(kelvinOutput + " K");
                break;
            default:
                System.out.println("Illegal input.");
                break;
        }
    }
}

最佳答案

您需要初始化将双double celsiusOutput;更改为double celsiusOutput = 0;的变量。在if语句中也有括号,算法才能起作用。正确的是:

import java.util.Scanner;
public class Temperature
{
    public static void main(String[] args)
    {
        // Declaration of output terms;
        double celsiusOutput = 0;
        double fahrenheitOutput = 0;
        double kelvinOutput = 0;

        // Printing request for input terms from the user + reception of said terms
        Scanner scan = new Scanner(System.in);
        System.out.println("Welcome to the temperature scale converter.");
        System.out.println("Please enter your input temperature scale and degree value in the following format:");
        System.out.println("\"A n∈ℝ\", where A is the first letter of your scale (C, F or K) and n∈ℝ is the degrees.");
        String input = scan.next().toUpperCase();
        char inputScale = input.charAt(0);
        double inputDegrees = scan.nextDouble();

        // Declaration of final terms, i.e. the conversion formulae:
        final double C_DEGREES_IN_F = (inputDegrees - 32.00) / 1.80;
        final double C_DEGREES_IN_K = inputDegrees + 273.15;
        final double F_DEGREES_IN_C = (inputDegrees - 32.00) / 1.80;
        final double F_DEGREES_IN_K = (inputDegrees + 459.67) / 1.80;
        final double K_DEGREES_IN_C = inputDegrees - 273.15;
        final double K_DEGREES_IN_F = (inputDegrees - 273.15) * 1.80 + 32.00;

        // Conditional assignment of output terms, as conditioned by the user's input terms
        if(inputScale == 'C') {
            celsiusOutput = inputDegrees;
            fahrenheitOutput = F_DEGREES_IN_C;
            kelvinOutput = K_DEGREES_IN_C;
        }
        if(inputScale == 'F') {
            celsiusOutput = C_DEGREES_IN_F;
            fahrenheitOutput = inputDegrees;
            kelvinOutput = K_DEGREES_IN_F;
        }
        if(inputScale == 'K') {
            celsiusOutput = C_DEGREES_IN_K;
            fahrenheitOutput = F_DEGREES_IN_K;
            kelvinOutput = inputDegrees;
        }

        // Printing of output terms + legality check
        switch(inputScale)
        {
            case 'C':
            case 'F':
            case 'K':
                System.out.println(celsiusOutput + " C");
                System.out.println(fahrenheitOutput + " F");
                System.out.println(kelvinOutput + " K");
                break;
            default:
                System.out.println("Illegal input.");
                break;
        }
    }
}

07-26 08:40