user1730056
user1730056

Reputation: 623

Simple Java math operations

I'm making a BMI calculator that doesn't seem to be working.

I keep getting 0 as my answer, when it should be 15.7

Could someone tell me what I'm doing wrong here?

public class ass10 {

    public static void main(String[] args) {
    bmi(223,100);
    }
    public static bmi(int w, int h){
        double result;
        result = (w/(h*h))*703
        System.out.println(result)             
    }

}

Upvotes: 0

Views: 716

Answers (1)

PearsonArtPhoto
PearsonArtPhoto

Reputation: 39698

In Java, if you take an int/int, you get an int, rounded to the lower number (99/100=0). You want to cast it as a float, or better yet, a double.

public static void bmi(int w, int h){
    double result;
    result = ((double)w/(h*h))*703;
    System.out.println(result);         
}

I also fixed 2 missing semicolons for you;-) And your function wouldn't work without a return type, so I set that to void.

Upvotes: 8

Related Questions