我现在正在学习堆栈,我的代码编译后发现。当我运行它时,代码不会打印我的调试 println 和错误
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at java.util.Arrays.copyOf(Arrays.java:2760)
at java.util.Arrays.copyOf(Arrays.java:2734)
at java.util.Vector.ensureCapacityHelper(Vector.java:226)
at java.util.Vector.addElement(Vector.java:573)
at java.util.Stack.push(Stack.java:50)
at stacks.main(stacks.java:56)
被陈列。
我的代码如下所示:
import ch03.stacks.*;
import java.util.*;
public class stacks {
public static void main (String []args){
System.out.printf("Enter a math equation in reverse polish notation:\n");
Stack<Double> pemdas = new Stack<Double>();
Scanner input = new Scanner(System.in);
String in = input.next();
double temp1, temp2, resultant = 0;
while(input.hasNext()){
if(in == "+"){
temp1 = pemdas.peek();
pemdas.pop();
temp2 = pemdas.peek();
pemdas.pop();
resultant = temp1 + temp2;
pemdas.push(resultant);
System.out.println(resultant);
}
if(in == "-"){
temp1 = pemdas.peek();
pemdas.pop();
temp2 = pemdas.peek();
pemdas.pop();
resultant = temp1 - temp2;
pemdas.push(resultant);
System.out.println(resultant);
}
if(in == "*"){
temp1 = pemdas.peek();
pemdas.pop();
temp2 = pemdas.peek();
pemdas.pop();
resultant = temp1 * temp2;
pemdas.push(resultant);
System.out.println(resultant);
}
if(in == "/"){
temp1 = pemdas.peek();
pemdas.pop();
temp2 = pemdas.peek();
pemdas.pop();
resultant = temp1 / temp2;
pemdas.push(resultant);
System.out.println(resultant);
}
else
pemdas.push(Double.parseDouble(in));
System.out.println(resultant);
}
System.out.println("Answer:"+ resultant);
}
}
因此,我首先以反向波兰表示法读取整数字符串,然后如果它不是操作数,则将其弹出到我的堆栈中。至少这就是我认为它正在做的事情。任何帮助是极大的赞赏。