黑马程序员技术交流社区

标题: 请问我这个排序哪里排错了? [打印本页]

作者: 马姗姗    时间: 2013-8-19 10:16
标题: 请问我这个排序哪里排错了?
本帖最后由 黄兴旺 于 2013-8-19 20:00 编辑

public class example{
  public static void main(String args[]){
    int a=9,b=5,c=7,t=0;
    if(b<a){
      t=a;
      a=b;
      b=a;
    }
    if(c<a){
      t=a;
      a=c;
      c=t;
    }
    if(c<b){
      t=b;
      b=c;
      c=t;
    }
    System.out.println("a,b,c从小到大排列:"+a+","+b+","+c);
  }
}
作者: 白堇翎    时间: 2013-8-19 10:25

  1. public class example{
  2.   public static void main(String args[]){
  3.     int a=9,b=5,c=7,t=0;
  4.     if(b<a){//true
  5.       t=a;// t = 9
  6.       a=b;//a = 5
  7.       b=a;// b = 5,a = 5,t = 9
  8.     }
  9.     if(c<a){//false,不执行 c = 7 ,a = 5
  10.       t=a;
  11.       a=c;
  12.       c=t;
  13.     }
  14.     if(c<b){//false,不执行 c = 7, b =5
  15.       t=b;
  16.       b=c;
  17.       c=t;
  18.     }
  19.     System.out.println("a,b,c从小到大排列:"+a+","+b+","+c);
  20.   }
  21. }
复制代码

作者: 朱艳    时间: 2013-8-19 18:22
楼主,你的问题已经解决了,把状态改成已解决吧,最好^_^
作者: 黄文军    时间: 2013-8-19 18:39
  1. public class Example{
  2.   public static void main(String args[]){
  3.     int a=9,b=5,c=7,t=0;
  4.     if(b<a){
  5.       t=a;
  6.       a=b;
  7.       b=t;   //你这个位置是b = a  没有完成置换
  8.     } //  5 9 7
  9.     if(c<a){
  10.       t=a;
  11.       a=c;
  12.       c=t;
  13.     } // 5 7 9
  14.     if(c<b){
  15.       t=b;
  16.       b=c;
  17.       c=t;
  18.     }   //5 9 7
  19.     System.out.println("a,b,c从小到大排列:"+a+","+b+","+c);
  20.   }
  21. }
复制代码





欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2