Hello and welcome to our community! Is this your first visit?
Register
Results 1 to 3 of 3
  1. #1
    Pen
    Pen is offline
    Enthusiast

    Join Date
    Mar 2009
    Posts
    1,282

    Pen's Learning Java :D

    Ok, I'm beginning to learn Java. And I've been trying to create a basic calculator.

    heres the coding I used:
    Code:
    import java.util.Scanner; 
    class mbc {
    	public static void main (String args [] ) {
    		Scanner calculator = new Scanner(System.in) ;
    		double fnum, snum, answer; 
    		System.out.println ("Enter first num") ; 
    		fnum = calculator.nextDouble(); 
    		System.out.println ("Enter second num") ; 
    		snum = calculator.nextDouble(); 
    		answer = fnum + snum; 
    		System.out.println (answer); 
    	}
    }
    Code:
    Enter first num
    34.6
    Enter second num
    54.2
    88.800000000001
    can anyone tell me what I did wrong? Or what I should do?

  2. #2
    Obsessed Veteran Adam's Avatar



    Join Date
    Nov 2004
    Location
    UK / England
    Posts
    17,271

    Re: Pen's Learning Java :D

    You're using type doubles, just round the answer to the nearest decimal place, or whatever precision you want.

  3. #3
    Pen
    Pen is offline
    Enthusiast

    Join Date
    Mar 2009
    Posts
    1,282

    Re: Pen's Learning Java :D

    Quote Originally Posted by Adam
    You're using type doubles, just round the answer to the nearest decimal place, or whatever precision you want.
    Hm.. thanks I'll try that.

    E: It worked. Thanks alot.


 

Posting Permissions

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