繁体   English   中英

在Java窗格中格式化输出

[英]Formatting output in Java pane

我正在尝试获取Java程序的输出以正确格式化。 该程序似乎可以正常工作。

[码]

import javax.swing.JOptionPane;

public class Exercise4 {

    public static void main(String[] args) {
        double weight;
        double height;
        double bmi;
        String input;

        // Get users's weight.
        input = JOptionPane.showInputDialog("Enter your weight.");

        weight = Double.parseDouble(input);

        //Get users's height
        input = JOptionPane.showInputDialog("Enter your height in inches.");

        height = Double.parseDouble(input);

        bmi = weight * (703 / Math.pow(height, 2.0));

        JOptionPane.showMessageDialog(null, "Your BMI is  " + bmi);


    }

}

[/码]

尝试这个:

JOptionPane.showMessageDialog(null, String.format("Your BMI is %1.2f", bmi));

%1.2f将十进制数字四舍五入到小数点后两位。

您应该看一下格式说明符

暂无
暂无

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

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