簡體   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