我一直在研究这个计算器进行课堂作业,而且无法找出找出错误的地方。声明如果没有足够数量的处理器数量。 我被允许有额外的数字(即3 3 3 +),但不能太少(即3 +)
如果您有任何想法,请告诉我......
import java.util.*;
class RPNCalculator{
public static void main (String [] str){
Scanner scan = new Scanner(System.in);
System.out.println("Please enter numbers and operations in PRN format");
String input = scan.nextLine();
String [] split = input.split("\\s+");
double [] numbers = new double [1000];
int numAmount = 0;
for (int i = 0; i < split.length; i++){
if (split[i].equals("+")){
numbers[numAmount-2] = numbers[numAmount-2] + numbers[numAmount-1];
numAmount--;
}
else if (split[i].equals("-")){
numbers[numAmount-2] = numbers[numAmount-2] - numbers[numAmount-1];
numAmount--;
}
else if (split[i].equals("*")){
numbers[numAmount-2] = numbers[numAmount-2] * numbers[numAmount-1];
numAmount--;
}
else if (split[i].equals("/")){
numbers[numAmount-2] = numbers[numAmount-2] / numbers[numAmount-1];
numAmount--;
}
else {
numbers[numAmount] = Double.parseDouble(split[i]);
numAmount++;
}
}
System.out.print (numbers[numAmount-1]);
}
}