[英]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.