如何在Android计算器中获得±的正确功能?

时间:2014-08-22 14:30:13

标签: java android calculator

我正在开发一个Android Calculator应用程序。我发现我的计算器显示:

6 + (-5) = -10 instead of 6 + (-5) = 1
6 − (-5) = 0 instead of 6 − (-5) = 11
6 × (-5) = 25 instead of 6 × (-5) = -30
6 ÷ (-5) = 1 instead of 6 ÷ (-5) = -1.2
6 % (-5) = 0.25 instead of 6 % (-5) = -0.3

(-5) + 6 = 1
(-5) − 6 = -11
(-5) × 6 = -30
(-5) ÷ 6 = -0.8333333333333334
(-5) % 6 = -0.3

当我使用±作为第一个数字时它工作正常但是当我将它用于第二个数字时,它显示错误的答案。所有数字都会出现,而不仅仅是65。我正在使用Eclipse制作这个应用程序。

我的问题是"我如何解决这个问题?"即; 6 + (-5) = 1; 6 − (-5) = 11; 6 × (-5) = -30; 6 ÷ (-5) = -1.2; 6 % (-5) = -0.3等.....所有其他数字。

这是我使用的代码:

ImageButton btnPlus;
ImageButton btnMin;
ImageButton btnMul;
ImageButton btnDiv;
ImageButton btnPercent;
ImageButton btnPlusmin;
ImageButton btnEqual;
TextView txtDisplay;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    btnPlus = (ImageButton) findViewById(R.id.btnPlus);
    btnMin = (ImageButton) findViewById(R.id.btnMin);
    btnMul = (ImageButton) findViewById(R.id.btnMul);
    btnDiv = (ImageButton) findViewById(R.id.btnDiv);
    btnPercent = (ImageButton) findViewById(R.id.btnPercent);
    btnPlusmin = (ImageButton) findViewById(R.id.btnPlusmin);
    btnEqual = (ImageButton) findViewById(R.id.btnEqual);
    txtDisplay = (TextView) findViewById(R.id.txtDisplay);

    btnPlus.setOnClickListener(this);
    btnMin.setOnClickListener(this);
    btnMul.setOnClickListener(this);
    btnDiv.setOnClickListener(this);
    btnPercent.setOnClickListener(this);
    btnPlusmin.setOnClickListener(this);
    btnEqual.setOnClickListener(this);

}

int clear_flag = 0;
String sign_flag = "";
Double total = 0.0;
int last_button = 0;

public void showsign(String sign) {
    if (last_button == R.id.btnPlus || last_button == R.id.btnMin || last_button == R.id.btnMul || last_button == R.id.btnDiv) {
    }
    else {
        clear_flag = 1;
        Double newNumber = Double.parseDouble(txtDisplay.getText().toString());
        if (sign_flag == "" || sign_flag == "=") {
            total = newNumber;
            txtDisplay.setText(total.toString());
        }
        else if (sign_flag == "+") {
            total = total + newNumber;
            txtDisplay.setText(total.toString());
        }
        else if (sign_flag == "-") {
            total = total - newNumber;
            txtDisplay.setText(total.toString());
        }
        else if (sign_flag == "*") {
            total = total * newNumber;
            txtDisplay.setText(total.toString());
        }
        else if (sign_flag == "/") {
            total = total / newNumber;
            txtDisplay.setText(total.toString());
        }
    }

    sign_flag = sign;

}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub

    if (v.getId() == R.id.btnPlus) {
        showsign("+");
    }
    else if (v.getId() == R.id.btnMin) {
        showsign("-");
    }
    else if (v.getId() == R.id.btnMul) {
        showsign("*");
    }
    else if (v.getId() == R.id.btnDiv) {
        showsign("/");
    }
    else if (v.getId() == R.id.btnEqual) {
        Double newNumber = Double.parseDouble(txtDisplay.getText().toString());
        if (sign_flag == "+") {
            total = total + newNumber;
            txtDisplay.setText(total.toString());
        }
        else if (sign_flag == "-") {
            total = total - newNumber;
            txtDisplay.setText(total.toString());
        }
        else if (sign_flag == "*") {
            total = total * newNumber;
            txtDisplay.setText(total.toString());
        }
        else if (sign_flag == "/") {
            total = total / newNumber;
            txtDisplay.setText(total.toString());
        }
        else if (sign_flag == "%") {
            total = (total * newNumber)/100;
            txtDisplay.setText(total.toString());
        }

        sign_flag = "=";

    }
    else if (v.getId() == R.id.btnPlusmin) {
        Double newNumber = Double.parseDouble(txtDisplay.getText().toString());
        total = newNumber * (-1);
        txtDisplay.setText(total.toString());
    }
    else if (v.getId() == R.id.btnPercent) {
        sign_flag = "%";
        clear_flag = 1;
        Double newNumber = Double.parseDouble(txtDisplay.getText().toString());
        total = newNumber;
    }
}

2 个答案:

答案 0 :(得分:2)

进行字符串比较时,你真的需要做sign_flag.equals(“+”)而不是'==',因为这只会检查地址的位置。字符串比较不以这种方式工作。可能会有更多,但从那开始。

