我在學習構造函數。
當我嘗試編譯下面的代碼時,出現錯誤「變量輸入和形狀未初始化」。可變內部的Luse構造函數
有誰能告訴我爲什麼以及如何解決它?
public class Try {
public static void main(String[] args)
{
String input;//user key in the height and width
int shape;//triangle or square
Count gen = new Count(input , shape);//is this the right way to code?
gen.solve();
}
}
public class Count {
public Count(String inp, int shp) {
String input_value = inp;
shape_type = shp;
}
public void solve() {
if shape_type==3{
//count the triangle
}
else if shape_type==4{
//count the square
}
}
}
你必須初始化你的變量。 int形狀的情況下它並不重要它只是一個瘋狂的數字,但在輸入的情況下,它的一個對象,你必須初始化它,否則你程序會崩潰。你至少應該做input = null,但是由於它的一個字符串,你應該做input =「」;或者input = new String();或者放一些文字; – 2011-01-20 01:33:58