Calculator - operations with decimals

Asked

Viewed 1,155 times

3

Once again I show up with this calculator here, I’ve been helped a lot with it and it’s almost ready!

It’s working all operations perfectly, no mistakes whatsoever, except for one small detail... it can only do calculations like 9+8, 7*8, etc..

I can’t operate things like 18+30 that the outcome of 12! 25+52 the result of 14, and 23*2 the result of 10...

In some cases it seems that it sums up the decimal numbers and then operates by the next unit, in others it seems that it does something crazy!.

The code goes below.

import javax.swing.*;
import java.awt.event.*;

public class Calculadora extends JFrame {

/**
 * 
 */
private static final long serialVersionUID = 1L;


    private JButton botaoMais;
    private JButton botaoMenos;
    private JButton botaoVezes;
    private JButton botaoDividi;
    private JButton botaoIgual;
    private JButton n1;
    private JButton n2;
    private JButton n3;
    private JButton n4;
    private JButton n5;
    private JButton n6;
    private JButton n7;
    private JButton n8;
    private JButton n9;
    private JButton n0;
    private char operacao;
    private JTextField campo;
    private int memoria;
    private int leitura;

    public Calculadora (){
        this.setTitle("Calculadora");
        this.setBounds(0,0,280,350);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.getContentPane().setLayout(null);

        leitura = 0;
        memoria = 0;


    n1 = new JButton();
    n1.setText("1");
    n1.setBounds(25, 70, 45, 45);
    this.add(n1);

    n2 = new JButton();
    n2.setText ("2");
    n2.setBounds(85,70,45,45);
    this.add(n2);

    n3 = new JButton();
    n3.setText("3");
    n3.setBounds(140,70,45,45);
    this.add(n3);

    n4 = new JButton();
    n4.setText("4");
    n4.setBounds(25,125,45,45);
    this.add(n4);

    n5 = new JButton();
    n5.setText("5");
    n5.setBounds(85,125,45,45);
    this.add(n5);

    n6 = new JButton();
    n6.setText("6");
    n6.setBounds(140,125,45,45);
    this.add(n6);

    n7 = new JButton();
    n7.setText("7");
    n7.setBounds(25,180,45,45);
    this.add(n7);

    n8 = new JButton();
    n8.setText("8");
    n8.setBounds (85,180,45,45);
    this.add(n8);

    n9 = new JButton();
    n9.setText("9");
    n9.setBounds (140,180,45,45);
    this.add (n9);

    n0 = new JButton();
    n0.setText("0");
    n0.setBounds(140,235,45,45);
    this.add(n0);

    botaoIgual = new JButton();
    botaoIgual.setText ("=");
    botaoIgual.setBounds(25, 235, 106, 45);
    this.add(botaoIgual);

    botaoMais = new JButton();
    botaoMais.setText("+");
    botaoMais.setBounds(195, 70, 45, 45);
    this.add(botaoMais);

    botaoMenos = new JButton();
    botaoMenos.setText("-");
    botaoMenos.setBounds(195,125,45,45);
    this.add(botaoMenos);

    botaoVezes = new JButton();
    botaoVezes.setText("x");
    botaoVezes.setBounds(195,180,45,45);
    this.add(botaoVezes);

    botaoDividi = new JButton();
    botaoDividi.setText("/");
    botaoDividi.setBounds(195,235,45,45);
    this.add(botaoDividi);

    campo = new JTextField();
    campo.setBounds(25, 25, 215, 30);
    this.add(campo);

    n1.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent evt){
            leitura +=1;
            campo.setText(campo.getText()+ "1");


    }
  });
    n2.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent evt){
            leitura +=2;
            campo.setText(campo.getText()+ "2");
        }
    });
    n3.addActionListener(new ActionListener(){
        public void actionPerformed (ActionEvent evt){
            leitura +=3;
            campo.setText(campo.getText()+"3");
        }
    });
    n4.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent evt){
            leitura+=4;
            campo.setText(campo.getText()+ "4");
        }
    });
    n5.addActionListener(new ActionListener(){
        public void actionPerformed (ActionEvent evt){
            leitura+=5;
            campo.setText(campo.getText()+ "5");
        }
    });
    n6.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent evt){
            leitura+=6;
            campo.setText(campo.getText()+"6");
        }
    });
    n7.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent evt){
            leitura+=7;
            campo.setText(campo.getText()+ "7");

        }
    });
    n8.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent evt){
            leitura+=8;
            campo.setText(campo.getText()+"8");

        }
    });
    n9.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent evt){
            leitura+=9;
            campo.setText(campo.getText()+ "9");
        }
    });
    n0.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent evt){
            leitura+=0;
            campo.setText(campo.getText()+ "0");
        }
    });
    botaoMais.addActionListener(new ActionListener(){
        public void actionPerformed (ActionEvent evt){
            operacao = '+';
            if (memoria > 0){
                memoria += leitura;
            }else {
                memoria = leitura;
            }
            leitura = 0;
                    campo.setText("");
        }
    });
    botaoMenos.addActionListener (new ActionListener(){
        public void actionPerformed (ActionEvent evt){
            operacao = '-';
            if(memoria > 0){
                memoria -= leitura;
            }else {
                memoria = leitura;
            }
            leitura = 0;
            campo.setText("");
        }
    });
    botaoVezes.addActionListener (new ActionListener(){
        public void actionPerformed (ActionEvent evt){
            operacao = '*';
            if (memoria > 0){
                memoria *= leitura;
            }else {
                memoria = leitura;
            }
            leitura = 0;
            campo.setText("");
        }
    });
    botaoDividi.addActionListener (new ActionListener(){
        public void actionPerformed (ActionEvent evt){
            operacao = '/';
            if (memoria > 0){
                memoria /= leitura;
            }else {
                memoria = leitura;
            }
            leitura = 0;
            campo.setText("");
        }
    });
    botaoIgual.addActionListener(new ActionListener(){
        public void actionPerformed (ActionEvent evt){
            switch (operacao){
            case '+':{
                memoria +=leitura;
                break;
            }
            case '-':{
                memoria-=leitura;
                break;
            }
            case '*':{
                memoria *=leitura;
                break;
            }
            case '/':{
                memoria /=leitura;
                break;
            }
            }
            leitura = 0;
            campo.setText (""+ memoria);

        }
    });
}

