gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 07:18:24 25 4
gpt4 key购买 nike

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

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com