(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.
Code :
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);
}
}
}
}
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.
Re: help on simple accumulator
Quote:
Originally Posted by
helloworld922
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. ^^