我有一個用於計算數據範圍的GUI Java代碼,例如,如果輸入了兩個值2.444
和3.555
,結果將是一個長整數1.11100000...
等。如何指定多少個應顯示小數點後的數字? (例如:%.2f
)Java:指定小數點後的數字
這是我的代碼:
public class Range
{
public static void main(String args[])
{
int num=0; //number of data
double d; //the data
double smallest = Integer.MAX_VALUE;
double largest = Integer.MIN_VALUE;
double range = 0;
String Num =
JOptionPane.showInputDialog("Enter the number of data ");
num=Integer.parseInt(Num);
for(int i=0; i<num; i++)
{
String D =
JOptionPane.showInputDialog("Enter the data ");
d=Double.parseDouble(D);
if(d < smallest)
smallest = d;
if(d > largest)
largest = d;
}
range = largest - smallest ; //calculating the range of the input
JOptionPane.showMessageDialog(null,"Range = "+smallest+"-"+largest+" = "+range,"Range",JOptionPane.PLAIN_MESSAGE);
}
}
是這個語句會在gui中顯示結果? – 2011-12-27 16:31:44
此方法返回一個字符串表示形式,以便您可以替換'showMessageDialog'中的代碼。 – Howard 2011-12-27 16:34:45