需要有关我的代码的帮助我的 switch 语句不起作用我尝试将其从 int 更改为 string 但无法弄清楚。我遇到问题的行是第 43 行,其中一个是我不知道如何修复的 switch 语句。有人可以帮助我吗,这非常重要,谢谢。
导入java.util.Scanner; 导入 java.util.Stack;
public class RPNCalculator
{
private static Stack<Integer> stack;
private static Object evaluator;
private static String again;
private static Scanner keyboard;
public RPNCalculator()
{
stack = new Stack<Integer>(); //creates stack
}
public static void main(String[] args)
{
String expression, again;
int result;
Scanner keyboard = new Scanner(System.in);
do
{
RPNCalculator evaluator = new RPNCalculator();
System.out.println("Enter a valid post-fix expression one token " +
"at a time with a space between each token (e.g. 5 4 + 3 2 1 - + *)");
System.out.println("Each token must be an integer or an operator (+,-,*,/)");
System.out.println();
System.out.println("Enter 'q' to quit, 'h' for help.");
expression = keyboard.nextLine();
result = evaluator.evaluate(expression);
System.out.println();
System.out.println("That expression equals " + result);
while (true) {
System.out.print("Enter command: ");
Scanner scanner;
Object input = scanner.nextLine();
switch ( ) {
case "m":
if (!stack.isEmpty()) {
stack.push(-stack.pop());
} else {
System.out.println("Error: Stack is empty");
}
break;
case "r":
if (stack.size() < 2) {
System.out.println("Error: Stack does not have enough elements");
} else {
double a = stack.pop();
double b = stack.pop();
stack.push((int) a);
stack.push((int) b);
}
break;
case "d":
if (!stack.isEmpty()) {
double top = stack.peek();
stack.push((int) top);
} else {
System.out.println("Error: Stack is empty");
}
break;
case "p":
if (!stack.isEmpty()) {
System.out.println("Top item: " + stack.peek());
} else {
System.out.println("Error: Stack is empty");
}
break;
case "n":
if (!stack.isEmpty()) {
System.out.println("Top item removed: " + stack.pop());
} else {
System.out.println("Error: Stack is empty");
}
break;
case "f":
System.out.println("Stack contents: " + stack);
break;
case "c":
stack.clear();
System.out.println("Stack cleared");
break;
case "q":
System.out.println("Exiting program");
return;
case "h":
case "?":
System.out.println("Operations:");
System.out.println("m - unary minus");
System.out.println("r - exchange the top two items");
System.out.println("d - duplicate top item on stack");
System.out.println("p - print the top item");
System.out.println("n - print and remove the top item");
System.out.println("f - print all contents of the stack");
System.out.println("c - clear the stack");
System.out.println("q - quit");
System.out.println("h or ? - print help message");
break;
default:
}
}
System.out.print("Evaluate another expression [Y/N]? ");
again = keyboard.nextLine();
System.out.println();
}
while (again.equalsIgnoreCase("y"));
}
public int evaluate(String expr)
{
int op1, op2, result = 0;
String token;
Scanner parser = new Scanner(expr);
while (parser.hasNext())
{
token = parser.next();
if (isOperator(token)) //if operator pop
{
op2 = (stack.pop()).intValue();
op1 = (stack.pop()).intValue();
result = evaluateSingleOperator(token.charAt(0), op1, op2); //
stack.push(new Integer(result));
}
else
stack.push(new Integer(Integer.parseInt(token)));
}
return result;
}
private boolean isOperator(String token)
{
return ( token.equals("+") || token.equals("-") ||
token.equals("*") || token.equals("/") || token.equals("%") );
}
private int evaluateSingleOperator(char operation, int op1, int op2)
{
int result = 0;
switch (operation)
{
case '+':
result = op1 + op2;
break;
case '-':
result = op1 - op2;
break;
case '*':
result = op1 * op2;
break;
case '/':
result = op1 / op2;
break;
case '%':
result = op1 % op2;
break;
}
return result;
}
}