2016-10-16 43 views
-1

我得到以下錯誤:Getter/Setter方法錯誤。找不到符號

Line 13, cannot find symbol - System.out.println(getDirection(room1)); (with an arrow pointing to room1). Symbol: Variable room 1 location: class ArtRecord.

本質是什麼代碼試圖做的是,去的getDirection的方法。並獲取在setDirection中設置的room1的方向。

import java.util.Scanner; 
class ArtRecord 
{ 
    public static void main (String[] param) 
    { 
     Scanner reader = new Scanner(System.in); 

     createandprintsart(); 
     String location = askwhere(); 
     if (location == "Room 1"){ 
      System.out.println(getDirection(room1)); //#ERROR FOUND ON THIS LINE 
     } 
    } 

    public static void createandprintsart() 
    { 
     Art room1 = new Art(); 
     Art room2 = new Art(); 
     Art room3 = new Art(); 
     Art room4 = new Art(); 

     //System.out.println(getDirection(room2)); 
     room1 = setDirection(room1, "To your left is a painting by"); 
     room2 = setDirection(room2, "Ahead of you is a painting by"); 
    } 

    public static String askwhere() 
    { 
    Scanner reader = new Scanner(System.in); 
    System.out.println("What room are you in?"); 
    System.out.println("Room 1"); 
    System.out.println("Room 2"); 
    System.out.println("Room 3"); 
    System.out.println("Room 4"); 
    String locationvalue = reader.nextLine(); 
    return locationvalue; 
    } 

    public static void doStuff(Art room) 
    { 

    } 


    public static Art setDirection(Art room, String direction) 

    { 
     room.direction=direction; 
     return room; 
    }  

    public static String getDirection(Art room) 

    { 
     return room.direction; 
    } 

} 

class Art 
{ 
    String direction; 
    String artist; 
    String title; 
    int year; 
    int height; 
    int width; 

} 

由於

+0

@ c650請您詳細說明。 – Haque

+0

查看我的回答... – Charles

+0

@ c650它不會編譯,那麼'println'如何提供幫助? – ajb

回答

1

room1被定義爲createandprintsart()的局部變量。因此,它不能從您的main功能訪問。相反,你可以使房間static實例變量,也許?

此外,請注意,您不應該將字符串與==進行比較,而應該與String.equals(String str)進行比較。

+0

感謝您的回答。 – Haque

+0

@Haque將其標記爲答案,以便其他人可以看到。 – Charles

+0

感謝您的回答。我怎樣才能讓房間變成靜態實例變量? – Haque

0

一些事情。您正在嘗試訪問room1對象,該對象不在函數main()的範圍內。所以Art類對象必須在類作用域中聲明。其次,Art類對象也必須是靜態的才能從main函數進行訪問。

最後,您不能使用==運算符比較java中的字符串。你必須使用.equals,你也可以避免爲每個功能創建scanner對象,方法是將其移動到班級分數上。

class ArtRecord { 

    static Art room1; 
    static Art room2; 
    static Art room3; 
    static Art room4; 
    static Scanner reader = new Scanner(System.in); 

    public static void main(String[] param) { 
     String next = reader.next(); 

     createandprintsart(); 
     String location = askwhere(); 
     if (location.equals("Room 1")) { 
      System.out.println(getDirection(room1)); //#ERROR FOUND ON THIS LINE 
     } 
    } 

    public static void createandprintsart() { 
     room1 = new Art(); 
     room2 = new Art(); 
     room3 = new Art(); 
     room4 = new Art(); 

     //System.out.println(getDirection(room2)); 
     room1 = setDirection(room1, "To your left is a painting by"); 
     room2 = setDirection(room2, "Ahead of you is a painting by"); 
    } 
    ... 
}