Calculator Androistudio

Asked

Viewed 318 times

0

Good night I am creating an application using 5 "boxes" (EditText) where he could multiply the boxes he wanted.

Example: value1 * value2= resu.... value1*value2*value3=resu.... value1*value2*va loodd3*value4=res u.... value1*value odd2*valoodd3*value4*value5=resu

As in the following image:

inserir a descrição da imagem aqui

Code:

public void somar (View v){

        EditText odd1 = (EditText) findViewById(R.id.editText7);
        String stringodd1 = odd1.getText().toString();

        EditText odd2 = (EditText) findViewById(R.id.editText8);
        String stringodd2 = odd2.getText().toString();


        EditText montante = (EditText) findViewById(R.id.editText12);
        String stringmontante = montante.getText().toString();


        NumberFormat formatter = new DecimalFormat("#0.00");

        if (stringodd1.trim().isEmpty() || stringodd2.trim().isEmpty()|| stringmontante.trim().isEmpty() )
        {
            Toast.makeText(getApplicationContext(), "Campos em branco",
                    Toast.LENGTH_LONG).show();
        }
        else
        {

            double valorodd1 = Double.parseDouble(stringodd1);
            double valorodd2 = Double.parseDouble(stringodd2);
            double valormontante = Double.parseDouble(stringmontante);


            double resu = valorodd1 * valorodd2 * valormontante;


            TextView resultado = (TextView) findViewById(R.id.textView15);
            resultado.setText (formatter.format(resu) + "€" );
  • 1

    What question do you have or difficulty finding? Take advantage of and add the code you are using in the application that focuses on your doubt/difficulty.

  • I have this code with a simple sum in box 1 and 2. My goal is to be able to calculate the box 1 x 2 = total, or box 1 x 2 x 3 = total or box 1 x 6 = total. Thank you

  • Do you have any button that when you click it does the operations ?

  • Do N successive multiplications ? And would be put where ? There are N TextViews for these results ?

  • Yes, as shown in my code above I have the button to calculate the multiplications. The error in my code is that I have 6 Edittext to enter 1 number and 1 textview to display the result, when I want to multiply 2 numbers does not give, because I am obliged to fill the remaining "boxes" with numbers to do the multiplication and I want to be able to use the "boxes" as you like. If I want to multiply 2 number I put a number in one box and another number in the other box, if I want to multiply 3 step by the same 1 number in each box and multiply. I hope you understand. Thank you

1 answer

0

The best thing for what you want would be to use a array for the EditText and a loop/loop to traverse and perform the multiplications if they contain values.

Following this idea, would be like this:

public void somar (View v){

    //cria um array com os edits todos
    EditText[] edits = new EditText[] {
        (EditText) findViewById(R.id.editText7),
        (EditText) findViewById(R.id.editText8),
        (EditText) findViewById(R.id.editText9),
        (EditText) findViewById(R.id.editText10),
        (EditText) findViewById(R.id.editText11),
        (EditText) findViewById(R.id.editText12) //12 é o montante
    }

    TextView textResultado = (TextView) findViewById(R.id.textView15);

    double resultado = 1;
    boolean algumPreenchido = false;

    //aqui percorre agora todos os edits, sejam eles quantos forem
    for (int i = 0; i < edits.length; ++i){

        //aqui vê se o edit foi preenchido, e só multiplica se tiver valor
        if (!edits[i].getText().toString().trim().isEmpty()){
            resultado *= Double.parseDouble(edits[i].getText().toString());
            algumPreenchido = true;
        }
    }

    NumberFormat formatter = new DecimalFormat("#0.00");

    //Mostra o resultado caso tenha sido considerado algum valor, ou 0 caso contrário
    textResultado.setText (formatter.format(algumPreenchido ? resu : 0) + "€" );
}

Note that the program will fail to calculate if any of the EditText has entered values that are not numbers. In this situation you have to add exceptions catch with try catch and give the treatment it deems appropriate.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.