2
I’m having trouble implementing multiplication and division in my calculator with graphical interface.
Regardless of anything I put using any of the operators (/ and *), the output result is 0.
Sum and subtraction are functioning normally.
package pkgCalculadora;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Calculadora extends JFrame implements ActionListener{
private JButton butSomar,butSubtrair, butMultiplicar, butDividir, butIgual;
private JButton but0,but1,but2,but3,but4,but5,but6,but7,but8,but9;
private JTextField campo1;
private JPanel painel1 = new JPanel();
private JPanel painel2 = new JPanel();
private JPanel painel3 = new JPanel();
private int leitura;
private int memoria;
private int op;
public Calculadora()
{
super("Calculadora");
butSomar = new JButton("+");
butSubtrair = new JButton(" -");
butMultiplicar = new JButton("*");
butDividir = new JButton(" /");
butIgual = new JButton("=");
but0 = new JButton("0");
but1 = new JButton("1");
but2 = new JButton("2");
but3 = new JButton("3");
but4 = new JButton("4");
but5 = new JButton("5");
but6 = new JButton("6");
but7 = new JButton("7");
but8 = new JButton("8");
but9 = new JButton("9");
campo1 = new JTextField(15);
painel1.setLayout(new FlowLayout());
painel2.setLayout(new BoxLayout(painel2, BoxLayout.Y_AXIS));
painel3.setLayout(new FlowLayout());
painel1.add(campo1);
painel2.add(butSomar);
painel2.add(butSubtrair);
painel2.add(butMultiplicar);
painel2.add(butDividir);
painel2.add(butIgual);
painel3.add(but0);
painel3.add(but1);
painel3.add(but2);
painel3.add(but3);
painel3.add(but4);
painel3.add(but5);
painel3.add(but6);
painel3.add(but7);
painel3.add(but8);
painel3.add(but9);
painel3.add(but0);
butSomar.addActionListener(this);
butSubtrair.addActionListener(this);
butMultiplicar.addActionListener(this);
butDividir.addActionListener(this);
butIgual.addActionListener(this);
but0.addActionListener(this);
but1.addActionListener(this);
but2.addActionListener(this);
but3.addActionListener(this);
but4.addActionListener(this);
but5.addActionListener(this);
but6.addActionListener(this);
but7.addActionListener(this);
but8.addActionListener(this);
but9.addActionListener(this);
add(painel1, BorderLayout.NORTH);
add(painel2, BorderLayout.EAST);
add(painel3, BorderLayout.CENTER);
// certo
// <HARD 1>
butSomar.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
campo1.setText(campo1.getText() + "+");
}
});
butSubtrair.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
campo1.setText(campo1.getText() + "-");
}
});
butMultiplicar.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
campo1.setText(campo1.getText() + "*");
}
});
butDividir.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
campo1.setText(campo1.getText() + "/");
}
});
but1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
leitura += 1;
campo1.setText(campo1.getText() + "1");
}
});
but2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
leitura += 2;
campo1.setText(campo1.getText() + "2");
}
});
but3.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
leitura += 3;
campo1.setText(campo1.getText() + "3");
}
});
but4.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
leitura += 4;
campo1.setText(campo1.getText() + "4");
}
});
but5.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
leitura += 5;
campo1.setText(campo1.getText() + "5");
}
});
but6.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
leitura += 6;
campo1.setText(campo1.getText() + "6");
}
});
but7.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
leitura += 7;
campo1.setText(campo1.getText() + "7");
}
});
but8.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
leitura += 8;
campo1.setText(campo1.getText() + "8");
}
});
but9.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
leitura += 9;
campo1.setText(campo1.getText() + "9");
}
});
but0.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
leitura += 0;
campo1.setText(campo1.getText() + "0");
}
});
// </HARD 1>
// <HARD 2>
butSomar.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
op = '+';
memoria += leitura; // guarda leitura na memoria
leitura = 0; // reseta memoria
}
});
butSubtrair.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
op = '-';
memoria -= leitura;
leitura = 0;
}
});
butMultiplicar.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
op = '*';
memoria *= leitura;
leitura = 0;
}
});
butDividir.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
op = '/';
memoria /= leitura;
leitura = 0;
}
});
butIgual.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
switch (op) {
case '+': {
memoria += leitura;
break;
}
case '-': {
memoria -= leitura;
break;
}
case '*': {
memoria *= leitura;
break;
}
case '/': {
memoria /= leitura;
break;
}
}
leitura = 0;
campo1.setText("" + memoria);
}
});
// </HARD 2>
}
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
}
}
In multiplication this is happening because you assign 0 to read and after that multiply it with memory. But I couldn’t identify why this happens to the division.
– pmargreff
but reading naturally gets 0, because its value is passed to variable 'memory' .-.
– RenanCn
Because you use
int
instead ofdouble
?– Victor Stafusa