Java堆栈从表达式问题到字符的基本移动
在花了4个多小时让这个流行/推送部件工作之后,我想不出来。我必须创建我自己的pop和push,就像我做的那样,用一个基本的插入字符到堆栈中,然后通过驱动程序显示它们。但现在无论我做什么都必须添加表达式,我无法摆脱“找不到符号错误”或“错误:类charStack中的方法pop无法应用于给定类型” 我试着把我的数组改成一个objects数组,我得到了一些类似的东西,但没有任何意义,也没有测试出我要做的事情 任何帮助都将不胜感激。 多谢各位Java堆栈从表达式问题到字符的基本移动,java,stack,Java,Stack,在花了4个多小时让这个流行/推送部件工作之后,我想不出来。我必须创建我自己的pop和push,就像我做的那样,用一个基本的插入字符到堆栈中,然后通过驱动程序显示它们。但现在无论我做什么都必须添加表达式,我无法摆脱“找不到符号错误”或“错误:类charStack中的方法pop无法应用于给定类型” 我试着把我的数组改成一个objects数组,我得到了一些类似的东西,但没有任何意义,也没有测试出我要做的事情 任何帮助都将不胜感激。 多谢各位 import java.util.Scanner;
import java.util.Scanner;
public class charStack
{
//class constants
private final static char ADD = '+'; // Intalizes character for adding
private final static char SUBTRACT = '-'; // Intalizes character for subtracting
private final static char MULTIPLY = '*'; // Intalizes character for multipluing
private final static char DIVIDE = '/'; // Intalizes character for dividing
//class variables
private int[] stack; // Intalizes array
private int stackSize; // Intalizes Size of stack
private int top; // Intalizes top position
public charStack()
{
//local constants
//local variables
/**************************/
stackSize = 10; // Sets size of stack to 10
stack = new int[stackSize]; // Sets array for stack
top = 0; // Sets top to zero
}// END constructor
public int calculation(String expr)
{
//local constants
//local variables
int operand1; // Intalizes operation place holder one
int operand2; // Intalizes operation place holder two
int result; // Intalizes result
String token; // Intalizes token
Scanner pars = new Scanner(expr); // Intalizes pars of scanner
/**********************************/
// Sets result to zero
result = 0;
// WHILE there is something in the string
while (pars.hasNext())
{
// Takes the first operand from the string
token = pars.next();
// IF operand is valid
if (isOperator(token))
{
// Operand 2 is moved from the stack (Issue 1)
operand2 = stack.pop();
// Operand 1 is moved from the stack (Issue 2)
operand1 = pop(stack);
// Operation is evaluated and set to result
result = evaluateSingleOperator(token.charAt(0), operand1, operand2);
// Returns the value of the given expression
stack.push = (new Integer(result));
}
// ELSE
else
{
// Set next token to top of stack (Issue 3)
stack[top] = (new Integer(Integer.parseInt(token)));
}// END IF
}// END WHILE
// Return result
return result;
}// END Calculation
public int pop()
{
//local constants
//local variables
int temp; // Intializes temp character holder
/**************************/
// Drops the count of the stack by 1
top--;
// Removes element from the top of the stack
temp = stack[top];
// Return element removed from top of stack
return temp;
}// END pop
public void push(int num)
{
//local constants
//local variables
/**************************/
// Sets top of stack to character
stack[top] = num;
// Add one to stack placement
top++;
}// END push
问题1:您正在尝试使用
stack.pop()
,但是stack
声明为int[]
,并且不是类的实例,并且int数组没有方法pop()
。问题2:您正在尝试使用pop(stack)
但您尚未声明任何采用参数的pop
方法。问题3:我不确定这里的“问题”是什么,因为我无法从您的问题中理解。无需将新整数
部分改为整数。parseInt(token)
返回一个整数
,而您的堆栈
已经是一个int[]
了。您的代码中还有更多的问题,但这些都是您突出显示的问题。请按照您对问题1的说法操作。如果我将“int[]”更改为“Stack Stack=new Stack();”那么这就是创建我的类的实例了吗?问题1:您正在尝试使用stack.pop()
,但是stack
被声明为int[]
,而不是您的类的实例,并且int数组没有方法pop()
。问题2:您正在尝试使用pop(stack)
但您尚未声明任何采用参数的pop
方法。问题3:我不确定这里的“问题”是什么,因为我无法从您的问题中理解。无需将新整数
部分改为整数。parseInt(token)
返回一个整数
,而您的堆栈
已经是一个int[]
了。您的代码中还有更多的问题,但这些都是您突出显示的问题。请按照您对问题1的说法操作。如果我将“int[]”更改为“Stack Stack=new Stack();”那就是创建我的类的一个实例?