Results 1 to 3 of 3

Thread: Calculator

  1. #1
    FuzzyJava is offline Member
    Join Date
    Oct 2014
    Posts
    1
    Rep Power
    0

    Lightbulb Calculator

    Hi!I am new to Java and i've built a calculator.How is it ?? What may I to add to it? See the code :
    Java Code:
     
    import java.util.Scanner;
    public class apples {
     public static void main (String[] args) {
    	double firstnumber, secondnumber;
    	Scanner calculator = new Scanner(System.in);
    	String operation;
    	
    	System.out.println("Enter first number");
    	firstnumber = calculator.nextDouble();
    	System.out.println("Enter second number");
    	secondnumber = calculator.nextDouble();
    	System.out.println("Enter your operation (* or / or - or +)");
    	operation = calculator.next();
    	
    	switch (operation) {
    	case "*" :
    		System.out.println("Your answer is\t" + (firstnumber * secondnumber));
    		break;
    	case "/":
    		System.out.println("Your answer is\t" + (firstnumber / secondnumber));
    		break;
    	case "+" :
    		System.out.println("Your answer is\t" + (firstnumber + secondnumber));
    		break;
    	case "-" :
    		System.out.println("Your answe is \t" + (firstnumber - secondnumber));
    		break;
    		default:
    			System.out.println("Sorry,we can't recognize your numbers or operation.Try again");
    	}
    	 }
    }
    Last edited by FuzzyJava; 10-27-2014 at 11:59 AM.

  2. #2
    ababa is offline Member
    Join Date
    Oct 2014
    Location
    Bhagalpur, India
    Posts
    3
    Rep Power
    0

    Default Re: Calculator

    expand it so it can keep on doing calculations not just on two numbers like a real calculator

  3. #3
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    13

    Default Re: Calculator

    How is it? Its a very minimalistic command line application that only supports the easiest of calculator functions. If that is what you wanted to achieve, then its fine. There is a typo in the "-" part.

    I don't know what you may add to it, you don't provide any context. Is this a school assignment? Are you building this to learn Java on your own? What have you learned so far? You can improve this with error handling (try inputting something which is NOT a number), but that would require you to learn exception handling first.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. need some help with my calculator
    By Tooks1 in forum New To Java
    Replies: 0
    Last Post: 02-13-2013, 07:38 PM
  2. Calculator
    By Elviii in forum New To Java
    Replies: 6
    Last Post: 01-06-2013, 03:43 AM
  3. BMI calculator
    By snustad in forum New To Java
    Replies: 4
    Last Post: 11-29-2012, 10:08 PM
  4. Calculator
    By water in forum AWT / Swing
    Replies: 4
    Last Post: 09-23-2009, 07:00 AM
  5. help with calculator
    By kalibballer in forum New To Java
    Replies: 8
    Last Post: 04-01-2009, 01:57 PM

Tags for this Thread

Posting Permissions

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