java - 变量可能尚未初始化(抽象类)

标签 java compiler-errors

我刚刚接触 Java 语言,所以我什至经常会犯一些平庸的错误。以下应该是其中之一。 Es1.java:93: 错误:变量 f 可能尚未初始化 查看代码之前需要知道的事情——我正在使用一些自定义类:ConsoleInputManager 和 ConsoleOutputManager 的工作方式与 Scanner 和 System.out 基本相似;图是一个抽象类;圆形、正方形和矩形扩展图

import prog.io.*;
//ConsoleInputManager, ConsoleOutputManager
import prog.utili.*;
//Figure, Circle, Squade, Rectangle
class Es1{
public static void main(String[] args){

//Variables
ConsoleInputManager in = new ConsoleInputManager();
ConsoleOutputManager out = new ConsoleOutputManager();
int com1,com2,com3;
com1 = com2 = com3 = 0;
double r,s,l,w;
r = s = l = w = 0.0;
Figure f;

do{
    //Menu
    out.println("1. Insert a new figure");
    out.println("2. Calculate the area of the current figure");
    out.println("3. Examine the current figure");
    out.println("4. Exit the program\n");

    //Input
    com1 = in.readInt("Command: ");

    //New figure
    if (com1 == 1){
        do{
            out.println("1. Circle");
            out.println("2. Square");
            out.println("3. Rectangle");
            out.println("4. Back to main menu\n");
            com2 = in.readInt("Command: ");
            //Circle
            if (com2 == 1){
                s = l = w = 0.0;
                do{
                    r = in.readDouble("Radius: ");
                } while (r <= 0);
            }
            else
            //Square
            if (com2 == 2){
                r = l = w = 0.0;
                do{
                    s = in.readDouble("Side: ");
                } while (s <= 0);
            }
            else
            //Rectangle
            if (com2 == 3){
                r = s = 0.0;
                do{
                    l = in.readDouble("Length: ");
                } while (l <= 0);
                do{
                    w = in.readDouble("Width: ");
                } while (w <= 0);
            }
        } while (com2 != 4);
    }
    else

    //Area
    if (com1 == 2){
        if (r != 0.0){
            f = new Circle(r);
            out.println("Area = " + f.getArea());
        }
        else
        if (l != 0.0){
            f = new Square(s);
            out.println("Area = " + f.getArea());
        }
        else
        if ( b != 0.0){
            f = new Rectangle(l,w);
            out.println("Area = " + f.getArea());
        }
        else
            out.println("It hasn't been inserted any figure");
    }
    else

    //Examination
    if (com1 == 3){
        do{
            out.println("1. Examine the current figure");
            out.println("2. Back to main menu");
            com3 = in.readInt("Command: ");
            if (com3 == 1){
                if (f instanceof Circle)
                    out.println("Circle");
                else
                if (f instanceof Square)
                    out.println("Square");
                else
                if (f instanceof Rectangle)
                    out.println("Rectangle");
                else
                    out.println("It hasn't been inserted any figure");
            }   
        } while (com3 != 2);
    }

} while (com1 != 4);
}
}

最佳答案

Figure f;

上面的语句在 main 方法中。有可能不满足 if/else 条件,导致 f 未分配。

将其更改为如下所示:

Figure f=null;

所有局部变量在使用前都需要赋值。

除此之外,确保在对 f 执行任何操作之前执行 null 检查。

关于java - 变量可能尚未初始化(抽象类),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13649112/

相关文章:

java - LinkedList Array 中每个索引的 LinkedList

javascript - Jscript语法错误-代码800A03EA-Microsoft JScript编译-锯齿供应链-Windows 10

C++类模板继承之谜

java - 编译并运行Java,具有捆绑的依赖项

c++ - 对vtable的 undefined reference -C++链接错误

32bit-64bit - 在 32 位模式下调试 java 应用程序

java - 按任意键继续不工作?

java - 使用来自网络的无监督爬行文本来训练 word2vec 是个好主意吗?

java - 使用 while 循环让用户输入多个 int 并找到最大值和最小值

c++ - 为什么当我做一个小的改变时,我会收到错误消息说事情没有被宣布?当他们是