采用自带的Stack库 public class Main { private Stack stack = new Stack<>();

public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    int N = scan.nextInt();
    Main main = new Main();
    for (int i = 0; i < N + 1; i++) {
        main.checkCommand(scan.nextLine());
    }
    scan.close();
}

public void checkCommand(String s) {
    String[] command = s.split(" ");
    if (command[0].equals("push")) {
        push(Integer.parseInt(command[1]));
    }else if (command[0].equals("pop")) {
        pop();
    }else if (command[0].equals("top")) {
        top();
    }
}

public void push(int x) {
    stack.push(x);
}

public void pop() {
    if (!stack.isEmpty()) {
        System.out.println(stack.pop());
    }else {
        System.out.println("error");
    }
}

public void top() {
    if (!stack.isEmpty()) {
        System.out.println(stack.peek());
    }else {
        System.out.println("error");
    }
}

}