+ 1

Can someone please tell me what's wrong with my code?

I'm trying to make a bmi calculator in java. import java.util.Scanner; public class Program { public static void main(String[] args) { Scanner height=new Scanner(System.in); Scanner weight=new Scanner(System.in); double h= height.nextDouble(); double w= weight.nextDouble(); double in=(h*0.0254); double bmi=((w*0.453592)/(in*in)); if (bmi<18.5) { System.out.println(bmi); System.out.println("Underweight"); }else if (bmi>=18.5 && bmi<25) { System.out.println(bmi); System.out.println("Normal"); }else if (bmi>=25 && bmi<30) { System.out.println(bmi); System.out.println("Overweight"); }else { System.out.println(bmi); System.out.println("Obese"); } } }

27th Mar 2021, 2:57 AM
Ava
Ava - avatar
4 odpowiedzi
+ 5
Hi Ava :) You don't need multiple scanner objects for taking multiple inputs. One scanner gets the job done for you. Though I'm still trying to figure out why both scanners don't work together, here's a working version of the code. And I think you should recheck the formula for finding the BMI as well. Happy Coding :) https://code.sololearn.com/cU9f58UKP5Uw/?ref=app
27th Mar 2021, 3:08 AM
Soumik
Soumik - avatar
+ 2
This is my code now: import java.util.Scanner; public class Program { public static void main(String[] args) { Scanner height=new Scanner(System.in); Scanner weight=new Scanner(System.in); double h= height.nextDouble(); double w= weight.nextDouble(); double in=(h*0.0254); double bmi=((w*0.453592)/(in*in)); if (bmi<18.5) { System.out.println(bmi); System.out.println("Underweight"); } else if (bmi>=18.5 && bmi<25) { System.out.println(bmi); System.out.println("Normal"); } else if (bmi>=25 && bmi<30) { System.out.println(bmi); System.out.println("Overweight"); } else { System.out.println(bmi); System.out.println("Obese"); } } } It still doesn't work though. Please help
27th Mar 2021, 3:02 AM
Ava
Ava - avatar
+ 2
Thank you. I'm using it to do bmi using inches and pounds. That's why it's a different formula
27th Mar 2021, 3:19 AM
Ava
Ava - avatar
+ 2
Ava You're Welcome :>
27th Mar 2021, 3:21 AM
Soumik
Soumik - avatar