Results 1 to 5 of 5
  1. #1
    johnWeldon is offline Member
    Join Date
    Feb 2013
    Posts
    9
    Rep Power
    0

    Default Help with a beginning program

    So I'm trying to just get used to java...and based on what I see we're coming up on next in lecture is just using the operators...so I'm trying to make a basic calculator program that add,sub,div,and mult 2 numbers.

    So here's my code...it does compile and run...however when I want to divide, multiply, or subtract...and hit the corresponding s,d,or m...It will only do addition....but the thing is...it does what addition tells it to do...and instead of ending...it goes straight into subtraction.....then multiplication....then division, like there's no stop to the program until all 4 operations finish running. Where am I going wrong?

    package egr118home;
    import java.util.*;
    public class Experimenting {

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

    String decision;
    System.out.println("What Operation would you like to do? \n"
    + "A for Addition, " + "S for Subtraction, " +
    "M for Multiplication," + " D for Division: ");

    decision = input.next();

    if (decision.equalsIgnoreCase("a"));

    {
    Addition();
    }

    if (decision.equalsIgnoreCase("s"));
    {
    Subtraction();
    }

    if (decision.equalsIgnoreCase("m"));
    {
    Multiplication();
    }

    if (decision.equalsIgnoreCase("d"));
    {
    Division();
    }
    }

    private static void Addition() {

    double aInput1;
    double aInput2;
    double aAnswer;
    Scanner input = new Scanner(System.in);
    System.out.println("Enter First Number to be Added: ");
    aInput1 = input.nextDouble();
    System.out.println("Enter Second Number to be Added: ");
    aInput2 = input.nextDouble();
    aAnswer = aInput1 + aInput2;
    System.out.println("Your answer is: " + aAnswer);

    }

    private static void Subtraction() {
    double sInput1;
    double sInput2;
    double sAnswer;
    Scanner input = new Scanner(System.in);
    System.out.println("Enter First Number to be Subtracted: ");
    sInput1 = input.nextDouble();
    System.out.println("Enter Second Number to be Subtracted: ");
    sInput2 = input.nextDouble();
    sAnswer = sInput1 - sInput2;
    System.out.println("Your answer is: " + sAnswer);

    }

    private static void Multiplication() {
    double mInput1;
    double mInput2;
    double mAnswer;
    Scanner input = new Scanner(System.in);
    System.out.println("Enter First Number to be Multiplied: ");
    mInput1 = input.nextDouble();
    System.out.println("Enter Second Number to be Multiplied: ");
    mInput2 = input.nextDouble();
    mAnswer = mInput1 * mInput2;
    System.out.println("Your answer is: " + mAnswer);

    }

    private static void Division() {
    double dInput1;
    double dInput2;
    double dAnswer;
    Scanner input = new Scanner(System.in);
    System.out.println("Enter First Number to be Divided: ");
    dInput1 = input.nextDouble();
    System.out.println("Enter Second Number to be Divided: ");
    dInput2 = input.nextDouble();
    dAnswer = dInput1 / dInput2;
    System.out.println("Your answer is: " + dAnswer);

    }
    }


    Can anyone help me out and point out where I went wrong?

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,242
    Rep Power
    19

    Default Re: Help with a beginning program

    Please go through the Forum Rules, particularly the third paragraph. Then go through Guide For New Members and BB Code List - Java Programming Forum - Learn Java Programming and edit your post accordingly.

    To edit the subject line, click 'Go Advanced' after 'Edit Post'

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,242
    Rep Power
    19

    Default Re: Help with a beginning program

    Read up here on how to use if statements: The if-then and if-then-else Statements (The Java™ Tutorials > Learning the Java Language > Language Basics)


    Particularly note where there are, and aren't, semicolons.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    foulkelore is offline Member
    Join Date
    Dec 2012
    Location
    Des Moines, IA
    Posts
    35
    Rep Power
    0

    Default Re: Help with a beginning program

    Darryl is right, check out the if statement tutorials. for sure they need a little work and Welcome!

  5. #5
    Aries. is offline Member
    Join Date
    Jan 2013
    Posts
    22
    Rep Power
    0

    Default Re: Help with a beginning program

    Just wondering why is it you use some class, I have only 73 lines for my calculator so means short no hassle.

    Your condition should be if > else if > else if > else goes on.

    Actually you can put the formula inside the conditional statement.

Similar Threads

  1. Beginning with Android
    By Dark in forum Android
    Replies: 10
    Last Post: 12-21-2011, 01:53 PM
  2. Beginning J2ee
    By vinci123 in forum Advanced Java
    Replies: 1
    Last Post: 11-21-2011, 09:56 AM
  3. Beginning advanced java
    By vinci123 in forum Advanced Java
    Replies: 7
    Last Post: 09-14-2011, 07:25 AM
  4. Adding something to beginning of JTextArea
    By Huskies in forum AWT / Swing
    Replies: 4
    Last Post: 08-30-2011, 12:01 AM
  5. beginning - IF
    By sparkling in forum New To Java
    Replies: 26
    Last Post: 06-05-2011, 04:17 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •