java - "Variable might not have been initialized"错误

标签 java if-statement io jvm switch-statement

我的编译器不会有它。 :( 现在怎么办?我必须完全重写整个应用程序吗?

要查看编译器拒绝的行,请执行 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 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;
        }
    }
}

关于java - "Variable might not have been initialized"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49450694/

相关文章:

java - 如果我将来添加功能,如何确保我的 "database"不会损坏

ms-access - VBA - "With Statements"嵌套在 "IF Statements"内

Python自动化

java - 使用 Java 7 API 逐字节读取文件

javascript - IE - 如何将客户端图像文件读取为base64

java - 在 Java 中将 JSON 字符串转换为文件

java - DI、Guice 和策略模式

java - JDBC 中的事务状态

if-statement - 一般编程 - else 或 else if 为清楚起见

database - 典型的 RDBMS 如何存储和定位 block ?