public static void main(String[] args) {
    Calculadora exemplo = new Calculadora();
        exemplo.setVisible(true);
}
}

1 answer

7


First congratulations! You’ve come very close to a 100% functional implementation...

As I mentioned in your first question (Eclipse Java Calculator) there were still some bugs to be fixed. And one of them persisted...

THE PROBLEM

When you click a numeric button (1, 2, 3 etc.) you increment the variable leitura with the value corresponding to the button, certain?

As long as only 1 digit is entered, everything will work correctly...

However, when typing 2 digits, your calculator is not entering the value of tens correctly, and yes, just adding up, thus:

The operation 18 + 30 = 12 for (1 + 8) + (3 + 0) = 12

The operation 25 + 52 = 14 for (2 + 5) + (5 + 2) = 14

The operation 23 * 2 = 10 for (2 + 3) * 2 = 10

FIXING THE PROBLEM

Come está (button 1):

n1.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent evt){
        leitura +=1;
        campo.setText(campo.getText()+ "1");
}
});

//para 1 clique: leitura = 1;
//para 2 cliques: leitura = 2;
//para 3 cliques: leitura = 3;
...

As it should be (button 1):

n1.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent evt){
        leitura *= 10;  // faltou esse pequeno detalhe em seu código!
        leitura += 1;
        campo.setText(campo.getText()+ "1");
}
});

//para 1 clique: leitura = 1;
//para 2 cliques: leitura = 11;
//para 3 cliques: leitura = 111;
...

Make this adjustment on all numeric buttons and the problem will be fixed!

Any questions ask in the comments. I hope I’ve helped!

  • Calculator working beautifully ! Today in class when quoting this, I will have to quote your name ! kkkk thank you very much man, really ! Now it’s time to do and redo to decorate what each function is for, and try to create new programs with these functions ! Thank you very much !

  • kkkkk arrange! :)

Browser other questions tagged

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