我已经研究了很多关于当前问题的答案,但我不确定出了什么问题:
import java.util.Map;
import java.util.HashMap;
public class Items
{
public static void main (String args[])
{
HashMap<String, Double> Hallway = new HashMap<String, Double>();
HashMap<String, Double> Toilet = new HashMap<String, Double>();
HashMap<String, Double> ChemistryLab = new HashMap<String, Double>();
HashMap<String, Double> Outdoors = new HashMap<String, Double>();
HashMap<String, Double> Library = new HashMap<String, Double>();
HashMap<String, Double> Engineering = new HashMap<String, Double>();
HashMap<String, Double> Cafeteria = new HashMap<String, Double>();
HashMap<String, Double> ComputerLab = new HashMap<String, Double>();
HashMap<String, Double> LectureTheater = new HashMap<String, Double>();
HashMap<String, Double> MedicalCentre = new HashMap<String, Double>();
}
public HashMap<String, Double> getHallwayItems()
{
return Hallway;
}
public HashMap<String, Double> getToiletItems()
{
return Toilet;
}
public HashMap<String, Double> getChemistryLabItems()
{
return ChemistryLab;
}
public HashMap<String, Double> getOutdoorItems()
{
return Outdoors;
}
public HashMap<String, Double> getLibraryItems()
{
return Library;
}
public HashMap<String, Double> getEngineeringItems()
{
return Engineering;
}
public HashMap<String, Double> getCafeteriaItems()
{
return Cafeteria;
}
public HashMap<String, Double> getComputerLabItems()
{
return ComputerLab;
}
public HashMap<String, Double> getLectureTheaterItems()
{
return LectureTheater;
}
public HashMap<String, Double> getMedicalCentreItems()
{
return MedicalCentre;
}
}
它说我在尝试编译时找不到变量Hallway,但是我看不出这是如何解决的。 感谢您提供的任何帮助。
答案 0 :(得分:1)
您需要将Hallway和其他人定义为实例变量。变量在方法范围内定义,在其他方法中无法访问。
import java.util.Map;
import java.util.HashMap;
public class Items
{
HashMap<String, Double> Hallway = new HashMap<String, Double>();
HashMap<String, Double> Toilet = new HashMap<String, Double>();
HashMap<String, Double> ChemistryLab = new HashMap<String, Double>();
HashMap<String, Double> Outdoors = new HashMap<String, Double>();
HashMap<String, Double> Library = new HashMap<String, Double>();
HashMap<String, Double> Engineering = new HashMap<String, Double>();
HashMap<String, Double> Cafeteria = new HashMap<String, Double>();
HashMap<String, Double> ComputerLab = new HashMap<String, Double>();
HashMap<String, Double> LectureTheater = new HashMap<String, Double>();
HashMap<String, Double> MedicalCentre = new HashMap<String, Double>();
public static void main (String args[])
{
}
public HashMap<String, Double> getHallwayItems()
{
return Hallway;
}
public HashMap<String, Double> getToiletItems()
{
return Toilet;
}
public HashMap<String, Double> getChemistryLabItems()
{
return ChemistryLab;
}
public HashMap<String, Double> getOutdoorItems()
{
return Outdoors;
}
public HashMap<String, Double> getLibraryItems()
{
return Library;
}
public HashMap<String, Double> getEngineeringItems()
{
return Engineering;
}
public HashMap<String, Double> getCafeteriaItems()
{
return Cafeteria;
}
public HashMap<String, Double> getComputerLabItems()
{
return ComputerLab;
}
public HashMap<String, Double> getLectureTheaterItems()
{
return LectureTheater;
}
public HashMap<String, Double> getMedicalCentreItems()
{
return MedicalCentre;
}
}
答案 1 :(得分:1)
Hallway
是main函数中的局部变量。而且主要是静态功能。在main
内声明的这些变量不能从其他函数访问。
有两种选择,
1)将所有地图声明为对象的静态成员,并具有静态getHallway()
静态getToilet
等。
2)我个人会建议这个,
import java.util.Map; import java.util.HashMap;
public class Items
{
private HashMap<String, Double> Hallway = new HashMap<String, Double>();
private HashMap<String, Double> Toilet = new HashMap<String, Double>();
...
public HashMap<String, Double> getHallwayItems()
{
return Hallway;
}
...
...
public static void main (String args[])
{
Items myItem = new Items();
myItem.getHallwayItems(); // and do whatever you want.
}
}
这样,我们也开发了OOPS ...