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

Java圆形计算类代码异常排查与修复:输入半径后结果全为0

Fixing Your Circle Calculation Java Code

Let's walk through the issues in your code and fix them one by one—you're really close, just a few key mistakes throwing everything off!

The Problems in Your Current Code

  • Calculation happens before input: You're computing area, diameter, and circumference right after initializing radius to 0. At that point, the user hasn't entered any value yet, so all calculations use the default 0 value—hence your 0 results.
  • Integer type causing precision loss: You're using int for Pi, area, circumference, and diameter. When you cast 3.14 to int, it gets truncated to 3, and any decimal results from your calculations will be chopped off too (like 50.24 becomes 50 at best, but since you calculated with 0 first, it's 0).
  • Variable name typo: In your last printf statement, you used environment instead of circumference—this would actually cause a compile error, not just wrong output.

Fixed Code

package circle;
import java.util.Scanner;

public class Circle {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        double radius; // Use double to allow non-integer radii too
        double diameter;
        double circumference;
        double area;
        final double PI = 3.14; // Use final double for Pi to keep precision

        // First get the user's input
        System.out.print("Enter radius value: ");
        radius = input.nextDouble(); // Use nextDouble to accept decimal radii

        // Now calculate values AFTER getting the radius
        diameter = radius * 2;
        circumference = 2 * PI * radius;
        area = PI * radius * radius;

        // Print results with 2 decimal places for readability
        System.out.printf("Area is %.2f%n", area);
        System.out.printf("Diameter is %.2f%n", diameter);
        System.out.printf("Circumference is %.2f%n", circumference);

        input.close(); // Don't forget to close the scanner!
    }
}

Key Changes Explained

  1. Swapped input and calculation order: We now get the user's radius first, then do all the math—so we're using the actual input value instead of 0.
  2. Switched to double types: This preserves decimal precision for Pi and all calculated values, so you'll get accurate results like 50.24 for radius 4.
  3. Fixed the variable typo: Replaced environment with circumference to match the variable name.
  4. Added final to PI: Marking Pi as final makes it clear this is a constant value that shouldn't be changed.
  5. Used nextDouble(): Allows users to enter non-integer radii (like 4.5) if they want, making the program more flexible.
  6. Closed the scanner: Good practice to clean up resources after use.

If you run this with radius 4, you'll get:

Enter radius value: 4
Area is 50.24
Diameter is 8.00
Circumference is 25.12

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 04:21:58