英文:
java Stack homework put number on stack
问题
以下是您提供的代码的翻译部分:
import java.util.Stack;
Stack<Integer> stk = new Stack<>();
int res = 0;
for (int i = 0; i < expression.length(); i++) {
    char ch = expression.charAt(i);
    if (Character.isDigit(ch)) {
        stk.push(ch - '0');
    } else {
        if (stk.isEmpty()) {
            return 0;
        }
        if (ch == '-') {
            res = stk.pop() - stk.pop();
            res = res * -1;
        }
        if (ch == '*') {
            res = stk.pop() * stk.pop();
        }
        if (ch == '+') {
            res = stk.pop() + stk.pop();
        }
        if (ch == '/') {
            res = stk.pop() / stk.pop();
        }
    }
}
return res;
请注意,上述翻译的代码部分是您提供的 Java 代码的翻译。如果您有任何进一步的问题或需要解决问题,请随时提问。
英文:
Hello eveyone in need help to answer about question in java stack
This is my question:
Complete the method below that evaluates a string representing an arithmetic expression in reverse Polish (postfix) notation. The string contains an expression consisting of numbers '0' to '9' and operators '+', '-', '*' and '/'.
For example, these are some possible input strings and the corresponding results:
Input:         Output:
"12+"             3       
"43*"            12       
"123+"           5       
"123+"           7       
"12+3*"           9       
To help you solve this problem, you can use the standard class Stack<Integer> which implements a stack of integers:
Stack<Integer> stk = new Stack<>(); // create a stack
stk.push(5);                        // push an integer onto the top of the stack
int n = stk.pop();                  // pop an integer from the top of the stack
if ( stk.empty() ) ...              // test if the stack is empty
I started to answer this is my code:
    Stack<Integer> stk = new Stack<>();
int res=0;
        for (int i = 0; i < expression.length(); i++) {
            char ch = expression.charAt(i);
            if (Character.isDigit(ch)) {
                stk.push(ch - '0');
            } 
else {
                if(stk.isEmpty())
            {
                return 0;
            }
                if (ch == '-') {
                    res = stk.pop() - stk.pop();
                    res=res*-1;
                }
                if (ch == '*') {
                    res = stk.pop() * stk.pop();
                }
                if (ch == '+') {
                    res = stk.pop() + stk.pop();
                }
                if (ch == '/') {
                    res = stk.pop() / stk.pop();
                }
            }
}
     return res;
This is error that i recived, i don't understand what is not ok in this code.
error:
 An exception of type java.util.EmptyStackException was reported when executing this line:
    res = stk.pop() * stk.pop();
Exception in thread "main" java.util.EmptyStackException
    at java.util.Stack.peek(Stack.java:102)
    at java.util.Stack.pop(Stack.java:84)
    at Main.evaluate(Main.java:28)
    at Main.exitTest(Main.java:78)
    at Main.main(Main.java:104)
i need your help thanks for everyone.
答案1
得分: 0
To check if the stack is empty, use stk.isEmpty().
For a computation, pop two items from the stack, do the computation on them, and push it on the stack. For example, multiplication:
if (ch == '*') {
    stk.push(stk.pop() * stk.pop());
}
英文:
To check if the stack is empty, use stk.isEmpty().
For a computation, pop two items from the stack, do the computation on them, and push it on the stack. For example a multiplication:
if (ch == '*') {
    stk.push(stk.pop() * stk.pop());
}
专注分享java语言的经验与见解,让所有开发者获益!

评论