BMIの計算 【Javaのサンプルコード】

Print Friendly, PDF & Email

ここでは、当社の新人研修受講者に向けて、Javaのサンプルコードを紹介しています。

BMIの計算

 

import java.io.*;

public class BMITest {
    public static void main( String[] args ) {
        double h, w, bmi;

        try {
            BufferedReader br
            = new BufferedReader( new InputStreamReader(System.in) );

            System.out.println( "This is a judging program of BMI" );
            System.out.print( "Please specify height (cm): " );
            h = Double.parseDouble( br.readLine() );
            System.out.print( "Please specify your weight (kg): " );
            w = Double.parseDouble( br.readLine() );

            h *= 0.01;  // from centimeters to meters
            bmi = w / (h*h);

            System.out.println( "Your BMI is " + Math.round( bmi*10.0 )/10.0 );
            if( bmi < 18.5 )      System.out.println( "thin type" );
            else if( bmi < 25.0 ) System.out.println( "standard type" );
            else                  System.out.println( "Obese type" );
            br.close();
        } catch( Exception e ) {
            System.out.println( "Exception occured: " + e );
        }
    }
}

参考:

BMIとは(Wikipedia)