簡體   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