需要帮助获取整数堆栈推送和弹出程序以显示推送的整数

时间:2016-12-04 07:14:34

标签: java arrays stack push pop

我试图编写的程序应该在数组LIFO堆栈中容纳10个用户未输入的整数,其中计算在IntStack类中完成。使用push方法向堆栈添加整数的选项,使用pop方法从堆栈中删除整数,并显示堆栈都在demo类中。我的问题是当试图将一个数字推入堆栈时,我得到了这个奇怪的输出" [I @ 55f96302"。任何帮助表示赞赏。

演示类:

import java.util.Scanner;

public class demo {

public static void main(String[] args) {

IntStack stack1;
IntStack stack2;

stack1 = new IntStack();
stack2 = new IntStack();

int value = 0;
int choice = 0;
Scanner inTok = new Scanner(System.in);

do { 

System.out.println("Choose one of these options:");
System.out.println("\t1. Add a number to the stack.");
System.out.println("\t2. Remove a number from the stack.");
System.out.println("\t3. Display the stack.");
System.out.println("\t4. Add a number to the queue."); 
System.out.println("\t5. Remove a number from the queue.");
System.out.println("\t6. Display the queue.");
System.out.println("\t7. Quit.");
System.out.print("Enter 1, 2, 3, 4, 5, 6, or 7: ");

choice = inTok.nextInt();


switch(choice){
  case 1:
  System.out.println(stack1.toString());

  break;

  case 2:

  break;

  case 3:

  break;

  case 4:

  break;

  case 5:

  break;

  case 6:

  break;

  case 7:
  System.out.print("\nGoodbye ");
  break;

  default:
  System.out.println("\n" + choice + " is invalid");
  }

}while (choice != 7);   

  }
}

IntStack类:

public class IntStack {

private int[] elements;
private int size; 

public static final int stackCap = 10;

public IntStack() {
  this(stackCap);
  }

public IntStack(int capacity) {
  elements = new int[capacity];
  }

public boolean empty() {
  return size == 0;
  }

public int getSize() {
  return size;
  }

public int peek() {
  return elements[size - 1];
  }

public int pop() {
  return elements[--size];
  }  

public static int push(int value) {
  return value;
  }

public String toString() {
return "Stack: " + elements;
}

} 

0 个答案:

没有答案