繁体   English   中英

如何创建在反向打印数组的 Main 方法中调用的 Print 方法?

[英]How do I create a Print Method that is called in the Main method that prints an array in reverse?

我将堆栈作为一项任务使用,我能够根据需要执行通用的弹出、推送和查看操作。 但是我无法打印? 与我一起工作的教授所做的事情与我在上一次 class 中所教的方式不同,所以我不确定他想要什么。 我试过问他,但结果通常是我更加困惑,他听起来居高临下,因为我没有接受他的 class 并且没有了解他的其他学生到底做了什么。 我有一系列方法来操作字符堆栈。 我应该从 main 方法调用 pop、peek 和 push 来添加和查看它。 然而,教授也想要一种打印方法,但我不确定该怎么做。 这是我到目前为止所拥有的;

public class StackChar {

    //data
    private static int size;
    private static int top;
    private static char[] data;

    public StackChar() {
    }

    public StackChar(int sz) {
        size = sz;
        top = -1;
        data = new char[size];
    }

    /**
     * Description:  This method will push an item onto the top of the stack
     */

    public void push(char letter) {
        if (top != size - 1) {
            top += 1;
            data[top] = letter;
        } else {
            System.out.println("Error: Stack Is Full");
        }
    }

    public char pop() {
        char let1;
        if (top >= 0) {
            let1 = data[top];
            top--;
            return (let1);
        }
        System.out.println("Error: Stack Is Empty");
        return (char)-1;
    }

    public char peek() {
        char let1;
        if (top >= 0) {
            let1 = data[top];
            return (let1);
        }

        System.out.println("Error: Stack Is Empty");
        return (char)-1;
    }

    public static void prints(char[] sc) {
        if (top == -1) {
            System.out.println("Error: Stack Is Empty");
            return;
        }


        for (int i = size - 1; i > -1; i--) {
            System.out.println(letterStack[i]);
        }
    }

    public static void main(String[] args) {
        StackChar sc = new StackChar(10);

        //after each stack method push, pop, peek, you will use prints().

        sc.push('a');
        sc.push('b');
        sc.push('c');
        prints(sc);
        sc.prints();

        //push 6 a-f letters of alphabet in normal order
        //THEN
        //push the next letter of alphabet
        //THEN
        //pop 1
        //push the next letter of the alphabet
        //pop 2
        //push the next letter of the alphabet
        //pop 3
        //peek and print what the peek gives
    }

}

我的问题在于 print 方法,并从 Main 方法调用 print 方法。 我习惯于简单地在我需要的任何地方调用该方法; sc.print()例如。 这似乎不想工作,我不知道为什么。 我还尝试更改 print 方法,以便将堆栈作为参数并调用 print 方法并将数组放入其中,但这似乎也不起作用。

public final class StackChar {

    private int top = -1;
    private final char[] data;

    public StackChar(int size) {
        data = new char[size];
    }

    public int getSize() {
        return top + 1;
    }

    public void push(char ch) {
        requireNotFull();
        data[++top] = ch;
    }

    public char pop() {
        requireNotEmpty();
        return data[top--];
    }

    public char element() {
        requireNotEmpty();
        return data[top];
    }

    private void requireNotFull() {
        if (top == data.length - 1)
            throw new RuntimeException("Stack Is Full");
    }

    private void requireNotEmpty() {
        if (top == -1)
            throw new RuntimeException("Stack Is Empty");
    }

    public static void print(StackChar stack) {
        System.out.print('[');

        for (int i = 0; i <= stack.top; i++) {
            if (i > 0)
                System.out.print(',');
            System.out.print(stack.data[i]);
        }

        System.out.println(']');
    }

    public static void main(String... args) {
        StackChar stack = new StackChar(10);

        stack.push('a');
        stack.push('b');
        stack.push('c');
        print(stack);   // [a,b,c]
        stack.push('a');
        stack.push('b');
        stack.push('c');
        stack.push('d');
        stack.push('e');
        stack.push('f');
        print(stack);   // [a,b,c,a,b,c,d,e,f]
        stack.push('g');
        print(stack);   // [a,b,c,a,b,c,d,e,f,g]
        stack.pop();    // g
        print(stack);   // [a,b,c,a,b,c,d,e,f]
        stack.push('h');
        print(stack);   // [a,b,c,a,b,c,d,e,f,h]
        stack.pop();    // h
        stack.pop();    // f
        print(stack);   // [a,b,c,a,b,c,d,e]
        stack.pop();    // e
        stack.pop();    // d
        stack.pop();    // c
        print(stack);   // [a,b,c,a,b]
    }

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM