初学者 - Stack类返回对象的引用,而不是名称

时间:2015-04-21 05:24:52

标签: java object stack

这是我正在撰写的项目的代码。我们的想法是前往不同的星球并拥有这些行星"被推到我所做的自定义堆栈类。我正在尝试制作一种方法,当你输入它们时弹出这些基于对象的行星。但是,我在编写它时遇到了麻烦。

主要方法:

public static void main(String[] args) {
    StackX theStack = new StackX(99);

    String current = "Enterprise";
    int logOut = 1;
    String planetName;
    int secretCode;
    //This is the start out, do not loop this
    System.out.println("You are on the enterprise, where would you like to teleport?");

    //while loop will be done after reaching Io
    do {

        Scanner input = new Scanner(System.in);
        //Grab input from user
        System.out.println("Enter a planet name: ");
        planetName = input.nextLine();
        System.out.println("Enter a the four digit code for " + planetName + ": ");
        secretCode = input.nextInt();

        if (current.equals("Enterprise")){

            if (planetName.equalsIgnoreCase("Europa") && secretCode == 9007){
                current = planetName;
                Moon Europa = new Moon(planetName, secretCode);
                theStack.push(Europa);
            } else if (planetName.equalsIgnoreCase("Titan") && secretCode == 1232){
                current = planetName;
                Moon Titan = new Moon(planetName, secretCode);
                theStack.push(Titan);
            } else if (planetName.equalsIgnoreCase("Rhea") && secretCode == 5623){
                current = planetName;
                Moon Rhea = new Moon(planetName, secretCode);
                theStack.push(Rhea);
            } else {
                logOut = 0;
                System.out.println("You are dead.");
            }

        } else if (current.equals("Titan")){

            if (planetName.equalsIgnoreCase("Enterprise") && secretCode == 1212){
                current = planetName;
                Moon Enterprise = new Moon(planetName, secretCode);
                theStack.push(Enterprise);
            } else if (planetName.equalsIgnoreCase("Rhea") && secretCode == 5623){
                current = planetName;
                Moon Rhea = new Moon(planetName, secretCode);
                theStack.push(Rhea);
            } else if (planetName.equalsIgnoreCase("Elara") && secretCode == 1264){
                current = planetName;
                Moon Elara = new Moon(planetName, secretCode);
                theStack.push(Elara);
            } else {
                logOut = 0;
                System.out.println("You are dead.");
            }

        } else if (current.equals("Rhea")){

            if (planetName.equalsIgnoreCase("Enterprise") && secretCode == 1212){
                current = planetName;
                Moon Enterprise = new Moon(planetName, secretCode);
                theStack.push(Enterprise);
            } else if (planetName.equalsIgnoreCase("Titan") && secretCode == 1232){
                current = planetName;
                Moon Titan = new Moon(planetName, secretCode);
                theStack.push(Titan);
            } else if (planetName.equalsIgnoreCase("Europa") && secretCode == 9007){
                current = planetName;
                Moon Europa = new Moon(planetName, secretCode);
                theStack.push(Europa);
            } else {
                logOut = 0;
                System.out.println("You are dead.");
            }

        } else if (current.equals("Europa")){

            if (planetName.equalsIgnoreCase("Enterprise") && secretCode == 1212){
                current = planetName;
                Moon Enterprise = new Moon(planetName, secretCode);
                theStack.push(Enterprise);
            } else if (planetName.equalsIgnoreCase("Rhea") && secretCode == 5623){
                current = planetName;
                Moon Rhea = new Moon(planetName, secretCode);
                theStack.push(Rhea);
            } else if (planetName.equalsIgnoreCase("Metis") && secretCode == 2535){
                current = planetName;
                Moon Metis = new Moon(planetName, secretCode);
                theStack.push(Metis);
            } else {
                logOut = 0;
                System.out.println("You are dead.");
            }

        } else if (current.equals("Elara")){

            if (planetName.equalsIgnoreCase("Titan") && secretCode == 1232){
                current = planetName;
                Moon Titan = new Moon(planetName, secretCode);
                theStack.push(Titan);
            } else if (planetName.equalsIgnoreCase("Metis") && secretCode == 2535){
                current = planetName;
                Moon Metis = new Moon(planetName, secretCode);
                theStack.push(Metis);
            } else {
                logOut = 0;
                System.out.println("You are dead.");
            }

        } else if (current.equals("Metis")){

            if (planetName.equalsIgnoreCase("Europa") && secretCode == 9007){
                current = planetName;
                Moon Europa = new Moon(planetName, secretCode);
                theStack.push(Europa);
            } else if (planetName.equalsIgnoreCase("Elara") && secretCode == 1264){
                current = planetName;
                Moon Elara = new Moon(planetName, secretCode);
                theStack.push(Elara);
            } else if (planetName.equalsIgnoreCase("Io") && secretCode == 4792){
                current = planetName;
                Moon Io = new Moon(planetName, secretCode);
                theStack.push(Io);
                System.out.println("You have acquired the Macho Orb!");
            } else {
                logOut = 0;
                System.out.println("You are dead.");
            }

        } else if (current.equals("Io")){

            if (planetName.equalsIgnoreCase("Metis") && secretCode == 2535){
                current = planetName;
                Moon Metis = new Moon(planetName, secretCode);
                theStack.push(Metis);
            } else {
                logOut = 0;
                System.out.println("You are dead.");
            }

        } else {
            logOut = 0;
            System.out.println("You are dead.");

        }

    } while((!current.equals("Io")) && (logOut != 0));

    //Pop method

    Scanner input = new Scanner(System.in);
    System.out.println("Return to the Enterprise using the same path.");


    System.out.println("Enter a planet name: ");
    planetName = input.nextLine();
    System.out.println("Enter a the four digit code for " + planetName + ": ");
    secretCode = input.nextInt();
    while(!theStack.isEmpty() ){
        if(planetName.equals(current)){
            theStack.pop();
        }
    }

}

