我做了一個計算(字符串)來評估字符串類型的操作數,例如「2 + 3」。 我已經嘗試了很多操作數和方法工作正常,除了這個: 計算(「2.002 * 1000)//返回2001.9999999999 我很好奇這個錯誤怎麼發生的我經歷了很多次代碼,我想不通問題出在哪裏Java方法計算(「2.002 * 1000」)//返回2001.999999999999
下面的代碼:。
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;
}
在此先感謝
http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html – kosa
@Nambari我正要張貼自己 – Will