It is made exactly according to the book of Fine, but during operations the number adds / takes itself away or divides / multiplies by itself. I understand why it works incorrectly, but I do not know how to fix it. The code is from the working class (the graphic is registered separately and just calls these commands):
import javax.swing.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class Dwizhok implements ActionListener { Main parent; char selectedAction =' '; double currentResult = 0; Dwizhok(Main parent){ this.parent = parent; } public void actionPerformed(ActionEvent e) { JButton clickButton = (JButton) e.getSource(); String dispFieldText = parent.okno.getText(); double displayValue = 0; if (!"".equals(dispFieldText)) { displayValue = Double.parseDouble(dispFieldText); } Object src = e.getSource(); if (src == parent.butPlus) { selectedAction = '+'; currentResult = displayValue; parent.okno.setText(""); } else if (src == parent.butMinus) { selectedAction = '-'; currentResult = displayValue; parent.okno.setText(""); } else if (src == parent.butUmnozh) { selectedAction = '*'; currentResult = displayValue; parent.okno.setText(""); } else if (src == parent.butPodel) { selectedAction = '/'; currentResult = displayValue; parent.okno.setText(""); } else if (src == parent.butRawno); if (selectedAction == '+') { currentResult += displayValue; parent.okno.setText("" + currentResult); } else if (selectedAction == '-') { currentResult -= displayValue; parent.okno.setText("" + currentResult); } else if (selectedAction == '*') { currentResult *= displayValue; parent.okno.setText("" + currentResult); } else if (selectedAction == '/') { currentResult /= displayValue; parent.okno.setText("" + currentResult); } else { String clickedButtonLabel = clickButton.getText(); parent.okno.setText(dispFieldText + clickedButtonLabel); } } }
currentResult = displayValue;they equalize instead of storing the already known value and the value just entered; Remove these lines according to the idea should be as it should - JVic