我创建了一个方法 calculate(String) 来评估 String 类型的操作数,例如“2+3”。我已经尝试了很多操作数并且该方法工作正常,除了这个:calculate("2.002*1000) //returns 2001.9999999999 我很好奇这个错误是怎么发生的。我已经通过代码很多次了,我不能找出问题所在。
这里的代码:
private String calculate(String str)
{
String num = "";
List<String> list = new ArrayList<String>();
for(int i = 0; i < str.length(); i++) //Arrange str into list, elements are seperated by "+" or "-"
{
if(! checkPM(str.substring(i, i + 1)))
{
num = num + str.substring(i, i + 1);
} else
{
list.add(num);
list.add(str.substring(i, i + 1));
num = "";
}
}
list.add(num);//add the last num into list
if(checkSign(list.get(list.size() - 1)))//remove last element if it is an operator
{
list.remove(list.get(list.size() - 1));
}
String numlistele = ""; //Elements of numlist
List<String> numlist = new ArrayList<String>(); //List of numbers to be TD
List<String> TDlist = new ArrayList<String>(); //List of times or divide
for(int j = 0; j < list.size(); j++) //Check which of the elements of list contains "*" or "/"
{
String tdAns = ""; //Answer of numlistele timed or divided
if(checkTD(list.get(j))) // When the elements of list contains "*" or "/"
{
for(int k = 0; k < list.get(j).length(); k++) //
{
if(! checkSign(list.get(j).substring(k, k + 1)))
{
numlistele = numlistele + list.get(j).substring(k, k+1);
} else
{
numlist.add(numlistele);
TDlist.add(list.get(j).substring(k, k+1));
numlistele = "";
}
}
numlist.add(numlistele); //Adds the last number into numlist
numlistele = ""; //Restore numlistele to "", to be used in next loop
tdAns = numlist.get(0); //Answer of numlistele timed or divided, firstly it is equals to the first elements of numlist
for(int l = 0; l < TDlist.size(); l++)
{
if(TDlist.get(l).equals("×"))
{
double tempdou = Double.parseDouble(tdAns) * //temporary double used to save to tdANS
Double.parseDouble(numlist.get(l+1));
tdAns = String.valueOf(tempdou);
} else //when TDlist.get(l).equals("/") is true
{
double tempdou = Double.parseDouble(tdAns) /
Double.parseDouble(numlist.get(l+1));
tdAns = String.valueOf(tempdou);
}
}
list.set(j, tdAns);
}
numlist.clear(); //Clear numlist for next loop
TDlist.clear(); //Clear TDlist for next loop
}
String ans = list.get(0); //Will become final answer later, first it is assign to first element of list
for(int m = 0; m < list.size(); m++)
{
if(list.get(m).equals("+"))
{
double tempdou = Double.parseDouble(ans) + //Temporary double used to save to ans
Double.parseDouble(list.get(m + 1));
ans = String.valueOf(tempdou);
} else if(list.get(m).equals("-"))
{
double tempdou = Double.parseDouble(ans) -
Double.parseDouble(list.get(m + 1));
ans = String.valueOf(tempdou);
}
}
if(ans.length() > 2)
{
if(ans.substring(ans.length() - 2).equals(".0")) //To remove .0 of the answer
{
ans = ans.substring(0, ans.length() - 2);
}
}
return ans;
}
提前致谢。