我有这段代码,它接受用户输入的数字并将其放入最后的等式中。但是,我在编译时不断收到错误/异常;“错误:变量 convertToNumL 可能尚未初始化。” 我理解错误在说什么,但是,我不知道如何调试它。
这就是我所知道的,try/catch 块以某种方式阻止了变量 convertToNum(它是从 .readLine () 方法派生的)。谁能告诉我如何解决这个问题?
非常感激。
我的部分代码:
if (realNumber2)
{
//Question 4: Height
while (realNumber2 = true) {
System.out.println ("3) Please enter the height of the triangle face.");
try
{
stringTriangleHeight = myInput.readLine ();
convertToNumH = Double.parseDouble (stringTriangleHeight);
Integer.parseInt (stringTriangleHeight);
System.out.println ("Your triangle height is " + stringTriangleHeight + " " + Units + ".");
realNumber3 = true;
break;
}
catch (Exception e)
{
System.out.println ("That was either a smart remark, a negative number or jibberish.");
realNumber3 = true;
}
}
while (realNumber3) {
total1 = convertToNumL * convertToNumW; //base area, length x width
total2 = convertToNumL * convertToNumH / 2; //triangle area using sidelength 'length', base x height / 2
total3 = convertToNumW * convertToNumH / 2; //triangle area using sidelength 'width , base x height / 2