I created a calculator by netbeans, which is working if only one operation is done. But if the user clicks on =
(equal button) the result adds the first value entered.
Example:
I typed 1 + 2
I pressed =
I get 3
But if I click again on the =
button, I have result 4 instead of 5.
My code looks like this:
I've created these variables in the main class
public class TelaCalculadora extends javax.swing.JFrame {
double numA;
String opera;
double numB;
I created the class to add
public class soma {
double a;
double b;
double res;
public double somar()
{
res = a+b;
return res;
}
The action when a button is triggered
private void btn1ActionPerformed(java.awt.event.ActionEvent evt) {
String numeroACalcular;
numeroACalcular = txtVisor.getText() + btn1.getText();
txtVisor.setText(numeroACalcular);
}
When you click the "+" button
private void btnSomaActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
opera = "somar";
numA = Double.parseDouble(txtVisor.getText());
txtVisor.setText("");
and when you click "="
private void btnResultadoActionPerformed(java.awt.event.ActionEvent evt) {
numB = Double.parseDouble(txtVisor.getText());
if(opera == "somar")
{
soma resu = new soma();
resu.a = numA;
resu.b = numB;
resu.somar();
txtVisor.setText(Double.toString(resu.res));
}