ここでは、当社の新人研修受講者に向けて、Javaのサンプルコードを紹介しています。
BMIの計算
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
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 ); } } } |
参考: