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