• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

calc.java

Amol S.

Platinum Member
import java.util.*;
import java.lang.System.*;
import java.awt.*;
import javax.swing.*;
public class calc
{
private static byte mode;
private static double num1,num2;
public static void main(String[] args)
{
Scanner keyboard=new Scanner(System.in);
out.print("Enter the first number. : ");
num1=keyboard.nextDouble();
out.print("Enter the second num. : ");
num2=keyboard.nextDouble();
setMode();
if(getMode()==1)
{
out.println(num1+num2);
}
else if(getMode==2)
{
out.println(num1-num2);
}
else if(getMode==3)
{
out.println(num1*num2);
}
else
{
out.println(num1/num2);
}
}
protected static void setMode()
{
Scanner keyboard=new Scanner(System.in);
out.println("Enter 1 for addition. Enter 2 for subtraction. ");
out.print("Enter 3 for multiplication. Enter 4 for division. : ");
try
{
mode=keyboard.nextByte();
}
catch(Exception somexception)
{
JOptionPane.showMessageDialog(null,"Error. There was a errorso the program must now exit. ");
JOptionPane.showMessageDialog(null,"The program will exit by it self. ");
exit();
}
}
protected static byte getMode()
{
return mode;
}
}
 
Last edited by a moderator:
Back
Top