我想知道我是否能得到一些帮助,我对这个创建的类有问题,输出不正确。任何帮助将不胜感激:
import java.util.*;
public class Comparison {
public static void main(String[] args) {
Stack<Character> stack = new Stack<>();
System.out.print("Please enter arithmetic expression: For example, the expression {25 + (3 – 6) * 8}");
Scanner input = new Scanner(System.in);
{
String capture = input.next();
for (int i = 0; i < capture.length(); i++) {
char p = capture.charAt(i);
if (p == '(' || p == '{' || p == '[' || p == ')' || p == '}' || p == ']') {
stack.push(p);
}
char r = stack.peek();
if (p == '{' && r == '}' || p == '(' && r == ')' || p == '[' && r == ']') {
stack.pop();
System.out.print("Arithmetic Expression: has matched symbols. ");
} else {
System.out.print("Arithmetic Expression: has mismatched symbols. ");
}
}
}
}
}
输出:
Please enter arithmetic expression: For example, the expression {25 + (3 – 6) * 8}
{25 + (3 – 6) * 8}
Arithmetic Expression: has mismatched symbols. Arithmetic Expression: has mismatched symbols. Arithmetic Expression: has mismatched symbols. BUILD SUCCESSFUL (total time: 6 seconds)
预期产出:
预期输出应该只是符号,在这种情况下输出应该读取算术表达式:具有匹配的符号:(),{},因为一旦找到匹配代码就会弹出。数值无关紧要。但是如果没有匹配 - 算术表达式:符号不匹配。 (然后是符号)。