Note that there are some explanatory texts on larger screens.

plurals
  1. POCalculator program in java
    text
    copied!<p>I am a beginner in java and I have written this code with the help of an online available program. The code attached is a part of the code which performs action when buttons of the calculator are pressed.In the CalculatorDemo class i have initialized all the buttons(b0-b24) and the TextField tf. In this class I am using char OP as a flag ,so that, when I press '+' the OP is assigned '+' and I have even checked it on command prompt. But when i press '=' ,the OP is automatically assigned '\0' and i don't know how and why. And hence, no operation is performed at all. And I don't understand where the logic is wrong. Please help </p> <pre><code> import java.awt.event.*; import java.awt.*; class CalculatorActionperform implements ActionListener { CalculatorDemo temp; public boolean foundKey; String command; String s="", s1,s2,s3,s4,s5,s6; int n; char OP; CalculatorActionperform(CalculatorDemo d) { temp=d; } public void actionPerformed(ActionEvent e) { if(e.getSource()==temp.b23) { s3=temp.tf.getText(); s4="0"; s5=s3+s4; temp.tf.setText(s5); } if(e.getSource()==temp.b22) { s3=temp.tf.getText(); s4="1"; s5=s3+s4; temp.tf.setText(s5); } if(e.getSource()==temp.b21) { s3=temp.tf.getText(); s4="2"; s5=s3+s4; temp.tf.setText(s5); } if(e.getSource()==temp.b20) { s3=temp.tf.getText(); s4="3"; s5=s3+s4; temp.tf.setText(s5); } if(e.getSource()==temp.b15) { s3=temp.tf.getText(); s4="4"; s5=s3+s4; temp.tf.setText(s5); } if(e.getSource()==temp.b16) { s3=temp.tf.getText(); s4="5"; s5=s3+s4; temp.tf.setText(s5); } if(e.getSource()==temp.b17) { s3=temp.tf.getText(); s4="6"; s5=s3+s4; temp.tf.setText(s5); } if(e.getSource()==temp.b12) { s3=temp.tf.getText(); s4="7"; s5=s3+s4; temp.tf.setText(s5); } if(e.getSource()==temp.b11) { s3=temp.tf.getText(); s4="8"; s5=s3+s4; temp.tf.setText(s5); } if(e.getSource()==temp.b10) { s3=temp.tf.getText(); s4="9"; s5=s3+s4; temp.tf.setText(s5); } if(e.getSource()==temp.b14) { s1=temp.tf.getText(); temp.tf.setText(""); OP='+'; } if(e.getSource()==temp.b19) { s1=temp.tf.getText(); temp.tf.setText(""); OP='-'; } if(e.getSource()==temp.b18) { s1=temp.tf.getText(); temp.tf.setText(""); OP='*'; } if(e.getSource()==temp.b13) { s1=temp.tf.getText(); temp.tf.setText(""); OP='/'; } if(e.getSource()==temp.b9) /* b9= '=' */ { s2=temp.tf.getText(); if(OP=='\0') { System.out.println("null"); } if(OP=='+') { n=Integer.parseInt(s1)+Integer.parseInt(s2); temp.tf.setText(String.valueOf(n)); } else if(OP=='-') { n=Integer.parseInt(s1)-Integer.parseInt(s2); temp.tf.setText(String.valueOf(n)); } else if(OP=='*') { n=Integer.parseInt(s1)*Integer.parseInt(s2); temp.tf.setText(String.valueOf(n)); } else if(OP=='/') { n=Integer.parseInt(s1)/Integer.parseInt(s2); temp.tf.setText(String.valueOf(n)); } } if(e.getSource()==temp.b6) { temp.tf.setText(""); } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload