問題描述
用的是toString();
你期待的結(jié)果是什么?實際看到的錯誤信息又是什么?
我期待是用科學(xué)計數(shù)法顯示 結(jié)果是直接顯示
問題出現(xiàn)的環(huán)境背景及自己嘗試過哪些方法
我正在制作安卓計算器
相關(guān)代碼
// 請把代碼文本粘貼到下方(請勿用圖片代替代碼)
List<Button> list = new ArrayList<Button>();//按鈕
List<Integer> thisnum= new ArrayList<Integer>();//臨時數(shù)字
List<Character> stack = new ArrayList<Character>();//表達(dá)式
Stack<Character> SOP = new Stack<Character>();//臨時存儲運算符和分界符(
List<String> L = new ArrayList<String>();//后綴表達(dá)式
Stack<BigDecimal> scalc = new Stack<BigDecimal>();//后綴表達(dá)式運算棧
boolean ClearA = false;
case R.id.buttonA://等于號
BigDecimal re;
ClearA=true;
//STACKcompress() 變?yōu)楹缶Y表示式
if(STACKcompress()){//是否正確運行
re = suffixToResult();//運算
if(!textView1.getText().equals("錯誤")){
textView.setText(re.toString());
}
}else if(stack.size()==1){ //只有一個元素
Character character = stack.get(0);
if(character!='-'){
textView1.setText("錯誤");
stack.clear();
}else{
textView.setText('-');
}
}
return;
private boolean STACKcompress() {//將參數(shù)中綴表達(dá)式expression轉(zhuǎn)為后綴表達(dá)式存放在L中,返回L
L.clear();
SOP.clear();
Log.d("stack",stack.toString());
int dian =0; //
boolean dia=false; //
for (int i = 0; i < stack.size(); i++) //對表達(dá)式中的每一個元素
{
char element = stack.get(i);
if ('0' <= element && element <= '9') {
if(dia){
dian++;
}
thisnum.add(element-'0');
Log.d("(int) element", String.valueOf(element-'0'));
if(i +1==stack.size()){
BigDecimal ten=BigDecimal.valueOf(10);
BigDecimal zxc= BigDecimal.valueOf(0);
for(int a=0;a<thisnum.size();a++){
zxc=zxc.multiply(ten);
zxc=zxc.add(BigDecimal.valueOf(thisnum.get(a)));
}
if(dia){
for(int a=0;a<dian;a++){
zxc=zxc.divide(ten);
}
dia=false;
dian=0;
}
L.add(""+zxc);
thisnum.clear();
Log.d("String(zxc) i+1 ", ""+zxc);
}
} else if (SwitchOperator(element)) {
if(stack.size()==1&&element!='-'){
Log.d("element in if",""+element);
return false;
}
if(!thisnum.isEmpty()){
BigDecimal ten=BigDecimal.valueOf(10);
BigDecimal zxc= BigDecimal.valueOf(0);
for(int a=0;a<thisnum.size();a++){
zxc=zxc.multiply(ten);
zxc=zxc.add(BigDecimal.valueOf(thisnum.get(a)));
}
if(dia){
for(int a=0;a<dian;a++){
zxc=zxc.divide(ten);
}
dia=false;
dian=0;
}
L.add(""+zxc);
thisnum.clear();
Log.d("String(zxc) i+1 ", ""+zxc);
}
while(SopTureOrFlase(element)) {
L.add(""+SOP.pop());
}
if(i +1!=stack.size()) {
SOP.push(element);
}else if(stack.size()!=1){
return false;
}
}else if(element=='.'){
dia=true;
}else if(element=='%'){
if(!thisnum.isEmpty()) {
BigDecimal ten=BigDecimal.valueOf(10);
BigDecimal zxc = BigDecimal.valueOf(0);
for (int a = 0; a < thisnum.size(); a++) {
zxc=zxc.multiply(ten);
zxc=zxc.add(BigDecimal.valueOf(thisnum.get(a)));
}
Log.d("thisnum", thisnum.toString());
thisnum.clear();
zxc=zxc.divide(BigDecimal.valueOf(100));
Log.d("zxc", "" + zxc);
L.add("" + zxc);
}
} else{
textView.setText("警告 :不在按鍵中的字符出現(xiàn)");
Log.e("警告","不在按鍵中的字符出現(xiàn)");
this.onStop();
}
}
while (!SOP.isEmpty()) //將sop棧中剩余的所有元素彈出,追加到L后
{
L.add(""+SOP.pop());
}
return true;
}
private BigDecimal suffixToResult() { //運算后綴表達(dá)式
BigDecimal temp_value ;
for (int i = 0; i < L.size(); i++) {
String element1 = L.get(i);
Log.d("L",L.toString());
char element = element1.charAt(0);//獲取字符串的第一個字符
Log.d("String.valueOf(BigDecimal.valueOf(element))", String.valueOf(BigDecimal.valueOf(element)));
if (SwitchOperator(element)) {
Log.d("是操作符", String.valueOf(element));
//從棧中彈出2個操作數(shù) num1 和 num2 。注意:后彈出的num2是第一操作數(shù),num1是第二操作數(shù) 。
//因為這里考慮的都是二元運算符,因此需要彈2個元素出來進(jìn)行運算。
try{
BigDecimal num1 = scalc.pop();
BigDecimal num2 = scalc.pop();
switch (element) {
case '+':
temp_value = num2.add(num1) ;
break;
case '-':
temp_value = num2.subtract(num1) ;
break;
case '*':
temp_value = num2.multiply(num1);
break;
case '/':
temp_value = num2.divide(num1) ;
break;
default:
temp_value = BigDecimal .valueOf(999);
break;
}
//將temp_value壓棧
TextView1("");
scalc.push(temp_value);
}catch (EmptyStackException e) {
TextView1("錯誤");
return BigDecimal .valueOf(-1);
}
//使用element代表的運算符完成 num2 和 num1 的計算,產(chǎn)生臨時結(jié)果 temp_value
} else {
BigDecimal bd = new BigDecimal(element1);
Log.d("bd", bd.toString()) ;
scalc.push(bd);
}
}Log.d("scalc",scalc.toString());
BigDecimal x=scalc.pop();
Log.d("x",x.toString());
return x.stripTrailingZeros();
}
我的這個程序BigDecimal 為什么沒有用科學(xué)計數(shù)法顯示 3588966822845?
30秒到達(dá)戰(zhàn)場
2019-01-17 19:49:46