Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Calculator Program

  1. #1
    Member
    Join Date
    Oct 2011
    Posts
    40
    My Mood
    Sneaky
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default Calculator Program

    // Practical 7A (Revision) - Q1
    // Marcus Ward
    // 26/10/2011
    /* Program will allow user to enter 2 values and an operator.
    Using a switch statement the program will display the result on screen. */

    import java.util.Scanner;

    public class CalculatorProgram
    {
    public static void main(String[] args)
    {
    Scanner keyboardIn = new Scanner(System.in);

    double num1, num2; // declare variables
    char operator;

    System.out.println("Enter 1st value"); // get user input
    num1 = keyboardIn.nextDouble();
    System.out.println("Enter operator");
    operator = keyboardIn.next.charAt(0);
    System.out.println("Enter 2nd value");
    num2 = keyboardIn.nextDouble();

    switch (operator) { // create calculator and display ouput

    case '+':
    System.out.println(num1 + num2);
    break;
    case '-':
    System.out.println(num1 - num2);
    break;
    case '*':
    System.out.println(num1 * num2);
    break;
    default:
    System.out.println(num1 / num2);
    break;
    }

    }
    }
    Last edited by mwardjava92; November 3rd, 2011 at 04:06 PM.


  2. #2
    Member
    Join Date
    Mar 2011
    Posts
    198
    My Mood
    Daring
    Thanks
    7
    Thanked 4 Times in 4 Posts

    Default Re: Calculator Program

    Please view: The switch Statement (The Java™ Tutorials > Learning the Java Language > Language Basics)

    To learn how to correctly use the switch function in java.

    this better kevin ?

  3. The Following User Says Thank You to macko For This Useful Post:

    mwardjava92 (November 3rd, 2011)

  4. #3
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,462
    My Mood
    Hungover
    Thanks
    144
    Thanked 639 Times in 547 Posts

    Default Re: Calculator Program

    Quote Originally Posted by macko View Post
    Please view: The switch Statement (The Java™ Tutorials > Learning the Java Language > Language Basics)

    To learn how to correctly use the switch function in java.

    this better kevin ?
    Much, thanks!

    Also, chars are surrounded by ' '. Otherwise Java won't know it's a char and will try to parse it as Java code!
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. The Following User Says Thank You to KevinWorkman For This Useful Post:

    mwardjava92 (November 3rd, 2011)

Similar Threads

  1. Calculator Using AWT
    By Allicat in forum What's Wrong With My Code?
    Replies: 7
    Last Post: May 17th, 2011, 06:49 AM
  2. Calculator
    By javapenguin in forum What's Wrong With My Code?
    Replies: 5
    Last Post: December 22nd, 2010, 08:00 AM
  3. My Algebraic Calculator Program.
    By crazed8s in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 30th, 2010, 02:07 AM
  4. [SOLVED] Calculator help
    By Bradshjo in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 1st, 2010, 04:27 PM
  5. Calculator help.
    By Skinnyskinny in forum Java Theory & Questions
    Replies: 6
    Last Post: August 1st, 2009, 12:34 PM