【问题标题】:How to I make the last line of my code print all values with 2 digits after decimal point?如何让我的代码的最后一行打印小数点后 2 位的所有值?
【发布时间】:2020-10-31 19:24:56
【问题描述】:
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);
    }
}

【问题讨论】:

标签: java syntax format


【解决方案1】:

您只需要一个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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-06-23
    • 1970-01-01
    • 1970-01-01
    • 2021-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-08
    相关资源
    最近更新 更多