gpt4 book ai didi

java - 尝试返回值时出错。

转载 作者:太空宇宙 更新时间:2023-11-04 06:29:23 24 4
gpt4 key购买 nike

我的作业被困在最后一部分了。我必须返回一个 Agent 值,但由于某种原因,我不断收到一条错误消息,指出“此方法必须返回 Agent 类型”,即使我返回的是 Agent。任何帮助将不胜感激。

import java.io.File;
import java.util.HashMap;

import jeff.ini.Ini;

public class ConfigLoader
{
private Ini _ini;
private HashMap<String, Space> _spaces = new HashMap<String, Space>();
private HashMap<String, Portal> _portals = new HashMap<String, Portal>();
private HashMap<String, Agent> _agents = new HashMap<String, Agent>();
public ConfigLoader(File iniFile)
{
_ini = new Ini(iniFile);
}
public Agent buildAll()
{
_buildSpaces();
_buildPortals();
_buildExits();
_buildDestinations();
_buildAgents();
return _selectStartAgent();

}
private void _buildSpaces(){
for(String spaceName : _ini.keys("spaces")){
String descrip= _ini.get("spaces", spaceName);
String image= _ini.get("images", "images");
Space space1= new Space(spaceName, descrip, image, null);
_spaces.put(spaceName, space1);
}
}
private void _buildPortals(){
for(String portalName : _ini.keys("portals")){
String descrip= _ini.get("portal", portalName);
Portal portal1=new Portal(portalName, descrip, null);
_portals.put(portalName, portal1);
}

}
private void _buildExits(){
for(String spaceName : _ini.keys("exits")){
String spaceExit = _ini.get("exits", spaceName);
Space space = _spaces.get(spaceName);
Portal exit = _portals.get(spaceExit);
space.setPortal(exit);

}
}
private void _buildDestinations(){
for(String portalName : _ini.keys("destinations")){
String destination = _ini.get("destinations", portalName);
Space des = _spaces.get(destination);

if(des == null){
System.out.print("ERROR");
System.exit(1);
}
else{

Portal portal = _portals.get(portalName);
portal.setDestination(des);

}

}
}

private void _buildAgents(){
for(String agentName : _ini.keys("agents")){
String agent = _ini.get("agents" , agentName);
Space space = _spaces.get(agent);

if(space == null){
System.out.print("ERROR");
System.exit(1);
}
else{
Agent a = new Agent(space, agentName);
_agents.put(agentName, a);
}

}
}
private Agent _selectStartAgent(){

for(String agentName : _ini.keys("start")){
String agent = _ini.get("start" , agentName);
Agent agent1 = _agents.get(agent);
if(agent == null){
System.out.print("ERROR");
System.exit(1);
}
else{
return agent1;
}


}


}

}

最佳答案

一个方法应该在所有不同的执行路径中返回一个值。您仅在 else block 中返回值,这意味着如果不执行 else block ,则不会返回该值,因此编译器会提示它。确保在所有不同的执行路径中返回一个值,当 if 不执行时,当 else 不执行时,当 for 循环本身不执行时。

关于java - 尝试返回值时出错。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26350161/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com