我需要一个在命令提示符下充当计算器的 Java 程序。我可以想出这个,但是我必须一次输入一个问题(输入“2”,按回车,输入“+”,按回车,输入“2”,按回车),我想知道我是否可以在我可以放入“2+2”的地方代替。提前感谢您的帮助!
import java.util.*;
public class memCalc
{
public static void main (String args[])
{
Scanner input = new Scanner(System.in);
String op;
int numberOne, numberTwo, result = 0;
numberOne = input.nextInt();
op = input.next();
numberTwo = input.nextInt();
if (op.equals("+"))
{
result = numberOne + numberTwo;
System.out.print("The answer is: " + result + " .\n");
}
else if (op.equals("-"))
{
result = numberOne - numberTwo;
System.out.print("The answer is: " + result + " .\n");
}
else if (op.equals("*"))
{
result = numberOne * numberTwo;
System.out.print("The answer is: " + result + " .\n");
}
else if (op.equals("/"))
{
result = numberOne / numberTwo;
System.out.print("The answer is: " + result + " .\n");
}
}
}