Results 1 to 15 of 15
- 01-23-2010, 10:19 PM #1
Help with the calculation of a variable.
Hi!
I just started learning java and got an assigment to create a program that calculates the BMI, and tells you one of the four options. The problem is that it always skips the first three options and gives the fourth every time.
Java Code:import javax.swing.JOptionPane; public class CharDemo2 { public static void main(String[] args) { int pikkus; int kaal; double indeks; String tulemus; pikkus = Integer.parseInt(JOptionPane.showInputDialog("Sisesta enda pikkus:")); kaal = Integer.parseInt(JOptionPane.showInputDialog("Sisesta enda kaal:")); indeks = kaal / (pikkus * pikkus); if (indeks >= 19 && indeks <= 25) { tulemus = "Te olete normaalkaalus."; } else if (indeks >= 25 && indeks <= 40) { tulemus = "Te olete ülekaaluline."; } else if (indeks > 40) { tulemus = "Teie tervis on ohus."; } else { tulemus = "Te olete anorektik."; } JOptionPane.showMessageDialog( null, "Tulemus: " + tulemus); } }
I believe the problem lies within the calculation of the variable "indeks", but I can't put my finger on it. Help is greatly appreciated.
-
put in System.out.println statements to check the value of all variables. Also println the result of the division to see if it is what you expect and to see if the program behaves correctly.
- 01-23-2010, 11:19 PM #3
println shows that "pikkus" and "kaal" are inserted but the line
gives a result of 0.0Java Code:indeks = kaal / (pikkus * pikkus);
- 01-23-2010, 11:57 PM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
/ performs what is called "integer division" it always rounds the answer so that it is an integer. (The rounding is towards zero: so positive results get rounded down). The solution is to make one or both of the things you are dividing a double. That way you will get ordinary division.
- 01-24-2010, 12:05 AM #5
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
The result is still a bit weird so you may want to check the formula. Isn't BMI weight/height? So why do you have pikkus*pikkus rather than just pikkus?
-
BMI is weight divided by height squared, so of pikkus is height in meters, the formula is set up fine. I still wonder if int division should matter too much here since BMI shouldn't be close to zero. To the original poster: are you using the correct units, meters and kg?
- 01-24-2010, 12:31 AM #7
actually no. i never even thought about the units not matching, but indeed that's the problem. i tried to get the indeks by using cm's instead of meters. but meters mean that the pikkus value should be double and when i set it as double it doesent compile.
-
It worked for me, but I unfortunately have no way of knowing how or why it's not working for you given the information above. We'll be able to help you fix the compile problem a lot quicker if you post your code attempt and the error message as well as indicate the line that causes the error.
- 01-24-2010, 01:33 AM #9
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
Oh! Sorry about introducing the confusion.BMI is weight divided by height squared
- 01-24-2010, 12:10 PM #10
I tried to get the double pikkus inserted using JOptionPane but I couldn't do it. So I tried this
I still get an error when trying to insert the double pikkus - for example 1.8.Java Code:import java.util.Scanner; public class CharDemo2 { public static void main(String[] args) { int kaal; double indeks; String tulemus; Scanner scan = new Scanner(System.in); System.out.println("Sisesta enda pikkus(meetrites): "); double pikkus = scan.nextDouble(); System.out.println("Sisesta enda kaal(kilodes): "); kaal = scan.nextInt(); indeks = kaal / ( pikkus * pikkus ); System.out.println(pikkus); System.out.println(kaal); System.out.println(indeks); if ((indeks >= 19) && (indeks <= 25)) { tulemus = "Te olete normaalkaalus."; } else if ((indeks >= 25) && (indeks <= 40)) { tulemus = "Te olete ülekaaluline."; } else if (indeks > 40) { tulemus = "Teie tervis on ohus."; } else { tulemus = "Te olete anorektik."; } System.out.println("Tulemus: " + tulemus); } }
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextDouble(Unknown Source)
at CharDemo2.main(CharDemo2.java:13)
//EDIT
It's working now. Thanks to everyone who replied :).Last edited by rarschach; 01-24-2010 at 12:48 PM.
- 01-24-2010, 01:41 PM #11
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,400
- Blog Entries
- 7
- Rep Power
- 17
I never understood those funky formulas: height is some one dimensional magnitude while weight is something three dimensional. I would've expected height raised to the power three to make it somehow compatible or measurable with weight; but who am I? ;-)
kind regards,
Jos
-
You're comparing a rough estimate of the body's surface area with its volume. This division should be a one-dimensional entity that correlates with "girth". Regardless of it looking funny, it just so happens to scale well be the patient tall or short.
- 01-24-2010, 02:05 PM #13
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,400
- Blog Entries
- 7
- Rep Power
- 17
Ah, now I get it: the height squared is a rough estimation for the total skin area which is supposed to be an indication for the amount of fat in the three dimensional body. Why don't they measure the entire volume and compare it to the weight instead? Fat doesn't weigh much ... (this isn't really a question, just wondering).
kind regards,
Jos
-
Not quite. The height squared won't change no matter how much fat you have. The weight over the height squared is an index of fat, something that is invariant no matter how tall you are.
We're talking screening tools here where ease and speed trump accuracy. If you are tagged by a screening tool, then you should undergo a series of more thorough and accurate nutritional-state tests.Why don't they measure the entire volume and compare it to the weight instead? Fat doesn't weigh much ... (this isn't really a question, just wondering).
- 01-24-2010, 02:50 PM #15
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,400
- Blog Entries
- 7
- Rep Power
- 17
Yep, you're right; silly me. Except when all the fat piles up on your head ;-)
I find it a silly scale; any division of height divided by length would've done just as well imho. Or do they just want to keep their patients and beginner programmers busy? ;-)We're talking screening tools here where ease and speed trump accuracy. If you are tagged by a screening tool, then you should undergo a series of more thorough and accurate nutritional-state tests.
kind regards,
Jos
Similar Threads
-
Need help with doing a calculation in Java
By John D. in forum New To JavaReplies: 6Last Post: 02-24-2009, 11:44 PM -
Delay on inputs during calculation
By matt_well in forum New To JavaReplies: 14Last Post: 07-26-2008, 04:17 PM -
Area Calculation: Add, Substract, XOR
By Java Tip in forum java.awtReplies: 0Last Post: 06-22-2008, 11:08 PM -
Two problem for my rssi calculation program
By iamchoilan in forum Advanced JavaReplies: 3Last Post: 04-25-2008, 03:49 PM -
Problem with Calculation ....
By danny000 in forum New To JavaReplies: 1Last Post: 04-15-2008, 02:42 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks