Performing a calculation in Java gives completely wrong answer
By : pragnesh
Date : March 29 2020, 07:55 AM
|
Java-Calculator more than one calculation
By : Kenny Bui
Date : March 29 2020, 07:55 AM
I think the issue was by ths following , You are having problems here because you are not careful with your if and else statements and the logic of the program. Firstly, in the actionPerformed method of ButtonOperation, you have an if(e.getActionCommand().equals("-")) inside the if(e.getActionCommand().equals("+")). That doesn't make sense, does it? The commands you are testing for with those ifs are mutually exclusive, so an else if for the - operation is what you want. Then in the ButtonAusfuehren actionPerformed() method: again, think about what you want to be doing. You don't need the last else - you can always set the result field (ergebnisFeld) to the result. Also, the operations themselves wouldn't have worked as the second time an operation was done, one of the arguments (arg1 or arg2) would have been an empty string. If you take this into account, you get something like this, which, with the other changes, makes your calculator work: code :
if (operation == '+')
if (arg1.equals("")) {
ergebnis += new Integer(arg2);
} else {
ergebnis = new Integer(arg1) + new Integer(arg2);
}
else if (operation == '-') {
if (arg1.equals("")) {
ergebnis -= new Integer(arg2);
} else {
ergebnis = new Integer(arg1) - new Integer(arg2);
}
}
|
java performing array calculation
By : Alex
Date : March 29 2020, 07:55 AM
|
Performing an EC public key calculation for given secret key gives wrong results
By : Christian Weißer
Date : March 29 2020, 07:55 AM
|
Java GUI calculator wont do the calculation
By : InVid San
Date : March 29 2020, 07:55 AM
Hope that helps You have confused where to put ActionListeners - they belong on components like buttons where you expect an action, and ItemListeners which belong to the check boxes. Once you sort this out your code will work. This code will update the result if you press any of the operations, or the result button. Working code: code :
public class Calculator extends JFrame implements ActionListener, ItemListener {
JTextField numri1, numri2;
JCheckBox shuma, diferenca, shumezimi;
JButton veprimi, ok;
JTextField rezultati;
double n1, n2;
String n1tekst, n2tekst;
public Calculator() {
setLayout(new FlowLayout());
numri1 = new JTextField(10);
add(numri1);
numri2 = new JTextField(10);
add(numri2);
shuma = new JCheckBox("Shuma");
add(shuma);
shuma.addItemListener(this);
diferenca = new JCheckBox("Diferenca");
add(diferenca);
diferenca.addItemListener(this);
shumezimi = new JCheckBox("Shumezimi");
add(shumezimi);
shumezimi.addItemListener(this);
veprimi = new JButton("Kryej Veprimin");
add(veprimi);
veprimi.addActionListener(this);
rezultati = new JTextField(10);
add(rezultati);
ok = new JButton("OK");
add(ok);
}
public void itemStateChanged(ItemEvent e) {
n1 = Double.parseDouble(numri1.getText());
n2 = Double.parseDouble(numri2.getText());
if (e.getSource() == shuma) {
String rez = shuma.isSelected() ? "" + (n1 + n2) : "";
rezultati.setText(rez);
} else if (e.getSource() == diferenca) {
String rez = diferenca.isSelected() ? "" + (n1 - n2) : "";
rezultati.setText(rez);
} else if (e.getSource() == shumezimi) {
String rez = shumezimi.isSelected() ? "" + (n1 * n2) : "";
rezultati.setText(rez);
}
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == veprimi) {
n1 = Double.parseDouble(numri1.getText());
n2 = Double.parseDouble(numri2.getText());
if (shuma.isSelected()) {
rezultati.setText("" + (n1 + n2));
} else if (diferenca.isSelected()) {
rezultati.setText("" + (n1 - n2));
} else if (shumezimi.isSelected()) {
rezultati.setText("" + (n1 * n2));
}
}
}
public static void main(String args[])
{
Calculator ob = new Calculator();
ob.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ob.setSize(900, 900);
ob.setVisible(true);
}
}
public class Ngjyra extends JFrame implements ActionListener {
JCheckBox kuqe, blu;
JButton veprimi, ok;
public Ngjyra() {
setLayout(new FlowLayout());
kuqe = new JCheckBox("Kuqe");
add(kuqe);
blu = new JCheckBox("Blu");
add(blu);
veprimi = new JButton("Kryej Veprimin");
add(veprimi);
veprimi.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == veprimi) {
if (kuqe.isSelected()) {
this.getContentPane().setBackground(Color.RED);
} else if (blu.isSelected()) {
this.getContentPane().setBackground(Color.BLUE);
}
}
}
public static void main(String args[])
{
Ngjyra ob = new Ngjyra();
ob.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ob.setSize(900, 900);
ob.setVisible(true);
}
}
|