Results 1 to 4 of 4
Like Tree1Likes
  • 1 Post By Baconator

Thread: Small Problem With a Maths Program

  1. #1
    Interista is offline Member
    Join Date
    Oct 2011
    Posts
    24
    Rep Power
    0

    Default Small Problem With a Maths Program

    Program works grand except it gives looser numbers as answers, rather than exact.

    I.e. it should add the three numbers and give the answer of 16.3
    and it should multiply and subtract and give answer 118.24

    Instead it gives 16.2999999xxxxxxxxx
    and 118.23999999xxxxxx

    How do I tighten it up?

    Java Code:
    public class VariableDifference
    {
    	public static void main (String[] args)
    		{
    			double num1=3.1f;
    			double num2=6.2f;
    			double num3=7.0f;
    									
    			double sum = num1+num2+num3;
    			double sum1 = num1*num2*num3-num1-num2-num3;
    						
    			System.out.println("The variable sum contains the value " + sum);
    			System.out.println("The variable difference contains the value " + sum1);
    			
    			System.exit(0);
    		}
    }

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,691
    Rep Power
    8

    Default Re: Small Problem With a Maths Program

    Sounds like a job for DecimalFormat or the printf method: PrintStream (Java Platform SE 6)
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Interista is offline Member
    Join Date
    Oct 2011
    Posts
    24
    Rep Power
    0

    Default Re: Small Problem With a Maths Program

    Ah right, I think I should be careful over that. My lecturer hasn't covered that yet so he might think I've been cheating!

  4. #4
    Baconator is offline Member
    Join Date
    Oct 2011
    Posts
    10
    Rep Power
    0

    Default Re: Small Problem With a Maths Program

    Quote Originally Posted by Interista View Post
    Ah right, I think I should be careful over that. My lecturer hasn't covered that yet so he might think I've been cheating!
    I wouldn't consider it cheating if you went ahead and did some out of class research to learn more about programming. I'm taking AP Computer Science at my high school and I'm a full semester ahead of my other classmates since I have been teaching myself through YouTube, Google, and referring to later chapters in the book. Good luck though!
    Anveshan likes this.

Similar Threads

  1. Help me with maths on my program!
    By Ruuhkis in forum New To Java
    Replies: 6
    Last Post: 02-17-2011, 06:27 AM
  2. I need help doing a small program.
    By fastcar83 in forum New To Java
    Replies: 7
    Last Post: 11-03-2010, 10:00 PM
  3. Small calender program
    By Moustafa taha in forum New To Java
    Replies: 1
    Last Post: 10-19-2010, 07:52 PM
  4. Please HELP Java small program
    By afrttoh in forum New To Java
    Replies: 14
    Last Post: 11-08-2008, 02:29 AM
  5. small issues with a program
    By jimJohnson in forum New To Java
    Replies: 6
    Last Post: 04-25-2008, 08:28 AM

Posting Permissions

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