Stack class:

class StackX { 
private int maxSize;        // size of stack array 
private Object[] stackArray; 
private int top;            // top of stack 

public StackX(int s)         // constructor 
  { 
  maxSize = s;             // set array size 
  stackArray = new Object[maxSize];  // create array 
  top = -1;                // no items yet 
  } 

public void push(Object j)  // put item on top of stack 
  { 
  stackArray[++top] = j;     // increment top, insert item 
  } 

public void pop()         // take item from top of stack 
  { 
  if(stackArray[top]!=null){
   stackArray[top]=null;
   top--;
  }
  } 

public Object peek()        // peek at top of stack 
  { 
  return stackArray[top]; 
  } 

public boolean isEmpty()    // true if stack is empty 
  { 
  return (top == -1);  // = =  returns true or false 
  } 

public boolean isFull()     // true if stack is full 
  { 
  return (top == maxSize-1);  // =  =   returns true or false 
  } 

}  // end class StackX 

月亮类(创建月球对象):

class Moon {
private int code;
private String name;

public Moon(String name, int code){
    setName(name);
    setCode(code);
}

public String getName(){
    return name;
}

public int getCode(){
    return code;
}

public void setName(String newName){
    name = newName;
}

public void setCode(int newCode){
    code = newCode;
}

我想写的pop方法就在这里。我想过使用堆栈类中的peek()方法来比较进入堆栈顶部的输入,但是peek()方法只返回对peek对象的引用。有一个更好的方法吗?我想比较对象的名称和代码(int值)与给定的输入(如果可能)。我知道代码很乱,没有方法,但请帮助一个noobie。

//Pop method

    Scanner input = new Scanner(System.in);
    System.out.println("Return to the Enterprise using the same path.");


    System.out.println("Enter a planet name: ");
    planetName = input.nextLine();
    System.out.println("Enter a the four digit code for " + planetName + ": ");
    secretCode = input.nextInt();
    while(!theStack.isEmpty() ){
        if(planetName.equals(current)){
            theStack.pop();
        }
    }

1 个答案:

答案 0 :(得分:0)

好吧,看来你需要一个可搜索的集合,而不是堆叠。您应该使用List来保存对象。稍后,您可以迭代它并检查所需的对象。

https://docs.oracle.com/javase/7/docs/api/java/util/List.html

http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.htm

http://www.tutorialspoint.com/java/java_arraylist_class.htm