Operator Based Calculator Using Swing Package
import javax.swing.JOptionPane;
class OperatorCalc{
public static void main(String[]args){
String operator = JOptionPane.showInputDialog("Enter any Operator" );
String op = operator;
String v1 = JOptionPane.showInputDialog("Enter First Value" );
int value1 = Integer.parseInt(v1);
String v2 = JOptionPane.showInputDialog("Enter Second Value" );
int value2 = Integer.parseInt(v2);
if (!(op == "+" || op == "-" || op == "*" || op == "/" || op == "%" ))
JOptionPane.showMessageDialog(null, " Enter a valid Operator");
if( op == "+" )
JOptionPane.showMessageDialog(null, " Addition is " + (value1 + value2) );
if( op == "-" )
JOptionPane.showMessageDialog(null, " Subtraction " + (value1 - value2) );
if( op == "*" )
JOptionPane.showMessageDialog(null, " Multiplication is " + (value1 * value2) );
if( op == "/" )
JOptionPane.showMessageDialog(null, " Division is " + (value1 / value2) );
if( op == "%" )
JOptionPane.showMessageDialog(null, " Reminder is " + (value1 % value2) );
}
}
No comments:
Post a Comment