Get the values of a calculator and use the logical operators

0

Hello sorry, it is very easy to ask my question but I can not solve it, I have a calculator that tries to do the basic operations but, I do not get the second value to perform the operation, I do not understand very well because. Next the code.

public TextView calText;
    public double value1, value2, ans;
    int operation;


     @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_constraint);
        calText=findViewById(R.id.cal_text);

     }

    public void bt0(View v) {
        String cap=calText.getText().toString();
        cap=cap+"0";
        calText.setText(cap);
    }

     public void bt1(View v) {
         String cap=calText.getText().toString();
         cap=cap+"1";
         calText.setText(cap);
     }
    public void bt2(View v) {
        String cap=calText.getText().toString();
        cap=cap+"2";
        calText.setText(cap);
    }
    public void bt3(View v) {
        String cap=calText.getText().toString();
        cap=cap+"3";
        calText.setText(cap);
    }
    public void bt4(View v) {
        String cap=calText.getText().toString();
        cap=cap+"4";
        calText.setText(cap);
    }
    public void bt5(View v) {
        String cap=calText.getText().toString();
        cap=cap+"5";
        calText.setText(cap);
    }
    public void bt6(View v) {
        String cap=calText.getText().toString();
        cap=cap+"6";
        calText.setText(cap);
    }
    public void bt7(View v) {
        String cap=calText.getText().toString();
        cap=cap+"7";
        calText.setText(cap);
    }
    public void bt8(View v) {
        String cap=calText.getText().toString();
        cap=cap+"8";
        calText.setText(cap);
    }
    public void bt9(View v) {
        String cap=calText.getText().toString();
        cap=cap+"9";
        calText.setText(cap);
    }
    public void bt_decimal(View v) {
        String cap=calText.getText().toString();
        cap=cap+",";
        calText.setText(cap);
    }
    public void sum (View v) {
        try {
            String aux1=calText.getText().toString();
            value1=Double.parseDouble(aux1);
        }catch (NumberFormatException nfe){
            calText.setText("");
            operation=1;
        }
    }
    public void rest (View v) {
        try {
            String aux1=calText.getText().toString();
            value1=Double.parseDouble(aux1);
        }catch (NumberFormatException nfe){
            calText.setText("");
            operation=2;
        }
    }
    public void mutliplication (View v) {
        try {
            String aux1=calText.getText().toString();
            value1=Double.parseDouble(aux1);
        }catch (NumberFormatException nfe){
            calText.setText("");
            operation=3;
        }
    }
    public void division (View v) {
        try {
            String aux1=calText.getText().toString();
            value1=Double.parseDouble(aux1);
        }catch (NumberFormatException nfe){
            calText.setText("");
            operation=4;
        }
    }
    public void percent (View v) {
        try {
            String aux1=calText.getText().toString();
            value1=Double.parseDouble(aux1);
        }catch (NumberFormatException nfe){
            calText.setText("");
            operation=5;
        }
    }
    public void equals (View v) {
        try {
            String aux2 = calText.getText().toString();
            value2 = Double.parseDouble(aux2);
        } catch (NumberFormatException nfe) {}
        calText.setText("");

        if (operation==1) {
            ans=value1+value2;

            }else if (operation==2) {
                ans=value1-value2;

            }else if (operation==3){
                ans=value1*value2;

            }else if (operation==4){
                if(value2==0){
                    calText.setText(R.string.error);
                }else {
                ans=value1/value2;

                }
            }else if (operation==5) {
                ans=value2*(value1*100.0);
            }
            calText.setText("" + ans);
            value1=ans;
     }

     public void clear (View v) {
         calText.setText("");
         value1=0;
         value2=0;
         ans=0;
    }

    public void erase (View v) {
         if (!calText.getText().toString().equals("")){
             calText.setText(calText.getText().subSequence(0, calText.getText().length()-1));
         }

    }

}
    
asked by Jhon 01.11.2018 в 21:09
source

0 answers