我有一个转换程序下面的代码:变量可能未被初始化?
private void convertButtonActionPerformed(java.awt.event.ActionEvent evt) {
int type, value;
double conversion;
String output;
type = Integer.parseInt(conversionchoiceInput.getText());
value = Integer.parseInt(valueInput.getText());
if (type == 1)
{
conversion = inchesToCentimetres(value);
output = value + " inches = " + Math.round(conversion) + " centimetres";
}
else if (type == 2)
{
}
else if (type == 3)
{
}
else if (type == 4)
{
}
outputLabel.setText(output);
}
它说:“可变输出可能尚未initiaized”时,我已经有了?
谢谢!
当你写'String output;'你是_declaring_'输出'。当你写'String output =“”;'(或任何值。“”仅仅是一个例子)时,你就是_initialising_。 – Tharwen
通常,分离变量的声明和初始化(类型,值)是一个坏主意。如果可能的话,在初始化时声明它们。只有必要时才分割它们(例如,在try/catch-block之前)。 –