Results 1 to 5 of 5
  1. #1
    Join Date
    Dec 2011
    Posts
    3
    Rep Power
    0

    Default Need help with Calculator

    So some days ago I started to actually learn Java. I learned a few commands and syntax and decided to try my hand at making a calculator without following an online guide.

    Java Code:
    import java.util.Scanner;
    
    
    public class Calculator {
    	
    	public static void main(String[] args) {
    		
    		//Calculator
    		
    		//Final formula: firstvalue + secondvalue = answer
    		
    		
    		//Display message asking for first number.
    		System.out.println("Please input first value: ");
    		Scanner scan = new Scanner(System.in);
    		int firstvalue = scan.nextInt();
    		//Pick what mathematics function.
    		System.out.println("Type: \n '1' for Addition \n '2' for Subtraction \n '3' for Multiplication \n '4' for Division ");
    		//Based on what function you chose, the function will then be carried out. 
    		
    		//Addition.
    		int addition = scan.nextInt();
    		if (addition == 1) {
    			System.out.println("Addition selected: Input second number please: " + firstvalue + " + ?\n");
    			int secondvalue = scan.nextInt();
    			int answer = firstvalue + secondvalue;
    			System.out.println(firstvalue + " + " + secondvalue + " = " + answer);
    		}
    		
    		//Subtraction.
    		int subtraction = scan.nextInt(); 
    		if (subtraction == 2) {
    			System.out.println("Subtraction selected: Input second number please: " + firstvalue + " - ?\n");
    			int secondvalue = scan.nextInt();
    			int answer = firstvalue - secondvalue;
    			System.out.println(firstvalue + " - " + secondvalue + " = " + answer);
    		}
    		//Multiplication.
    		int multiplication = scan.nextInt();
    		if (multiplication == 3) {
    			System.out.println("Multiplication selected: Input second number please: " + firstvalue + " x ?\n");
    			int secondvalue = scan.nextInt();
    			int answer = firstvalue * secondvalue;
    			System.out.println(firstvalue + " x " + secondvalue + " = " + answer);
    		}
    		//Division.
    		int division = scan.nextInt();
    		if (division == 4) {
    			System.out.println("Division selected: Input second number please: " + firstvalue + " / ?\n");
    			int secondvalue = scan.nextInt();
    			int answer = firstvalue / secondvalue;
    			System.out.println(firstvalue + " / " + secondvalue + " = " + answer);
    		}
    		
    		
    	}
    	
    
    }

    The code works, but my dilemma is that only the addition section works. This is probably due to the fact that code works it's way down, and since the addition condition is not met, none of the other conditions work. How would I make it so that all of the Addition, Subtraction, Multiplication, and Division conditions are activated at the same time?

    Thanks in advance for your help. :)

    -Joshua

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,569
    Rep Power
    12

    Default Re: Need help with Calculator

    only the addition section works. This is probably due to the fact that code works it's way down, and since the addition condition is not met, none of the other conditions work.
    Yes - you ask about addition, then you ask about subtraction etc.

    A better approach, since you prompt the user once you should get their response once and then deal with it:

    Java Code:
        // prompt user and get response
    System.out.println("Type: \n '1' for Addition etc");
    int response = scan.nextInt();
    
        // deal with the response they give
    if(response == 1) {
        // addition code here
    }
    if(response == 2) {
        // subtraction code here
    }
    // etc - what happens if they enter 42?
    Note that whatever they enter it will be 1 OR 2 OR 3 etc but never more than one thing. So we could write:

    Java Code:
    if(response == 1) {
    
    } else if(response == 2) {
    
    } else //etc
    Have you come across "switch" statements, because that's what we really have here.

  3. #3
    Join Date
    Dec 2011
    Posts
    3
    Rep Power
    0

    Default Re: Need help with Calculator

    Okay, thanks. I made that change and also changed the "int" to "double". Thanks again. :)

    Now how would I reset the entire code so that once they get their answer it starts again at the top with "Please input first value: "?

  4. #4
    jiffi is offline Member
    Join Date
    Oct 2011
    Posts
    79
    Rep Power
    0

    Default Re: Need help with Calculator

    use a loop

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,569
    Rep Power
    12

    Default Re: Need help with Calculator

    I agree, use a loop.

    To elaborate just a little, it would be a good idea if the main() method were not allowed to grow like topsy. main() might just consist of a loop whose body consists of a call to another method to run the calculator and a prompt asking if the user wants to continue.

Similar Threads

  1. calculator
    By rithish in forum AWT / Swing
    Replies: 10
    Last Post: 04-26-2011, 12:24 AM
  2. Help with AWT CALCULATOR
    By Megan Dosnueve in forum AWT / Swing
    Replies: 2
    Last Post: 04-04-2011, 06:49 PM
  3. Help in a calculator
    By Ayannie in forum New To Java
    Replies: 6
    Last Post: 01-04-2011, 09:21 PM
  4. help with calculator
    By kalibballer in forum New To Java
    Replies: 8
    Last Post: 04-01-2009, 01:57 PM
  5. Calculator help.
    By madkidd02 in forum New To Java
    Replies: 2
    Last Post: 10-25-2008, 08:42 AM

Posting Permissions

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