簡體   English   中英

在自定義int堆棧數組中接收到錯誤的結果

[英]Receiving the wrong results in custom int Stack Array

我正在為整數做自己的堆棧數組。 它工作正常,除了在將值添加到StackArray時始終添加0。 每次我想接收peek的值時,結果就是它是空的。 我已經多次更改了代碼,但是當我將其更改為與給出的代碼不同的代碼時,在執行過程中會出錯

這是StackArray類

public class StackArray{
//declare variables
int top, size, length;
int array[];

//constructor to initialize variables
public StackArray(int _size)
{
    length = 0;
    top = -1;
    size = _size;
    array = new int[size];
}

//push method to add numbers to the stack
void push(int newNum)
{
    //if statement to check if the stack is full
    if(top != size)
    {
        //update top and length
        top++;
        length++;
        array[top] = newNum;
    }
}

//method to remove the top number in the stack
int pop()
{
    //declare local variable
    int temp;
    //if statement to check if stack is not empty
    if(!isEmpty())
    {
        temp = top;
        top--;
        length--;
    }
    else
        System.out.println("No more items in Stack.");

    return top;
}

//boolean method to check if the stack is empty
boolean isEmpty()
{
    if(top == -1)
        return true;
    else
        return false;
}

//method to return the size of the stack
int size()
{
    return length;
}

//method to print out the top number in the stack
void peek() {
    if (isEmpty())
        System.out.println(array[top]);
    else
        System.out.println("Stack is empty");

}

//method to turn the stack into a String
public String toString()
{
    System.out.print("Stack: [");
    for(int i = 0; i <= length; i++)
    {
        System.out.print(array[i] + ", ");
    }
    System.out.println("]");

    return "";
}}

這是我用來運行程序的Driver類

import java.util.Scanner;
public class Driver
{
    public static void main(String[] args)
    {
        //declare variables and initialize scanner
        Scanner key = new Scanner(System.in);
        int size, choice, value, end;
    end = 0;

    //ask user to enter the length of the stack
    System.out.print("Please enter the length of the stack: ");
    size = key.nextInt();

    //declate and initialize the stack
    StackArray stack1 = new StackArray(size);

    //loop to continue operations
    while(end == 0)
    {
        //print out menu for commands
        System.out.println("\t1) Push \n\t2) Pop \n\t3) Peek \n\t4) Size \n\t5) isEmpty \n\t6) End");
        System.out.print("Please choose an option: ");
        choice = key.nextInt();

        //switch the choice and execute commands
        switch (choice)
        {
            case 1: System.out.println("Please enter a value: ");
                    value = key.nextInt();
                    stack1.push(value);
                    stack1.toString();
                    break;
            case 2: stack1.pop();
                    stack1.toString();
                    break;
            case 3: stack1.peek();
                    stack1.toString();
                    break;
            case 4: System.out.println("Size: " + stack1.size());
                    stack1.toString();
                    break;
            case 5: if(!stack1.isEmpty())
                    {
                        System.out.println("Stack is empty.");
                    }
                    else
                        System.out.println("Stack is NOT empty.");
                    stack1.toString();
                    break;
            case 6: end = 1;
                    System.out.println("Goodbye!");
                    break;
        }
    }
}

}

該代碼有幾個問題:

void peek() {
    //This was the change - you can peek as long as the stack
    // is NOT empty.
    if (!isEmpty())
        System.out.println(array[top]);
    else
        System.out.println("Stack is empty");

}

在這種狀態下,堆棧堆棧為空的那一刻,您將出現超出范圍的異常。 這很容易解決。 另外,您的pop方法從根本上是錯誤的:

int pop()
{
    //declare local variable
    int temp;
    //if statement to check if stack is not empty
    if(!isEmpty())
    {
        temp = array[top]; //note the change here
        top--;
        length--;
    }
    else
        System.out.println("No more items in Stack.");

    return temp; //note the change here
}

另外-我建議toString實現其原始目的-返回字符串表示形式,而不是將其打印出來。

public String toString()
{
    String returnStr = "Stack: [";
    for(int i = 0; i < length; i++)
    {
        returnStr+= array[i] + ", ";
    }
    returnStr += "]";

   return returnStr;
}

現在而不是編寫array1.toString(); 在您的主代碼中,只需編寫System.out.println(array1); 隱含了toString。 請注意,我將<=更改為<,現在在按下后您將在打印輸出中沒有多余的0!

另一個問題與您的主要案例陳述有關:

 //The check here should be positive - say the stack is empty when it is!
 case 5: if(stack1.isEmpty()) {
             System.out.println("Stack is empty.");
         }
         else
             System.out.println("Stack is NOT empty.");

我認為這就是全部。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM