Results 1 to 7 of 7
Thread: Simple Help
- 01-09-2010, 05:58 PM #1
Member
- Join Date
- Jan 2010
- Posts
- 3
- Rep Power
- 0
Simple Help
Hello All!
I am new to Java and hope to learn this over time and progress onto EE and ME for future developments.
I am still learning Java syntax basics.
Below is some code I have written, but found when using a single line variable it gives me the wrong answer. I decided to do this with multiple variables and get the correct answer, now I have managed to problem solve the single line variable with stepping the code and found the divide is where my problems start.
As you can see I also tried to Double this, with no success. When using a method to view the calculation it shows fine in console, just wrong numbers for one output.Java Code:public class Temperature { public int fahrenheit = 9; public int celsius1 = fahrenheit - 32; public int celsius2 = celsius1 * 5; public int celsius3 = celsius2 / 9; public int celsius = fahrenheit - 32 * 5 / 9; // public double celsius = fahrenheit(double) - 32 * 5 / 9; public void calculateCelsius() { System.out.println(fahrenheit + "Fahrenheit to Celsius is " + celsius3 + "c is correct"); System.out.println(fahrenheit + " Fahrenheit to Celsius is " + celsius + "c"); } }
I don't want to move on in learning until I understand the problem and what I did wrong.
Any help welcomed :)Last edited by Andy_M; 01-09-2010 at 06:00 PM. Reason: Update
- 01-09-2010, 06:04 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,601
- Blog Entries
- 7
- Rep Power
- 17
You have been bitten by the 'integer division' operator. That operator divides two ints and forgets all about the remainder. Literal numbers that don't contain a decimal point (a dot) are interpreted as ints by the compiler, so 5/9 is an integer division of the two int numbers 5 and 9; the result is 0 (that forgotten remainder, remember?).
Better use double numbers, e.g. 5.0/9.0 and use variables of type double. Try it and see for yourself.
kind regards,
Jos
- 01-09-2010, 06:08 PM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,601
- Blog Entries
- 7
- Rep Power
- 17
I just noticed; change your code to the above version. (multiplication has a higher precedence than subtraction).Java Code:public double celsius = (fahrenheit - 32) * 5.0 / 9.0;
kind regards,
Jos
- 01-09-2010, 06:12 PM #4
Member
- Join Date
- Jan 2010
- Posts
- 3
- Rep Power
- 0
Jos,
This has worked a treat. So the game is to make sure I try and use double numbers IE 1.0 for doubles and make sure I close in calculation with ( ) before a higher operator?
- 01-09-2010, 07:45 PM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,601
- Blog Entries
- 7
- Rep Power
- 17
- 01-09-2010, 08:06 PM #6
Member
- Join Date
- Jan 2010
- Posts
- 3
- Rep Power
- 0
Thank you Jos for taking time out to help me
- 01-09-2010, 09:10 PM #7
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,601
- Blog Entries
- 7
- Rep Power
- 17
Similar Threads
-
Simple AI?
By Atriamax in forum New To JavaReplies: 4Last Post: 12-23-2009, 10:35 PM -
New simple application using a simple database
By webbusiness23 in forum New To JavaReplies: 9Last Post: 08-03-2009, 02:55 AM -
Simple Help....
By jpnym15 in forum New To JavaReplies: 1Last Post: 11-11-2008, 02:33 PM -
Help with a very simple method for a very simple beginner.
By cakeman in forum New To JavaReplies: 2Last Post: 05-04-2008, 05:27 PM -
simple GUI
By dim_ath in forum New To JavaReplies: 3Last Post: 01-07-2008, 03:00 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks