繁体   English   中英

如何让我的代码的最后一行打印小数点后 2 位的所有值?

[英]How to I make the last line of my code print all values with 2 digits after decimal point?

import java.util.Scanner;
import javax.swing.plaf.basic.BasicInternalFrameTitlePane.SystemMenuBar;

class Main {

    public static void main(String[] args) {
        System.out.println("My name is Akhil Madusudan");
        System.out.println("This program calculates the volume and surface area of a cuboid");

        Scanner l = new Scanner(System.in);
        System.out.println("Enter the cuboid length:");
        double length = l.nextDouble();


        Scanner h = new Scanner(System.in);
        System.out.println("Enter the cuboid height:");
        double height = h.nextDouble();

        Scanner w = new Scanner(System.in);
        System.out.println("Enter the cuboid width:");
        double width = w.nextDouble();

        double cuboidVolume;
        cuboidVolume = (length * width * height);
        double surfaceArea;
        surfaceArea = ((2) * width * length + (2) * length * height + (2) * width * height);

        System.out.printf("Volume of the cuboid (Length: " + length + "/Height: " + height + "/ Width: " + width + ") is " + cuboidVolume);
    }
}

您只需要一台Scanner 只是改变

System.out.printf("Volume of the cuboid (Length: " + 
        length + "/Height: " + height + 
        "/ Width: " + width + ") is " + cuboidVolume);

类似于

System.out.printf("Volume of the cuboid (Length: "
        + "%.2f/Height: %.2f/ Width: %.2f) is %.2f%n",
        length, height, width, cuboidVolume);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM