'ans'字符串包含“55/42 =”(假设)。我正在尝试计算这个字符串的答案并将其存储在'ans'本身。请在代码中指出我的错误。
public void equalto() {
int i, j, ltemp2;
String temp1 = "", temp2 = "", temp2rev = "", temp3 = "";
char temp;
double temp11, temp22, temp33;
for (i = 0; ans.charAt(i) != '='; i++) {
if (ans.charAt(i) == '/') {
j = i + 1;
while (ans.charAt(j) != '+' || ans.charAt(j) != '-' || ans.charAt(j) != '*' || ans.charAt(j) != '/' || ans.charAt(j) != '=') {
temp = ans.charAt(j);
temp1 = temp1 + temp;
j++;
}
j = i - 1;
while ((j >= 0) && (ans.charAt(j) != '+' || ans.charAt(j) != '-' || ans.charAt(j) != '*' || ans.charAt(j) != '/')) {
temp = ans.charAt(j);
temp2 = temp2 + temp;
j--;
}
}
}
ltemp2 = temp2.length(); //For reversing the temp2
for (j = ltemp2 - 1; j >= 0; j--)
temp2rev = temp2rev + temp2.charAt(j);
temp11 = Double.parseDouble(temp1);
temp22 = Double.parseDouble(temp2rev); //temp2rev is the actual string. temp2 is in the reversed form.
temp33 = temp22 / temp11;
temp3 = String.valueOf(temp33);
ans = ans.replaceFirst(temp2rev + "/" + temp1 + "=", temp3);
}
答案 0 :(得分:0)
while(ans.charAt(j)!='+' || ...
while((j>=0)&&(ans.charAt(j)!='+' || ...
替换所有||与&&
temp2=temp2+temp;
直接获得正确的temp2,你可以
temp2 = temp + temp2;