【发布时间】:2011-12-26 18:50:37
【问题描述】:
我有一个用于计算数据范围的 GUI Java 代码,例如,如果输入了两个值 2.444 和 3.555,则结果将是一个 long double 1.11100000... 等。如何指定小数点后的位数点它应该显示? (例如:%.2f)
这是我的代码:
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);
}
}
【问题讨论】: