黑马程序员技术交流社区

标题: double数组排序 [打印本页]

作者: vincentgood    时间: 2014-4-18 12:30
标题: double数组排序
package airthmatic;
public class demo{
    public static void main(String[] args) {
        double n[]={9,1.2,5,3.2,1.1};
        orderNum(n);
    }
    /**
     * double 和 int 数字排序
     * @param n
     */
    public static void orderNum(double []n){        
        for(int i=0;i<n.length-1;i++){
            for(int j=0;j<n.length-1-i;j++){
                double temp=0;
                if(n[j]>n[j+1]){
                    temp=n[j+1];
                    n[j+1]=n[j];
                    n[j]=temp;
                }
            }
        }
        /**
         * 这里是过滤掉整数的double类型
         */
        for(int i=0;i<n.length;i++){
            int temp=(int)n[i];
            if(n[i]%temp==0){
                System.out.println(temp);
            }else{
                System.out.println(n[i]);
            }
        }
    }
}


作者: 杨庆雷    时间: 2014-4-18 14:53
  1. public class Demo{
  2.     public static void main(String[] args) {
  3.         double arr[]={3.22,1,3,5,4,7,12,85,222};
  4.         orderNum(arr);
  5.     }
  6.     /**
  7.      * double 和 int 数字排序
  8.      * @param n
  9.      */
  10.     public static void orderNum(double[] arr){  
  11.              double temp;
  12.              for(int x=0;x<arr.length;x++){
  13.              for(int y=0;y<arr.length-x-1;y++){
  14.                  if(arr[y]>arr[y+1]){
  15.                      temp=arr[y];
  16.                      arr[y]=arr[y+1];
  17.                      arr[y+1]=temp;
  18.                  }
  19.              }
  20. //             System.out.print(arr[x]+"\t");
  21.              }
  22. //             for (int i = 0; i < arr.length; i++) {
  23. //                     System.out.println("\n"+arr[i]);
  24. //                }
  25.              
  26.         /**
  27.          * 这里是过滤掉整数的double类型
  28.          */
  29.         for(int i=0;i<arr.length;i++){
  30.             if(arr[i]!=(int)arr[i])
  31.                 System.out.println(arr[i]);
  32.            
  33.         }
  34.         
  35.     }
  36. }
复制代码
代码改好了  





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