2014-01-25 103 views
0

我想要正確地格式化java程序的輸出。該程序似乎無法正常工作。在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); 


    } 

} 

[/代碼]

+0

你試圖達到什麼樣的格式? –

+0

小數點右側不得超過2個位置 – MikeinFl

回答

1

嘗試這種情況:

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

%1.2f輪十進制數爲2位小數。

你應該看看format specifiers