黑马程序员技术交流社区

标题: 郁闷死了 [打印本页]

作者: cishengbuhuan    时间: 2015-3-11 15:17
标题: 郁闷死了
本帖最后由 cishengbuhuan 于 2015-3-11 15:21 编辑

class SortDemo
{
        public static void main(String[] args)
        {
                int[] arr = {12,3,5,7,99,0,6};
                printArray(arr);
                bubbleSort(arr);
                printArray(arr);
        }
        public static void bubbleSort(int arr)
        {
                for (int x=0; x<arr.length-1; x++)
                {
                        for (int y=0; y<arr.length-x-1; y++)
                        {
                                if (arr[y]>arr[y+1])
                                {
                                        int temp = arr[y];
                                        arr[y] = arr[y+1];
                                        arr[y+1] = temp;
                                }
                        }
                }
        }
        public static void printArray(int[] arr)
        {
                System.out.print("[");
                for (int x = 0; x<arr.length; x++)
                {
                        if (x!=arr.length-1)
                                System.out.print(arr[x]+",");
                        else
                                System.out.println(arr[x]+"]");
                }
        }
}


111.png (60.92 KB, 下载次数: 11)

111.png

作者: 艺多不压身丶    时间: 2015-3-11 15:37
bubblieSort函数应该接受int类型的数组,你接受的是int啊
  1. class SortDemo {
  2.     public static void main(String[] args) {
  3.         int[] arr = {12,3,5,7,99,0,6};
  4.         printArray(arr);
  5.         bubbleSort(arr);
  6.         printArray(arr);
  7.     }
  8.     public static void bubbleSort(int[] arr){
  9.         for (int x=0; x<arr.length-1; x++){
  10.                 for (int y=0; y<arr.length-x-1; y++){
  11.                 if (arr[y]>arr[y+1]){
  12.                     int temp = arr[y];
  13.                     arr[y] = arr[y+1];
  14.                     arr[y+1] = temp;
  15.                 }
  16.             }
  17.         }
  18.     }
  19.         public static void printArray(int[] arr){
  20.         System.out.print("[");
  21.         for (int x = 0; x<arr.length; x++){
  22.             if (x!=arr.length-1)
  23.                 System.out.print(arr[x]+",");
  24.             else
  25.                 System.out.println(arr[x]+"]");
  26.         }
  27.     }
  28. }
复制代码

作者: 若辰    时间: 2015-3-11 15:44
少打中括号了。
作者: cishengbuhuan    时间: 2015-3-11 15:51
哦哦,发现了,还是我敲漏了一个,谢谢
作者: android-liu    时间: 2015-3-11 17:09
哥们 你好好看看 你定义 public static void bubbleSort(int arr)这个方法的形式参数类型是什么?是int  而你方法里却把它当成数组来用,能不出错吗




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