Java 代码的某些循环错误
我设计了一个RPN算法来计算给计算器的和的结果。我的RPN代码如下:Java 代码的某些循环错误,java,casting,compiler-errors,rpn,Java,Casting,Compiler Errors,Rpn,我设计了一个RPN算法来计算给计算器的和的结果。我的RPN代码如下: import java.util.ArrayList; public class RPNCalculator { String operator; double number_1; double number_2; double result; public int length_change; public void calculateResult(ArrayList<Obj
import java.util.ArrayList;
public class RPNCalculator {
String operator;
double number_1;
double number_2;
double result;
public int length_change;
public void calculateResult(ArrayList<Object> outputQueueArray)
{
int length = outputQueueArray.size();
length_change = length;
int i;
int b;
int a;
for(b = 0; b < length; b++){
for(i = 0; i < length_change; i++){
if(outputQueueArray.get(i).equals("+") || outputQueueArray.get(i).equals("-") || outputQueueArray.get(i).equals("/") || outputQueueArray.get(i).equals("*")){
a = i - 2;
operator = (String) outputQueueArray.remove(i) ;
number_1 = (double) outputQueueArray.remove(i - 1);
number_2 = (double) outputQueueArray.remove(i - 2);
outputQueueArray.add(a,useOperator(number_1, number_2, operator));
length_change = outputQueueArray.size();
System.out.println(outputQueueArray);
}
}
}
}
public double useOperator(double number_1, double number_2, String operator)
{
if(operator.equals("+")){
return number_2 + number_1;
}
else if(operator.equals("-")){
return number_2 - number_1;
}
else if(operator.equals("/")){
return number_2 / number_1;
}
else if(operator.equals("*")){
return number_2 * number_1;
}
else{
return 0;
}
}
}
它给出以下输出:
[1.5, 8.0, 3.0, +, -, 2.0, /]
[1.5, 11.0, -, 2.0, /] java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Double
因此,在处理[1.5,11.0,-,2.0,/]时会出现错误
但是,如果我最初给出以下计算:
[3.0, 2.0, /, 8.0, 3.0, +, -, 2.0, /]
[1.5, 11.0, -, 2.0, /]
它给出了正确的答案:
[1.5, 11.0, -, 2.0, /]
[-9.5, 2.0, /]
[-4.75]
有人能帮忙吗:)
p、 很抱歉问了这么长的问题你最好使用堆栈
// returns result instead of modifying the input list
// input is still a list of Double s (literals) and String s (operators)
public double calculateResult(ArrayList<Object> input)
{
// create new java.util.Stack
// the new stack is empty
Stack<Double> operands = new Stack<>();
for (Object o : input) {
if (o instanceof String) {
// remove operands of the operation from the stack and "replace"
// with the result of the operation
double operand2 = operands.pop();
double operand1 = operands.pop();
operands.push(useOperator(operand2, operand1, o));
} else {
// push a "literal" (i.e. a Double from input) to operands
operands.push((Double)o);
}
}
if (operands.size() != 1)
throw new IllegalArgumentException("Input not valid. Missing operator or empty input.");
return operands.pop();
}
//返回结果,而不是修改输入列表
//输入仍然是双s(文本)和字符串s(运算符)的列表
公共双计算器结果(ArrayList输入)
{
//创建新的java.util.Stack
//新堆栈为空
堆栈操作数=新堆栈();
for(对象o:输入){
如果(字符串的o实例){
//从堆栈中删除操作数并“替换”
//根据手术结果
双操作数2=操作数.pop();
双操作数1=操作数.pop();
push(使用运算符(操作数2,操作数1,o));
}否则{
//将“文本”(即输入的双精度)推送到操作数
操作数。push((双)o);
}
}
if(操作数.size()!=1)
抛出新的IllegalArgumentException(“输入无效。缺少运算符或输入为空”);
返回操作数.pop();
}
这样,算法应该会更快,因为从ArrayList L的位置i删除元素需要O(L.size()-i)时间
执行示例
input=newarraylist(Arrays.asList)(新对象[]){
新双(3.0),
新双(2.0),
"/",
新双(8.0),
新双(3.0),
"+",
"-",
新双(2.0),
"/"}))
:
- 循环前:
操作数=[]
- 使用
进行迭代后:o=3.0
操作数=[3.0]
- 使用
进行迭代后:o=2.0
操作数=[3.0,2.0]
的迭代:o=“/”
- 从
:操作数中删除操作数
操作数=[];操作数1=3.0;操作数2=2.0
- 将
的结果推送到堆栈:(操作数1/操作数2)
操作数=[1.5]
- 从
- 使用
进行迭代后:o=8.0
操作数=[1.5,8.0]
- 使用
进行迭代后:o=3.0
操作数=[1.5,8.0,3.0]
的迭代:o=“+”
- 从
:操作数中删除操作数
操作数=[1.5];操作数1=8.0;操作数2=3.0
- 将
的结果推送到堆栈:(操作数1+操作数2)
操作数=[1.5,11.0]
- 从
的迭代:o=“-”
- 从
:操作数中删除操作数
操作数=[];操作数1=1.5;操作数2=11.0
- 将
的结果推送到堆栈:(操作数1-操作数2)
操作数=[-9.5]
- 从
- 使用
进行迭代后:o=2.0
操作数=[-9.5,2.0]
的迭代:o=“+”
- 从
:操作数中删除操作数
操作数=[];操作数1=-9.5;操作数2=2.0
- 将
的结果推送到堆栈:(操作数1+操作数2)
操作数=[-7.5]
- 从
- 您最好使用堆栈
// returns result instead of modifying the input list
// input is still a list of Double s (literals) and String s (operators)
public double calculateResult(ArrayList<Object> input)
{
// create new java.util.Stack
// the new stack is empty
Stack<Double> operands = new Stack<>();
for (Object o : input) {
if (o instanceof String) {
// remove operands of the operation from the stack and "replace"
// with the result of the operation
double operand2 = operands.pop();
double operand1 = operands.pop();
operands.push(useOperator(operand2, operand1, o));
} else {
// push a "literal" (i.e. a Double from input) to operands
operands.push((Double)o);
}
}
if (operands.size() != 1)
throw new IllegalArgumentException("Input not valid. Missing operator or empty input.");
return operands.pop();
}
//返回结果,而不是修改输入列表
//输入仍然是双s(文本)和字符串s(运算符)的列表
公共双计算器结果(ArrayList输入)
{
//创建新的java.util.Stack
//新堆栈为空
堆栈操作数=新堆栈();
for(对象o:输入){
如果(字符串的o实例){
//从堆栈中删除操作数并“替换”
//根据手术结果
双操作数2=操作数.pop();
双操作数1=操作数.pop();
push(使用运算符(操作数2,操作数1,o));
}否则{
//将“文本”(即输入的双精度)推送到操作数
操作数。push((双)o);
}
}
if(操作数.size()!=1)
抛出新的IllegalArgumentException(“输入无效。缺少运算符或输入为空”);
返回操作数.pop();
}
这样,算法应该会更快,因为从ArrayList L的位置i删除元素需要O(L.size()-i)时间
执行示例
input=newarraylist(Arrays.asList)(新对象[]){
新双(3.0),
新双(2.0),
"/",
新双(8.0),
新双(3.0),
"+",
"-",
新双(2.0),
"/"}))
:
- 循环前:
操作数=[]
- 使用
进行迭代后:o=3.0
操作数=[3.0]
- 使用
进行迭代后:o=2.0
操作数=[3.0,2.0]
的迭代:o=“/”
- 从
:操作数中删除操作数
操作数=[];操作数1=3.0;操作数2=2.0
- 将
的结果推送到堆栈:(操作数1/操作数2)
操作数=[1.5]
- 从
- 使用
进行迭代后:o=8.0
操作数=[1.5,8.0]
- 使用
进行迭代后:o=3.0
操作数=[1.5,8.0,3.0]
的迭代:o=“+”
- 从
:操作数中删除操作数
操作数=[1.5];操作数1=8.0;操作数2=3.0
- 将
的结果推送到堆栈:(操作数1+操作数2)
操作数=[1.5,11.0]
- 从
的迭代:o=“-”
- 从
:操作数中删除操作数
操作数=[];操作数1=1.5;操作数2=11.0
- 将
的结果推送到堆栈:(操作数1-操作数2)
操作数=[-9.5]
- 从
- 使用
进行迭代后:o=2.0
操作数=[-9.5,2.0]