java - 在 if 语句中为方法调用正确的对象?

标签 java

我现在很迷茫。我有一个方法 .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/

相关文章:

java - mapreduce 二次排序不起作用

java - 从 3.4.d 迁移扩展到 4.2.e(和 5.0.a)后,Alfresco 无法加载 spring 上下文

java - 快速的 Java 小玩意儿

java - 什么时候使用 JSP,什么时候使用 Servlet?

java - 如何将我的域名映射到 TOMCAT 服务器中的 ZK 应用程序?

java - 访问 Java 调用堆栈

java - 将一个程序分成两个不同的类

Java、GUI 构建器还是手动编码?

我的 for 循环中出现 java.lang.OutOfMemoryError

Java 未处理异常消失