以下是我正在编写的一个项目的代码。这个想法是旅行到不同的行星,并将这些“行星”推送到我制作的自定义堆栈类中。我目前正在尝试制作一种方法,当你键入这些基于对象的行星时,它会弹出它们。然而,我在写这篇文章时遇到了麻烦。
Main方法:
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 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方法在这里。我考虑使用stack类中的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();
}
}发布于 2015-04-21 13:33:40
看起来你需要一个可搜索的集合,而不是堆栈。你应该使用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
https://stackoverflow.com/questions/29763618
复制相似问题