2014-11-25 76 views
1

我是一名新程序員,我在我的ShapeApp類中找到錯誤找不到符號。我的錯誤是在changeRecL方法中找不到符號但在主方法中聲明

System.out.println(「矩形的當前長度是:」+ r1.getLength()); ^ 符號:變量R1 位置:類ShapeApp

請嘗試和更簡單的方式解釋。非常感謝,我的代碼如下。

public class Rectangle 
{ 
protected double length; 
protected double width; 

public Rectangle() { } 
public Rectangle(double l,double w) 
{ 
    length = l; 
    width = w; 
} 
public void setLength(double l) {length = l;} 
public double getLength() {return length;} 

public void setWidth(double w) {width = w;} 
public double getWidth() {return width;} 

public double findArea() {return length * width;} 

public String toString() 
{ 
    return "\tLength " + length + "\tWidth " + width; 
} 
} 



public class Box extends Rectangle 
{ 
private double height; 

public Box() { } 
public Box(double l,double w,double h) 
{ 
    super(l,w); 
    height = h; 
} 
public void setHeight(double h) {height = h;} 
public double getHeight() {return height;} 

public double findArea() {return ((super.findArea() * 2) + (2 * height * width) + (2 * height * length));} 
public double findVolume() {return super.findArea() * height;} 

public String toString() 
{ 
return super.toString() + "\tHeight " + height; 
} 
} 





import java.util.*; 

public class ShapeApp 
{ 
    public static void main(String[] args) 
    { 
    Rectangle r1 = new Rectangle(20,10); 
    Box b1 = new Box(10,5,5); 

    int options; 

    Scanner input = new Scanner(System.in); 

    do{ 
     displayMenu(); 
     options = input.nextInt(); 

      switch(options) 
      { 
      case 1: changeRecL(); 
        break; 
      case 2: changeBoxL(); 
        break; 
      case 3: changeBoxH(); 
        break; 
      case 4: displayAreaRec(); 
        break; 
      case 5: displaySaBox(); 
        break; 
      case 6: displayVoBox(); 
        break;    
      case 0: System.out.println("Exiting Program"); 
        break; 
      default: System.out.println("Invalid Option. "); 
      } 
     }while(options != 0);  
    } 

    public static void displayMenu() 
    { 
    System.out.println("-------------------------------MENU-------------------------------"); 
    System.out.println("[1] Change the length of rectangle"); 
    System.out.println("[2] Change the length of box"); 
    System.out.println("[3] Change the height of box"); 
    System.out.println("[4] Display the area of rectangle"); 
    System.out.println("[5] Display the surface area of box"); 
    System.out.println("[6] Display the volume of box"); 
    System.out.println("[0] Exit"); 
    System.out.println("------------------------------------------------------------------"); 
    System.out.println("Enter your option:"); 
    } 

    public static void changeRecL() 
    { 
    Scanner input = new Scanner(System.in); 

    System.out.println("Current length of rectangle is: " + r1.getLength()); 
    System.out.println("Enter new length of rectangle: "); 

    double nlength = input.nextDouble(); 

    r1.setLength(nlength); 
    } 

    public static void changeBoxL() 
    { 

    } 

    public static void changeBoxH() 
    { 

    } 

    public static void displayAreaRec() 
    { 

    } 

    public static void displaySaBox() 
    { 

    } 

    public static void displayVoBox() 
    { 

    } 

} 
+0

您的標題說明了一切。在方法外部聲明的變量在該方法外部是不可見的。如果您希望它可見,請將其設爲類成員或將其作爲參數傳遞。 – EJP 2014-11-25 08:36:57

回答

1

這是因爲您還沒有在您的方法changeRecL中定義r1。

也許你想傳遞的是R1從主到你的方法如下圖所示:

case 1: changeRecL(r1); 

並接受R1爲下文相同:

public static void changeRecL(Rectangle r1) 
+0

感謝您的幫助,我設法編譯它。在我決定在這裏發佈之前,花了我2​​個小時!我真的很感激你的快速反應!:) – Brandon 2014-11-25 08:28:43

+0

你永遠是受歡迎的。這就是爲什麼我們有這個論壇:)如果你認爲這有幫助,請接受答案並關閉這個問題。希望有一天對別人有幫助。 – SMA 2014-11-25 08:30:54

0

當你定義一個對象類,你可以將該類的幾個實例作爲對象。不僅有一個Rectangle r1。如果你只想使用一個矩形來實現一個Singleton類。

public class Rectangle { 
    double len; 
    double wid; 
    private static Rectangle instance = null; 
    protected Rectangle() { 
     // Exists only to defeat instantiation. 
    } 
    public static Rectangle getInstance() { 
     if(instance == null) { 
     instance = new Rectangle(); 
     } 
     return instance; 
    } 
public static void setLen (double l){ 
     len = l; 
} 
public static double getLen(){ 
     return len; 
    } 
public static void setWid (double w){ 
     wid = w; 
    } 
public static double getWid(){ 
     return wid; 
    } 
} 

但是,就像@almas_shaikh所說的那樣,將對象實例傳遞給方法更容易。

另外,讓我提醒你,單詞length被java用來確定數組和其他對象的大小,如getLength()方法。您應該使用其他名稱來指定長度屬性和方法以避免衝突。

+0

我不明白實例== null部分。也許這對我來說太複雜了。 – Brandon 2014-11-25 11:12:31

+0

這是'Singleton Pattern'。您可能會在Google中找到很多信息。 – 2014-11-25 13:47:28

+0

當你創建一個類時,它通常被用來讓你的程序中生成的對象是唯一的。如果是這樣,從每個方法你有權訪問該對象 – 2014-11-25 14:01:58