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: help on simple accumulator

  1. #1
    Junior Member Khalon's Avatar
    Join Date
    Jan 2010
    Location
    Canada
    Posts
    4
    My Mood
    Tolerant
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default (SOLVED) help on simple accumulator

    Hello everyone, I am new here and I am looking for help on a small assignment I have. It is a simple calculator which performs basic arithmetic operations (+ - / * power and square root) and I am having trouble with making so that once a user has performed a calculation, the calculator will continue and use the result of the last equation as the new first operand and prompt the user for a new operator and second operand. This will continue in a loop until the user enters 'q' as an operator to quit. As well I need to make it so when the user enters 'c' the accumulator will clear and the calculator resets asking the user to enter the first number.

    I've got everything else in my code I just cant wrap my head around how to do those two things.

    package simlpecalculator;
     
    /**
     * FileName: simpleCalculator.java
     * @author XXXXXXX
     * Date: January 13, 2010
     * Description: A simple calculator program used to perform basic arithmetic operations
     */
     
    import java.util.Scanner;
     
    public class Main {
     
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
     
     
            // variables
        	double frstNum = 0, scndNum = 0, ans = 0;
        	String operator = " ";
     
        	// create input object
        	Scanner keyboard = new Scanner(System.in);
     
     
            while (!operator.equals("q")){ // loop to continue application process until user quits.
     
        	// obtain the first number and operator
     
            System.out.println();
        	System.out.println("Enter the first number: ");
        	frstNum = keyboard.nextDouble();
     
     
        	System.out.println(); // white space
     
        	System.out.println("Enter an operator: ");
        	operator = keyboard.next();
     
        	System.out.println(); // white space
     
        	// quit the application if operator entered is 'q'
        	// if not continue application processes
        	if (operator.equals("q")){
                System.out.println("End of program");
            }
            else{
     
        	if (operator.equals("s")){ // check to see if user desires square root
        		ans = Math.sqrt(frstNum); // take the square root of the first number
     
        	}
        	else{ // if user does not need square root, prompt for second number
        		System.out.println("Enter the second number: ");
        		scndNum = keyboard.nextDouble();
        	}
     
        	System.out.println(); // white space
     
        	if (operator.equals("+")){ // perform addition
        		ans = frstNum + scndNum;
        	}
        	else if (operator.equals("-")){ // perform subtraction
        		ans = frstNum - scndNum;
        	}
        	else if (operator.equals("*")){ // perform multiplication
        		ans = frstNum * scndNum;
        	}
        	else if (operator.equals("/")){ // perform division
        		ans = frstNum / scndNum;
        	}
        	else if (operator.equals("p")){ // take the power of a number
        		ans = Math.pow(frstNum, scndNum);
        	}
     
            System.out.println("Your result is: " + ans);
            }
            }
     
     
        }
    }
    Last edited by Khalon; January 14th, 2010 at 11:48 PM.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: help on simple accumulator

    You need to re-assign firstNum with ans at the end of your while loop. Implementation of 'c' can be done at the end by asking the user for a new firstNum and restarting your while-loop.

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

    Khalon (January 14th, 2010)

  4. #3
    Junior Member Khalon's Avatar
    Join Date
    Jan 2010
    Location
    Canada
    Posts
    4
    My Mood
    Tolerant
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: help on simple accumulator

    Quote Originally Posted by helloworld922 View Post
    You need to re-assign firstNum with ans at the end of your while loop. Implementation of 'c' can be done at the end by asking the user for a new firstNum and restarting your while-loop.
    Thank you. Your answer helped me to solve my problem. ^^

Similar Threads

  1. not so simple, simple swing question box
    By wolfgar in forum AWT / Java Swing
    Replies: 2
    Last Post: November 20th, 2009, 03:47 AM