【问题标题】:I cannot get my program output to align correctly in Java我无法让我的程序输出在 Java 中正确对齐
【发布时间】:2021-02-18 11:06:36
【问题描述】:

这些是说明... 编写一个类来计算行星的测量值。

  1. 用这些方法编写一个类。

    • 以英里为单位初始化行星圆周的构造函数或 公里
    • 计算行星测量值的方法 - 圆、面积、直径、半径、曲面、面积、体积
  2. 使用你的类编写一个程序 - 询问用户行星的周长 - 创建一个行星对象 - 用逗号和小数打印行星的测量值 - 确保小数对齐。

package com.company;
import java.util.Scanner;
import java.lang.reflect.Field;

public class Main {

    public static void main(String[] args) {
        Main planet = new Main();
        Scanner in = new Scanner(System.in);
        classP cP = new classP();


        //cP.getRadius();
        //cP.getCirleArea();
        //cP.getcirc();

    }
}
package com.company;
import java.util.Scanner;
import java.lang.Math;
import java.text.DecimalFormat;

public class classP {
    private double circ, radius, circleArea, surfArea, volume;
    //public classP radius;
    //public double circ;
    //public double circleArea;

    public Main planet = new Main();

    public classP() {
        this.circ = circ;
        this.radius = radius;
        this.circleArea = circleArea;
        this.surfArea = surfArea;
        this.volume = volume;

        Main mainClass = new Main();
        Scanner in = new Scanner(System.in);
        DecimalFormat df = new DecimalFormat("###,###,###.##");

        System.out.print("What is the planet's circumference in miles?  ");
        double circ = in.nextDouble();
        double radius = circ / (Math.PI * 2);
        double circleArea = Math.PI * Math.pow(radius, 2);
        System.out.printf("%nThe radius of the planet is: " + "%,12.1f\n" , radius);
        System.out.printf("%nThe area of the planet is: " + "%,12.1f\n", circleArea);
        double diameter = circ / Math.PI;
        System.out.printf("%nThe diameter of the planet is: " + "%,12.1f\n", diameter);
        double surfArea = 4 * Math.PI * Math.pow(radius, 2);
        System.out.printf("%nThe surface area of the planet is: " + "%,12.1f\n", surfArea);
        double volume = (4.0 / 3) * Math.PI * Math.pow(radius, 3);
        System.out.printf("%nThe volume of the planet is: " + "%,12.1f\n", volume);
    }


    //public final double getcirc(){
    //    return this.circ;
    //}
    //public final classP getRadius(){
    //    return this.radius;
    //}
    //public final double getCirleArea() {
    //    return this.circleArea;
    //}
}

这是我的输出,示例用户输入为:100

What is the planet's circumference in miles?  100

The radius of the planet is:         15.9

The radius of the planet is: 15.9        

The area of the planet is:        795.8

The area of the planet is: 795.8       

The diameter of the planet is: 31.8                          

The surface area of the planet is: 3,183.1                       

The volume of the planet is: 16,886.9 

【问题讨论】:

    标签: java


    【解决方案1】:

    需要对您的代码进行一些基本的改进。代码块中存在许多不必要的代码。我试图组织它们。它可以指导你。

    对于类似的打印,使用String.format 创建具有打印机功能的自定义模板。实际上很容易维护。

    代码:

    import java.util.Scanner;
    
    public class Main {
    
        public static void main(String[] args) {
            Main planet = new Main();
            Scanner in = new Scanner(System.in);
            
            System.out.print("What is the planet's circumference in miles?  ");
            double circ = in.nextDouble();
            
            ClassP cP = new ClassP(circ);
            cP.calculateData();
            cP.print();
    
        }
    
    }
    
    class ClassP{
        
        private double circ, radius, circleArea, diameter, surfArea, volume;
    
        public ClassP(double circ) {
            this.circ = circ;  
        }
        
        public void calculateData() {
            radius = circ / (Math.PI * 2);
            circleArea = Math.PI * Math.pow(radius, 2);
            diameter = circ / Math.PI;
            surfArea = 4 * Math.PI * Math.pow(radius, 2);
            volume = (4.0 / 3) * Math.PI * Math.pow(radius, 3);
            
        }
        
        public void print() {
            customAlignPrinter("The radius of the planet is:", radius);
            customAlignPrinter("The area of the planet is:", circleArea);
            customAlignPrinter("The diameter of the planet is:",diameter);
            customAlignPrinter("The surface area of the planet is:", surfArea);
            customAlignPrinter("The volume of the planet is:", volume);
        }
        
        public void customAlignPrinter(String msg,double val) {
            System.out.println(String.format("%40s %,20f", msg, val));
        }
        
    }
    

    输出控制台:

    What is the planet's circumference in miles?  100
                The radius of the planet is:            15.915494
                  The area of the planet is:           795.774715
              The diameter of the planet is:            31.830989
          The surface area of the planet is:         3,183.098862
                The volume of the planet is:        16,886.863940
    

    【讨论】:

    • 这也很有帮助。我还很新,所以我正在学习。我慢慢好起来了。我也做了一些工作,发现我可以放置 ("%-40s") 而不是 ("%40s") ,它会将字符串向左对齐。你们俩都帮了我很大的忙,我很感激。这是我在这里发布的第一个问题,非常棒。
    • 不错,这个地方非常适合学习。我们互相帮助学习和提高自己。 :)
    【解决方案2】:

    您需要创建一个格式字符串,您可以将标签传递给该格式字符串,以指定一个足够大的填充空间以容纳最长的空间。

    package com.company;
    import java.util.Scanner;
    import java.lang.Math;
    import java.text.DecimalFormat;
    
    class classP {
        public static void main(String[] args) {
            Scanner in = new Scanner(System.in);
            System.out.print("What is the planet's circumference in miles?  ");
    
            double circ = in.nextDouble();
            double radius = circ / (Math.PI * 2);
            double circleArea = Math.PI * Math.pow(radius, 2);
    
            System.out.println();
    
            String format = "%35s %,12.1f\n";
    
            System.out.printf(format, "The radius of the planet is" , radius);
            System.out.printf(format, "The area of the planet is", circleArea);
            double diameter = circ / Math.PI;
            System.out.printf(format, "The diameter of the planet is", diameter);
            double surfArea = 4 * Math.PI * Math.pow(radius, 2);
            System.out.printf(format, "The surface area of the planet is", surfArea);
            double volume = (4.0 / 3) * Math.PI * Math.pow(radius, 3);
            System.out.printf(format, "The volume of the planet is", volume);
        }
    }
    

    结果:

    What is the planet's circumference in miles?  100
    
            The radius of the planet is         15.9
              The area of the planet is        795.8
          The diameter of the planet is         31.8
      The surface area of the planet is      3,183.1
            The volume of the planet is     16,886.9
    

    【讨论】:

    • 加密傻瓜!这正是我所需要的。非常感谢。之后我一直在努力,从你所说的看来我走在正确的道路上。
    • 是的。你到了那里!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-03
    • 2021-03-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-20
    • 2014-02-08
    相关资源
    最近更新 更多