for(int i = 1; i <=4;i++){
System.out.print("输入整数: ");
num = input.nextInt();
if(i==1){
a = num;
}else if(i==2){
b = num;
}else if(i==3){
c = num ; 
}else if(i==4){
d = num;
}
}
if(a>b){
t = a;
a = b;
b = t;
}else if(a>c){
t = a;
a = c;
c = t;
}else if(a>d){
t = a;
a = d;
d = t;
}else if(b>c){
t = b;
b = c;
c = t;
}else if(b>d){
t = b;
b = d;
d = t;
}else if(c>d){
t = c;
c = d;
d = t;
}//请大家指导一下 小弟刚刚学......

解决方案 »

  1.   

    投机一下,不用数组用List也行,list.add(a).../b/c/d
    Collections.sort(list)
    估计lz是要写排序算法吧,查一下资料
      

  2.   

    不用数组?那可否用动态数组ArrayList?
      

  3.   

         最笨的办法是定义四个int变量了,接收进来数值之后,来进行比较。
          虽然lz思路是对的,可惜写的不是对的!
          public static void main(String[] args) {
    if(args.length < 4) throw new IllegalArgumentException("参数个数小于4");
    int a = 0, b = 0, c = 0, d = 0;
    int t = 0;
    for (int i = 0; i < 4; i++) {
    if (i == 0) {
    a = Integer.valueOf(args[0]);
    } else if (i == 1) {
    b = Integer.valueOf(args[1]);
    } else if (i == 2) {
    c = Integer.valueOf(args[2]);
    } else if (i == 3) {
    d = Integer.valueOf(args[3]);
    }
    }
    if (a > b) {
    t = a;
    a = b;
    b = t;

    if (a > c) {
    t = a;
    a = c;
    c = t;
    }
    if (a > d) {
    t = a;
    a = d;
    d = t;

    /**a此时比b,c ,d 都小**/
    if (b > c) {
    t = b;
    b = c;
    c = t;
    }
    if (b > d) {
    t = b;
    b = d;
    d = t;

    /***同样的思路处理b,c**/
    if (c > d) {
    t = c;
    c = d;
    d = t;
    }

    System.out.println(a + "  " + b + "  " + c + "  " + d);
    }          运行 java FoutInt  9 7 5 4
         得到结果: 4 5 7 9
      

  4.   

    还是赞同2楼的,用TreeSet也可以排序,算法都狠简单,而且效率高..