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

初学者求大神指导
小弟初学Java遇到一个难题,求大神指导。

题目:从键盘输入三个数,并赋值,比较之后按从小到大的顺序输出。

------解决方案--------------------
用Scanner,其它的自己练一下手
------解决方案--------------------
Java code

import java.util.Scanner;


public class Test {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int[] a = new int[3];
        a[0] = scan.nextInt();
        a[1] = scan.nextInt();
        a[2] = scan.nextInt();
        sort(a);
        show(a);
    }
    public static void sort(int[] a){ //冒泡排序
        int n = a.length;
        for (int i = n; i > 1; i--){
            for (int j = 0; j < i-1; j++) {
                if ( a[j] > a[j+1]) {
                    int x = a[j];
                    a[j] = a[j+1];
                    a[j+1] = x;
                }
            }
        }
    }
    public static void show(int[] a){
        for (int i = 0; i < a.length; i++) {
            System.out.print(a[i]+" ");
        }
        System.out.println();
    }

}

------解决方案--------------------
import java.util.Scanner;
public class SortNum {
public static void main(String[] args) {
int a, b, c;
int t; //临时变量
Scanner input = new Scanner(System.in);
System.out.println("请输入a的值:");
a = input.nextInt();
System.out.println("请输入b的值:");
b = input.nextInt();
System.out.println("请输入c的值:");
c = input.nextInt();
System.out.println("排序前" + a + " " + b + " " + c);
if (a > b) {
t = a;
a = b;
b = t;
}
if (a > c) {
t = a;
a = c;
c = t;
}
if (b > c) {
t = b;
b = c;
c = t;
}
System.out.println("排序后" + a + " " + b + " " + c);

}
}
------解决方案--------------------
PS:记得要结贴噢。