Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
My first program: A calculator
#1
Code:
import java.util.Scanner;
public class calc
{
    public static void main(String[] args)
    {
        int x = 1;
        while (x == 1)
        {
            Scanner num = new Scanner(System.in);
            Scanner num2 = new Scanner(System.in);
            Scanner op = new Scanner(System.in);
            float userinput;
            float userinput2;
            int userinput3;
            System.out.print("Enter a Number: ");
            userinput = num.nextInt();
            System.out.print("Enter Another Number: ");
            userinput2 = num2.nextInt();
            System.out.println("1) Add");
            System.out.println("2) Subtract");
            System.out.println("3) Multiply");
            System.out.println("4) Divide");
            System.out.print("Enter an Operation: ");
            userinput3 = op.nextInt();
            if (userinput3 == 1)
            {         
                System.out.println(userinput + userinput2);
            }        
            else if (userinput3 == 2)
            {         
                System.out.println(userinput - userinput2);
            }
            else if (userinput3 == 3)
            {         
                System.out.println(userinput * userinput2);
            }
            else if (userinput3 == 4)
            {         
                System.out.println(userinput / userinput2);
            }
            else
            {
                System.out.println("Please enter a valid option...");
            }
        }
    }    
}
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  My first GUI: Adding Calculator nevets04 25 5,454 01-02-2012, 06:38 PM
Last Post: MineCrack
  The Alpha Calculator Mia 7 1,672 10-13-2009, 01:17 PM
Last Post: Mia
  Mortgage Calculator [Open source] Mia 3 2,817 10-10-2009, 06:50 PM
Last Post: Winner

Forum Jump:


Users browsing this thread: 1 Guest(s)