日期:2014-05-20  浏览次数:20622 次

我是初学者
随机输入三个数,要求他们从大到小的顺序从控制台输出,用Java语言程序来解决。
------最佳解决方案--------------------
public static void main(String[] args) {
System.out.println("请输入三个整形数字");
Scanner scan = new Scanner(System.in);
int[] a = new int[3];
a[0] = scan.nextInt();
a[1] = scan.nextInt();
a[2] = scan.nextInt();
Arrays.sort(a);
for(int temp:a){
System.out.println(temp);
}
scan.close();
}

没有输入合法性等问题的校验。这样的代码楼主最好还是自己再写一遍。
------其他解决方案--------------------
话说实现代码最好还是自己写。这里有用到的技术有:
1.控制台输入数字,涉及输入流。
2.根据大小排序,涉及排序算法(例如冒泡)
只要你谷歌明白了这两种的基本原理你也就自己能搞定了。
------其他解决方案--------------------

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]+" ");        
    }
 
}

------其他解决方案--------------------
没事时多看看书,自己试着练习一下,想要学好编程,多练习是必须的。
------其他解决方案--------------------
3 楼的答案太复杂,四楼的话意义深远,我喜欢。
------其他解决方案--------------------