2012-11-30 11 views
1

저는 자바 언어에 접근 했으므로 대개 일반적인 오류가 발생합니다. 다음 중 하나가되어야합니다. 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); 
} 
} 

답변

3
Figure f; 

내부 main 방법으로 그림을 확장 할 수 있습니다. if/else 조건이 충족되지 않아 f가 할당되지 않은 상태로 남을 가능성이 있습니다. 아래처럼 뭔가

변경이 :

Figure f=null; 

모든 지역 변수는 사용하기 전에 할당 된 일부 값이 필요합니다.

이와 함께 f에 대한 작업을 수행하기 전에 널 확인을 수행해야합니다.

+0

감사합니다. 생각했던 것처럼 매우 사소했습니다! – Marco

0

Figure f;

이 초기화되지 않습니다.

관련 문제