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 4 of 4

Thread: Simple Calculator

  1. #1
    Junior Member
    Join Date
    Mar 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Simple Calculator

    Hi everyone!

    I'm programming a simple calculator and this is my first time programming. I'm having a bit of difficulty on figuring out this assignment. Everything is working correctly except I need to have the console ask the user if he would like to rerun the program. When the user inputs, "yes" it should start again from the beginning. This is the code I have so far. Do I need to nest all of this code into another loop statement?

    import java.util.*;
    import javax.swing.*;
     
    public class SimpleCalculator {
    	public static void main (String [] args){
    		double result = 0.0, temp = result, oldResult = result;
     
    		Scanner input = new Scanner(System.in);
     
    		//Initialization of Calculator
    		System.out.println("Calculator is on");
    		System.out.println("result = " + result);
     
    		//Calculations
    		String operation = "";
    		while (!operation.equals("R") && !operation.equals("r")) {
    			operation = input.nextLine();
     
    			//Read operator
    			char sign = operation.charAt(0);
     
    			//Addition, Subtraction, Multiplication, Division
    			switch (sign){
    			case '+': temp = Double.parseDouble(operation.substring(1));
    					  oldResult = result;
    					  result = result + temp;
    					  System.out.println(oldResult + " + " + temp + " = " + result);
    					  break;
    			case '-': temp = Double.parseDouble(operation.substring(1));
    					  oldResult = result;
    					  result = result - temp;
    					  System.out.println(oldResult + " - " + temp + " = " + result);
    			  		  break;
    			case '*': temp = Double.parseDouble(operation.substring(1));
    					  oldResult = result;
    					  result = result * temp;
    					  System.out.println(oldResult + " * " + temp + " = " + result);
    					  break;
    			case '/': temp = Double.parseDouble(operation.substring(1));
    					  oldResult = result;
    					  result = result / temp;
    					  System.out.println(oldResult + " / " + temp + " = " + result);
    	  		          break;
    			case 'r': break;
    			case 'R': break;
    			default: System.out.println(sign + " is an unknown operation");
    					 System.out.println("Reenter your last line: ");
     
    			}
     
    			//END
    		}	
     
     
    		//Printing of Result
    		System.out.println("Final result = " + result);
     
    		//Again Method
    		System.out.println("Again?");
    		String again = input.nextLine();
     
     
    		}
     
     
    }
    Last edited by helloworld922; March 21st, 2011 at 10:16 PM.


  2. #2
    Member vanDarg's Avatar
    Join Date
    Jan 2011
    Location
    Chicago
    Posts
    65
    My Mood
    Mellow
    Thanks
    1
    Thanked 7 Times in 7 Posts

    Default Re: Simple Calculator

    This would best be handled with a do while loop.
    Yes, you would have to enclose the code you want within the loop. while and do-while statements
    "Everything should be made as simple as possible, but not simpler."
    Asking Questions for Dummies | The Java Tutorials | Java Coding Styling Guide

  3. #3
    Junior Member
    Join Date
    May 2011
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Simple Calculator

    you could also use a SENTINEL constant value that the user can type to escape the loop. make another switch statement after the
    System.out.println("Again?");
    String again = input.nextLine();
    that would continue for yes(say !(operation.equals(r)) again, which will reiterate the loop) for no, say operation.equals(r) is true, and the SENTINEL value could be 0 or q or something, which would also exit the loop.

  4. #4

    Default Re: Simple Calculator

    add a Do-While Statement into your code and then run your code. It will work as you want it to.
    Warm Regards,

    weakprogrammer

    Code 2 Learn

Similar Threads

  1. Calculator
    By Andrew Wilson in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 2nd, 2011, 08:08 AM
  2. Calculator
    By javapenguin in forum What's Wrong With My Code?
    Replies: 5
    Last Post: December 22nd, 2010, 09:00 AM
  3. [SOLVED] Calculator help
    By Bradshjo in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 1st, 2010, 04:27 PM
  4. Basic Calculator
    By Yes. in forum What's Wrong With My Code?
    Replies: 13
    Last Post: June 4th, 2010, 04:24 PM
  5. Calculator help.
    By Skinnyskinny in forum Java Theory & Questions
    Replies: 6
    Last Post: August 1st, 2009, 12:34 PM