#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    8
    Rep Power
    0

    Java calculator using swing and applet


    import java.io.*;
    import java.util.*;
    import javax.swing.JOptionPane;

    public class exercise1
    {

    public static void main(String args[]){
    String input = JOptionPane.showInputDialog("Selection from the choices below:\n\n 1. Addition\n 2. Subtraction\n 3. Multiplication\n 4. Division\n 5. Modulo\n\n Your choice?");


    JOptionPane.showInputDialog("\nEnter first number: ");
    float x = kbReader.nextFloat();
    JOptionPane.showInputDialog("Enter second number: ");
    float y = kbReader.nextFloat();


    Scanner kbReader = new Scanner(System.in);
    int choice = kbReader.nextInt();


    if((choice<=5) && (choice>0))
    {

    switch (choice)
    {

    case 1: //addition 1 decimal
    System.out.println(x + " plus " + y + " = " + (x + y) );
    break;

    case 2: //subtraction no decimal
    System.out.println(x + " minus " + y + " = " + (x - y) );
    break;

    case 3: //multiplication 3 decimal
    System.out.println(x + " times " + y + " = " + (x * y) );
    break;

    case 4: //division 4 decimal
    System.out.println(x + " divided by " + y + " = " + (x / y) );
    break;

    case 5: //modulo
    System.out.println(x + " modulo " + y + " = " + (x % y) );
    //break;
    }

    }
    else
    {
    System.out.println("Please enter a 1, 2, 3, 4 or 5.");
    }
    }
    }
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,714
    Rep Power
    348
    Please edit your post and wrap the code in code tags to make it easier to read:
    [code]
    ... the code here
    [/code]

    Do you have a question or problem?

IMN logo majestic logo threadwatch logo seochat tools logo