我收到以下错误:
第13行,找不到符号--System.out.println(getDirection(room1)); (带箭头指向room1)。 符号:可变空间1 地点: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 :(得分:1)
room1
被定义为createandprintsart()
中的局部变量。因此,无法从main
函数访问它。相反,你可以制作房间static
实例变量吗?
另请注意,您不应将字符串与==
进行比较,而应与String.equals(String str)
进行比较。
答案 1 :(得分: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");
}
...
}