日期:2014-05-20 浏览次数:20631 次
import java.io.*;
public class InputTest {
public static void sort(double[] a)
{
for(int i=0;i<a.length-1;i++)
for(int j=i+1;j<a.length;j++)
{
if(a[i]<a[j])
{
double temp;
temp=a[i];a[i]=a[j];a[j]=temp;
}
}
}
public static void main(String[] args)
{
BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));
double[] array=new double[3];
try{
for(int i=0;i<array.length;i++)
{
System.out.println("请输入第"+(i+1)+"个数");
array[i]=Double.parseDouble(stdin.readLine());
}
}catch(Exception e){}
sort(array);
System.out.print("排序后的结果为:");
for(int i=0;i<array.length;i++)
System.out.print(array[i]+" ");
}
}
public static void main(String[] args) {
String input = null;
Scanner in = new Scanner(System.in);
System.out.println("请输入n个数字,数字之间用空格分隔,如:12.34 34 -4");
while(!(input = in.nextLine()).matches("^(\\s*-?\\d+(\\.\\d+)?\\s*)+$"))
System.out.println("输入有误,请重输");
List<BigDecimal> result = new LinkedList<BigDecimal>();
for (String s : input.trim().split("\\s+"))
result.add(new BigDecimal(s));
Collections.sort(result, new Comparator<BigDecimal>() {
@Override
public int compare(BigDecimal o1, BigDecimal o2) {
return o2.compareTo(o1);
}
});
System.out.println(result);
}