The story when I was advancing Progate
Height (height) and weight (weight) are defined in double type, and use it to calculate BMI (bmi). Since BMI is an integer type, it is necessary to round off the calculated value. You could use the Math.round () method.
public static int bmi(double weight,double height){
int bmi=Math.round(weight/height/height);
return bmi;
}
I wrote it like this. I thought that if I rounded it with the Math.round () method, it would naturally become an int type. But it returns an error.
Apparently, the value rounded off by the Math.round () method seems to be a long type, and if you want to use it as an int type, you have to convert it. So
public static int bmi(double weight,double height){
int bmi=int(Math.round(weight/height/height));
return bmi;
}
I rewrote it. If you think that the value rounded by Math.round () is now an int type, you will get an error again.
I misunderstood it as Python, and the type conversion method was wrong.
Finally
public static int bmi(double weight,double height){
int bmi=(int)Math.round(weight/height/height);
return bmi;
}
By doing so, it finally processed without error. I'm happy.
It was a memorandum memo.
-~~ If you round off with the Math.round () method, it will be a long type ~~ --Math.round () method has Math.round (float) and Math.round (double), (float) will be int type, and (double) will be long type. --When converting data type in Java, you have to say "(data type) variable name;"
It was that.
When I was looking for a way to deal with various things, I found a page of teratail that was asking the same question in the same situation, and I got nothing. URL:https://teratail.com/questions/114663
Recommended Posts