002-001-005
Scanner Keyboard Input: BMI Calculator with nextDouble (using System.in)
Hard
Problem Description
Explanation
In this problem, you will learn comprehensive programming that reads multiple different data types with Scanner class, performs calculations and makes judgments.
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.
.Test Cases
※ Output examples follow programming industry standards
Input:
Taro 1.75 68.5 30
Expected Output:
===== Health Check Card ===== Name: Taro Height: 1.75m Weight: 68.5kg Age: 30 years old BMI: 22.4 Status: Normal =============================
Input:
Alice 1.60 45.0 25
Expected Output:
===== Health Check Card ===== Name: Alice Height: 1.6m Weight: 45.0kg Age: 25 years old BMI: 17.6 Status: Underweight =============================
Input:
Bob 1.80 85.0 35
Expected Output:
===== Health Check Card ===== Name: Bob Height: 1.8m Weight: 85.0kg Age: 35 years old BMI: 26.2 Status: Overweight =============================
Input:
Test 1.70 53.5 20
Expected Output:
===== Health Check Card ===== Name: Test Height: 1.7m Weight: 53.5kg Age: 20 years old BMI: 18.5 Status: Normal =============================
Input:
User 1.65 68.0 40
Expected Output:
===== Health Check Card ===== Name: User Height: 1.65m Weight: 68.0kg Age: 40 years old BMI: 25.0 Status: Normal =============================
Input:
Name abc 60.0 25
Expected Output:
Error: Invalid height
Input:
Name 1.70 -50.0 25
Expected Output:
===== Health Check Card ===== Name: Name Height: 1.7m Weight: -50.0kg Age: 25 years old BMI: -17.3 Status: Underweight =============================
❌ Some tests failed
Your Solution
Current Mode:● My Code
99
1
2
3
4
5
6
7
8
9
10
›
⌄
⌄
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// Write your code here
sc.close();
}
}
0 B / 5 MB
You have 6 free executions remaining
