Scanner Keyboard Input: BMI Calculator with nextDouble (using System.in)
Problem Description
Explanation
In this problem, you will create a program that reads name, height (in meters), weight (in kilograms), and age from standard input, calculates the BMI (Body Mass Index), classifies the result as Underweight, Normal, or Overweight, and displays a formatted health check card to standard output.
nextDouble() method
nextDouble() is a method that reads decimal numbers (double type). It's used for input that requires decimals, such as height and weight.
Implementation Example
import java.util.Scanner;Public class Solution {
Public static void main(String[] args) {
Scanner sc = new Scanner(System.in);// Read each data String name = sc.nextLine(); Double height = sc.nextDouble(); Double weight = sc.nextDouble(); Int age = sc.nextInt(); // Calculate BMI Double bmi = weight / (height * height); // Determine status String status; If (bmi < 18.5) { Status = "Underweight"; } else if (bmi < 25.0) { Status = "Normal"; } else { Status = "Overweight"; } // Display health check card System.out.println("===== Health Check Card ====="); System.out.println("Name: " + name); System.out.println("Height: " + height + "m"); System.out.println("Weight: " + weight + "kg"); System.out.println("Age: " + age + " years old"); System.out.printf("BMI: %.1f%n", bmi); System.out.println("Status: " + status); System.out.println("=============================");
}
}
Key Points
- nextDouble(): Method to read decimal numbers
- double type calculation: Calculations with decimals result in decimals
- printf(): Output with controlled decimal places (%.1f for one decimal place)
- Multiple conditions: Step-by-step judgment using if-else if
Meaning of BMI Calculation
BMI is an index showing the balance between weight and height. Through this calculation, you learn how to create practical programs.
Ready to Try Running Code?
Log in to access the code editor and execute your solutions for this problem.
Don't have an account?
Sign Up