java的。在房间之间移动,但卡在一个

时间:2014-10-09 11:10:49

标签: java

只有这么长时间你才能在失明之前看一个问题。我有两个叫做A和B的房间,我想在它们之间移动,但无论我做什么,我都被困在房间A(当前)和失败消息("这里没有空间&#34 ;)弹出我写的任何东西。我要添加一堆房间,所以我不能过多地改变代码,但另一方面,我不认为我到了很远的地方。我尝试了很多东西,但是在把它交给你之前,我不想添加太多代码并弄乱它们。 。

要清楚,如果我写得正确我希望外印是你在B房间,当前要转移到B.如果我向左写,我希望输出为"她没有空间和目前留在A室。

如果有人知道我做错了什么,那将会非常有帮助。

import java.util.Scanner;
public class Totest {
  public static void main (String [] args ) {
    Scanner keyboard = new Scanner (System.in);

    TryRoom A = new TryRoom  ("room a","Awesome","Left");
    TryRoom B = new TryRoom ("room b","Bad","Right");
    B.setLeft(A);
    A.setRight(B);

    TryRoom Current;
    Current = A;
    String move;

    while (true) {
      System.out.println(("you are in ") + (Current.getName()));
      System.out.println(("the exit is ") + (Current.getExit()));
      move = keyboard.nextLine();

      if(move.equals("right")){
        if(Current.checkRight()){
          Current = Current.getRight();//problem?
        }else{
          System.out.println("there is no room here");
        }
      }
      if(move.equals("left")){
        if(Current.checkLeft()){//problem?
          Current = Current.getLeft();
        }else{
          System.out.println("There is no room here");
        }
      }
    }//while  
  }
}

:::::::::::

class TryRoom {
  private String name;
  private String description;
  private String exit;
  private TryRoom left;
  private TryRoom right;  

  public TryRoom(String name, String description, String exit)
  {
    this.name = name;
    this.description = description;
    this.exit = exit;
  }

  public String getName(){
    return name;
  } 

  public String getDescription(){
    return description;
  } 

  public String getExit(){
    return exit;
  } 

  void setLeft(TryRoom A)
  {this.left=left;}
  public TryRoom getLeft() 
  {return left;}

  void setRight(TryRoom B)
  {this.right=right;}
  public TryRoom getRight() 
  {return right;}

  boolean checkRight(){ 
    if (this.right==null){
      return false;}
    else{
      return true;}
  }

  boolean checkLeft(){
    if (this.left==null){
      return false;}
    else{
      return true;}
  }
}

2 个答案:

答案 0 :(得分:5)

你的问题在你的二传手中:

void setLeft(TryRoom A)
{this.left=A;}

setRight()方法的同义词

答案 1 :(得分:0)

我相信你已经用setLeftsetRight方法获得了它。尝试以这种方式改变它:

void setLeft(TryRoom A)
  {this.left=A;}

void setRight(TryRoom B)
  {this.right=B;}