java - What is wrong with my BMI Calculator? -
i trying make bmi calculator calls method of separate class calculate bmi, continue in main method. below source code.
import java.util.scanner; public class testbmi { public static void main(string[] args) { string name; double bmi; string bmiclass; char z; scanner readinput = new scanner(system.in); { system.out.print("please enter name: "); name = readinput.nextline(); bmi.calculatebmi(); if (bmi < 18.5) { bmiclass = "(underweight)"; } else if (bmi >= 18.5 && bmi <= 25) { bmiclass = "(normal)"; } else if (bmi > 25.0 && bmi <= 30.0) { bmiclass = "(overweight)"; } else bmiclass = "(obese)"; system.out.println(name + ", bmi " +bmi+ "" +bmiclass+ "."); system.out.print("type y or y continue, or other key quit: "); z = readinput.next().charat(0); } while (z == 'y' || z == 'y'); } }
and other class:
import java.util.scanner; public class bmi { public static void calculatebmi() { scanner readinput = new scanner(system.in); double weight; double height; double newweight; double newheight; double bmi; system.out.print("please enter weight in pounds: "); weight = readinput.nextint(); system.out.print("please enter height in inches: "); height = readinput.nextint(); newweight = weight * 0.45359237; newheight = height * 0.0254; bmi = newweight/(newheight*newheight); } }
when compiling these files compile method class ( second snippet ) 1 error on first, reads this:
testbmi.java:21: error: variable bmi might not have been initialized if (bmi < 18.5) ^ 1 error
the variable bmi declared, initialization (actual value adding variable) occurs within separate method, however, i'm not sure why main method hasn't recognized it's initialization running of second method. please tell me doing wrong.
you need return value of bmi in bmi class. when check value of bmi in testbmi class it's been locally initialized in bmi class, not testbmi class.
Comments
Post a Comment