我的编译器不会有它。 :(现在怎么办?我必须完全重写整个应用程序吗?
要查看我的编译器拒绝的行,请按 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;
}
}
}
你犯了两个错误:
-
如果
inputScale
既不是"C"、"F"也不是"K",则三个变量都没有初始化,因此会出现错误。您可以通过使用虚拟值 (double celsiusOutput = 0;
( 初始化它们来解决此问题。 -
您的错误仅在
celsiusOutput
显示,因为当您在if
语句后不添加任何大括号时,该if
只会影响下一个命令;在这种情况下,celsiusOutput = inputDegrees
您的第一个if
。无论如何,其他两行都将被执行。只需在您希望受if
语句影响的行周围添加大括号即可。