Java 初学者堆栈类返回对象的引用,而不是名称

Java 初学者堆栈类返回对象的引用,而不是名称,java,object,stack,Java,Object,Stack,这是我正在写的一个项目的代码。我们的想法是去不同的行星旅行,把这些“行星”推到我制作的自定义堆栈类上。我目前正在尝试一种方法,当你输入这些基于对象的行星时,它会弹出。然而,我写它有困难 主要方法: public static void main(String[] args) { StackX theStack = new StackX(99); String current = "Enterprise"; int logOut = 1; String plane

这是我正在写的一个项目的代码。我们的想法是去不同的行星旅行,把这些“行星”推到我制作的自定义堆栈类上。我目前正在尝试一种方法,当你输入这些基于对象的行星时,它会弹出。然而,我写它有困难

主要方法:

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();
        }
    }

}
堆栈类:

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 
月球类(创建月球对象):

我正在尝试写的pop方法就在这里。我考虑过使用stack类中的peek()方法来比较堆栈顶部的输入,但是peek()方法只返回对peek对象的引用。有更好的方法吗?如果可能的话,我想将对象的名称和代码(int值)与给定的输入进行比较。我知道代码非常混乱,没有任何方法,但是请帮个忙

//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();
        }
    }

嗯,这里似乎需要一个可搜索的集合,而不是堆栈。您应该使用
列表
来保存对象。稍后,您可以迭代它并检查所需的对象


当然,它会返回一个对象引用。这就是你要放在堆栈中的内容。您的
while
循环逻辑也错误。它将进入一个无限循环。@Kayaman我知道while循环将是无限的,最后一部分只是一个粗略的轮廓,它需要如何。我只需要知道如何比较对象名和代码,而不是引用(如果可能的话),这就是为什么在Planet类中有
getName()
方法。这些代码都是你的吗?因为编写这么多代码,然后在自己的类上调用方法时遇到问题,这似乎很奇怪……我已经编写了所有这些代码,我基于对象的编程知识有点生疏,这就是为什么。我知道getName()和getCode()方法可以返回对象的名称和代码,但是我在实现它时遇到了困难。但是您已经完成了所需的比较,例如。为月球做这件事并没有什么不同。它们显然是为了编写自己的堆栈类。
//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();
        }
    }