[英]Formatting output in Java pane
I am trying to get the output of a java program to format correctly. 我正在尝试获取Java程序的输出以正确格式化。 The program seems to otherwise work correctly. 该程序似乎可以正常工作。
[code] [码]
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);
}
}
[/code] [/码]
Try this: 尝试这个:
JOptionPane.showMessageDialog(null, String.format("Your BMI is %1.2f", bmi));
%1.2f
rounds a decimal number to 2 decimal places. %1.2f
将十进制数字四舍五入到小数点后两位。
You should take a look at format specifiers . 您应该看一下格式说明符 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.