我有一個轉換程序下面的代碼:變量可能未被初始化?
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之前)。 –