我现在很迷茫。我有一个方法 .usePortal() 应该检查位置是否为空,如果不是,则将此人传送到下一个连接的位置。但是,我对调用什么对象来使传送部分发生感到困惑。
public void usePortal(){
if(agentLocation != null){
transport(this);
}
}
这是其他类
public class Space {
private String name;
private String description;
private Portal portal;
public String getName() {
return this.name;
}
public String getDescription() {
return this.description;
}
public Portal getPortal() {
return this.portal;
}
public void setName(String userName){
this.name = userName ;
}
public void setDescription(String userDescription){
this.description = userDescription;
}
public void setPortal(Portal portal){
this.portal = portal;
}
public String toString() {
return getName();
}
public String toStringLong() {
return this.name + ":" +" " + this.description + " with a " + portal.portalToStringLong();
}
}
class Portal {
private String portalName;
private String portalDirection;
private Space portalDestination;
public String getName(){
return this.portalName;
}
public String getDirection(){
return this.portalDirection;
}
public Space getDestination(){
return this.portalDestination;
}
public void setName(String portalName){
this.portalName = portalName;
}
public void setDirection(String portalDirection){
this.portalDirection = portalDirection;
}
public void setDestination(Space portalDestination){
this.portalDestination = portalDestination;
}
public String portalToString() {
return this.portalName + " that goes " + this.portalDirection;
}
public String portalToStringLong(){
return this.portalName + " that goes " + this.portalDirection + " to " + this.portalDestination;
}
public void transport(Agent inputAgent) {
inputAgent.setAgentLocation(this.portalDestination);
}
}
class Agent{
private Space agentLocation = null;
private String agentName;
public void setAgentLocation(Space classroom){
agentLocation = classroom;
}
public void setAgentName(String name){
agentName = name;
}
public Space getAgentLocation(){
return this.agentLocation;
}
public String getAgentName(){
return this.agentName;
}
public String agentToString(){
return this.agentName;
}
public String agentToStringLong(){
return this.agentName + " is in " + this.agentLocation;
}
public void usePortal(){
if(agentLocation != null){
transport(this);
}
}
class CommandInterpreter{
public static void run(Agent student){
boolean continueCommand = true;
while(continueCommand == true){
Scanner userInput = new Scanner(System.in);
System.out.println("==>");
String userCommand = userInput.next();
switch(userCommand){
case "help":
System.out.println("go, help, look, quit, where");
case "where":
student.getAgentLocation().toString();
case "look":
student.getAgentLocation().toStringLong();
//case "go":
//student.usePortal();
case "quit":
continueCommand = false;
}
}
}
最佳答案
it doesnt send me anything back/terminates randomly
在你的 switch 语句中没有中断,因此它只会提示用户一次并一直流到 quit
并退出循环。
switch(userCommand){
case "help":
System.out.println("go, help, look, quit, where");
break;
case "where":
student.getAgentLocation().toString();
break;
case "look":
student.getAgentLocation().toStringLong();
break;
case "quit":
continueCommand = false;
}
line 35, it says to return the location, description, and the portal. But after giving it a room with no portal, it gives an error and then terminates. Is there a way to use something similar to overload and use a return for when I dont have a portal available?
在你的 Space
类中:
您可以先检查门户是否存在:
public String toStringLong() {
String descPortal = "Empty portal";
if(portal != null)
descPortal = portal.portalToStringLong();
return this.name + ": " + this.description + " with a " + descPotal;
}
关于java - 在 if 语句中为方法调用正确的对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35886300/