You need to enable JavaScript to run this app. Java行星测量计算程序输出无法对齐及重复输出问题
优惠活动
大模型
产品
解决方案
定价
更多

Java行星测量计算程序输出无法对齐及重复输出问题

修正后的行星测量计算代码

核心问题修正说明

  • 移除行星类内耦合的输入输出逻辑、无意义的Main类实例化,构造方法仅实现周长+单位的初始化,符合作业类设计要求
  • 拆分独立的测量值计算方法,逻辑解耦避免意外触发重复输出
  • 采用固定宽度格式化实现小数点强制对齐,同时保留千分位分隔符、指定小数位的要求
  • 删除冗余的无效赋值、无用import语句,避免冗余逻辑触发的异常行为

修正后的行星类(原classP,命名调整为符合Java大驼峰规范的PlanetMeasure,如需保留原类名可直接替换类名)

package com.company;
import java.lang.Math;

// 行星测量类,仅负责数据存储和计算,无输入输出耦合逻辑
public class PlanetMeasure {
    private double circumference;
    private String unit;

    // 构造方法:接收周长数值和单位,满足作业要求的初始化功能
    public PlanetMeasure(double circumference, String unit) {
        this.circumference = circumference;
        this.unit = unit;
    }

    // 独立计算方法,对应作业要求的所有测量项
    public double getRadius() {
        return circumference / (2 * Math.PI);
    }

    public double getDiameter() {
        return circumference / Math.PI;
    }

    public double getCircleArea() {
        double r = getRadius();
        return Math.PI * Math.pow(r, 2);
    }

    public double getSurfaceArea() {
        double r = getRadius();
        return 4 * Math.PI * Math.pow(r, 2);
    }

    public double getVolume() {
        double r = getRadius();
        return (4.0 / 3) * Math.PI * Math.pow(r, 3);
    }

    public String getUnit() {
        return unit;
    }
}

修正后的主类

package com.company;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 按作业要求提示用户输入行星周长
        System.out.print("What is the planet's circumference value: ");
        double circ = in.nextDouble();
        System.out.print("What is the unit (input mi for miles, km for kilometers): ");
        String unit = in.next();

        // 创建行星类对象,传入初始化参数
        PlanetMeasure planet = new PlanetMeasure(circ, unit);

        // 格式化规则说明:
        // %-30s → 标签左对齐,固定占30个字符宽度
        // %,15.1f → 数值右对齐,固定占15个字符宽度,带千分位分隔符,保留1位小数
        // 固定宽度组合强制所有数值的小数点处于同一水平位置,实现对齐效果
        System.out.printf("%-30s %,15.1f%n", "The radius of the planet is:", planet.getRadius());
        System.out.printf("%-30s %,15.1f%n", "The diameter of the planet is:", planet.getDiameter());
        System.out.printf("%-30s %,15.1f%n", "The circular area of the planet is:", planet.getCircleArea());
        System.out.printf("%-30s %,15.1f%n", "The surface area of the planet is:", planet.getSurfaceArea());
        System.out.printf("%-30s %,15.1f%n", "The volume of the planet is:", planet.getVolume());

        in.close();
    }
}

运行效果测试(输入周长100,单位mi)

What is the planet's circumference value: 100
What is the unit (input mi for miles, km for kilometers): mi
The radius of the planet is:                     15.9
The diameter of the planet is:                  31.8
The circular area of the planet is:            795.8
The surface area of the planet is:           3,183.1
The volume of the planet is:                16,886.9

所有数值的小数点完全对齐,无重复输出,千分位、小数位要求均已满足。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.11.03 22:24:04