修改

数学逻辑表示这些方程中的第一个数字乘以(-1)然后加上一个数字。这就是模式。因此,应该遵循以确定计算出错的地方。

是什么意思:

6 +( - 5)!= -10但((6 * -1)+ 1)+( - 5)= -10

6 - (-5)!= 0但((6 * -1)+ 1) - ( - 5)= 0

6×( - 5)!= 25但((6 * -1)+ 1)x(-5)= 25

这就是我所看到的模式正在计算中。

答案 1 :(得分:0)

非常感谢。

通过更改

修改我的代码
else if (v.getId() == R.id.btnPlusmin) {
    Double newNumber = Double.parseDouble(txtDisplay.getText().toString());
    total = newNumber * (-1);
    txtDisplay.setText(total.toString());
}

else if (v.getId() == R.id.btnPlusmin) {
    Double newNumber = Double.parseDouble(txtDisplay.getText().toString());
    subtotal = newNumber * (-1);
    txtDisplay.setText(subtotal.toString());
}

并添加

Double subtotal = 0.0;

int clear_flag = 0;
String sign_flag = "";
Double total = 0.0;
int last_button = 0;

解决了我的问题。

所以,我的新代码是:

ImageButton btnPlus;
ImageButton btnMin;
ImageButton btnMul;
ImageButton btnDiv;
ImageButton btnPercent;
ImageButton btnPlusmin;
ImageButton btnEqual;
TextView txtDisplay;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    btnPlus = (ImageButton) findViewById(R.id.btnPlus);
    btnMin = (ImageButton) findViewById(R.id.btnMin);
    btnMul = (ImageButton) findViewById(R.id.btnMul);
    btnDiv = (ImageButton) findViewById(R.id.btnDiv);
    btnPercent = (ImageButton) findViewById(R.id.btnPercent);
    btnPlusmin = (ImageButton) findViewById(R.id.btnPlusmin);
    btnEqual = (ImageButton) findViewById(R.id.btnEqual);
    txtDisplay = (TextView) findViewById(R.id.txtDisplay);

    btnPlus.setOnClickListener(this);
    btnMin.setOnClickListener(this);
    btnMul.setOnClickListener(this);
    btnDiv.setOnClickListener(this);
    btnPercent.setOnClickListener(this);
    btnPlusmin.setOnClickListener(this);
    btnEqual.setOnClickListener(this);

}

int clear_flag = 0;
String sign_flag = "";
Double total = 0.0;
Double subtotal = 0.0;
int last_button = 0;

public void showsign(String sign) {
    if (last_button == R.id.btnPlus || last_button == R.id.btnMin || last_button == R.id.btnMul || last_button == R.id.btnDiv) {
    }
    else {
        clear_flag = 1;
        Double newNumber = Double.parseDouble(txtDisplay.getText().toString());
        if (sign_flag == "" || sign_flag == "=") {
            total = newNumber;
            txtDisplay.setText(total.toString());
        }
        else if (sign_flag == "+") {
            total = total + newNumber;
            txtDisplay.setText(total.toString());
        }
        else if (sign_flag == "-") {
            total = total - newNumber;
            txtDisplay.setText(total.toString());
        }
        else if (sign_flag == "*") {
            total = total * newNumber;
            txtDisplay.setText(total.toString());
        }
        else if (sign_flag == "/") {
            total = total / newNumber;
            txtDisplay.setText(total.toString());
        }
    }

    sign_flag = sign;

}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub

    if (v.getId() == R.id.btnPlus) {
        showsign("+");
    }
    else if (v.getId() == R.id.btnMin) {
        showsign("-");
    }
    else if (v.getId() == R.id.btnMul) {
        showsign("*");
    }
    else if (v.getId() == R.id.btnDiv) {
        showsign("/");
    }
    else if (v.getId() == R.id.btnEqual) {
        Double newNumber = Double.parseDouble(txtDisplay.getText().toString());
        if (sign_flag == "+") {
            total = total + newNumber;
            txtDisplay.setText(total.toString());
        }
        else if (sign_flag == "-") {
            total = total - newNumber;
            txtDisplay.setText(total.toString());
        }
        else if (sign_flag == "*") {
            total = total * newNumber;
            txtDisplay.setText(total.toString());
        }
        else if (sign_flag == "/") {
            total = total / newNumber;
            txtDisplay.setText(total.toString());
        }
        else if (sign_flag == "%") {
            total = (total * newNumber)/100;
            txtDisplay.setText(total.toString());
        }

        sign_flag = "=";

    }
    else if (v.getId() == R.id.btnPlusmin) {
        Double newNumber = Double.parseDouble(txtDisplay.getText().toString());
        subtotal = newNumber * (-1);
        txtDisplay.setText(subtotal.toString());
    }
    else if (v.getId() == R.id.btnPercent) {
        sign_flag = "%";
        clear_flag = 1;
        Double newNumber = Double.parseDouble(txtDisplay.getText().toString());
        total = newNumber;
    }
}