You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java初学者求助:BMI计算器遇variable might not have been initialized编译错误

Fixing the "Variable bmi might not have been initialized" Error in Your BMI Calculator

Hey Julie! I totally get the frustration of hitting that last stubborn bug after spending so much time tweaking your code—let's get this sorted out so your calculator runs smoothly.

What's Causing the Error?

The error message Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - variable bmi might not have been initialized spells out exactly the issue: you declared the bmi variable with double bmi;, but never gave it a concrete value before trying to use it in your if/else checks. Java requires all local variables to be explicitly initialized before they're referenced—unlike instance variables, it won't assign a default value automatically for these.

How to Fix It

First, you need to calculate the BMI value before checking its range. Remember, BMI is calculated as weight (in kilograms) divided by the square of height (in meters). Let's adjust your code to include that calculation, plus add steps to get user input (since you'll need height and weight values to compute BMI):

import java.util.Scanner;

public class BmiCalc {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        
        // Get user input for height and weight
        System.out.print("Enter your height in meters: ");
        double height = scanner.nextDouble();
        
        System.out.print("Enter your weight in kilograms: ");
        double weight = scanner.nextDouble();
        
        // Calculate BMI (this initializes the variable properly!)
        double bmi = weight / (height * height);
        
        // Now check the BMI range
        if (bmi < 18.5) {
            System.out.println("You are underweight");
        } else if (bmi >= 18.5 && bmi <= 24.9) {
            System.out.println("You are a healthy, normal weight");
        } else if (bmi >= 25 && bmi <= 29.9) {
            // Added this category for completeness!
            System.out.println("You are overweight");
        } else {
            System.out.println("You are obese");
        }
        
        scanner.close();
    }
}

Quick Tips for Your Upcoming Exception Handling

Since you mentioned planning to add exception handling later, here are a couple of key things to keep in mind:

  • Use try-catch blocks to handle cases where the user enters non-numeric values (like letters instead of numbers for height/weight)
  • Add validation checks to ensure height and weight are positive numbers—you can't have a negative height or weight!

You're doing awesome sticking with this as a beginner—don't get discouraged by these small hiccups! Every bug you fix is a chance to learn more about how Java works.

内容的提问来源于stack exchange,提问作者Julie Cohen

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 08:18:13