A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© gtf 中级黑马   /  2014-7-2 16:22  /  964 人查看  /  9 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

自己编写的排序的程序,用于把一个数组按从大到小的顺序打印出来。
先写了一个函数式用来换位的。
下面一个函数是把数组里的元素全部遍历,把最大的放前面。
class  test4
{
        public static void main(String[] args)
        {
                int[] arr=new int[]{3,12,44,67,9,7,5,8};
                sort(arr);
        }
}
void huanwei(int[],b;int,c;int,d)
{int temp=b[c];
        b[c]=b[d];
        b[d]=temp;

}
void sort(int[],a)
{
        for (int x = 0;x<a.length ;x++ )
        {        for (int y=0;y<a.length-1;y++ )
                {if(a[x]<a[y])
                huanwei(a,x,y);

                }
        }
        for (int x=0;x<a.length ;x++ )
        {System.out.print(a[x]+",");
        }
}
但是编译的时候出现了好多需要class,interface或enum的情况。
请指点哪地方错了?

9 个回复

倒序浏览
静态方法内是不能调用非静态方法的,所以你要把sort和huanwei都改成static。还有,你的huanwei和sort函数的形参列表中,逗号只用来分隔多个参数,而同一参数的类型和参数名之间不能加逗号。比如sort方法应该写成:void sort(int[] a)
回复 使用道具 举报
本帖最后由 燿陚√揚葳 于 2014-7-2 16:38 编辑

void huanwei(int[],b;int,c;int,d)//int[] b,int c, int d
void sort(int[],a)//是int[] a
书写上的错误,楼主应该知道的吧
没有加静态,其余好像没有了
  1. class test4
  2. {
  3. public static void main(String[] args)
  4. {
  5. int[] arr=new int[]{3,12,44,67,9,7,5,8};
  6. sort(arr<FONT color=orange>);//因为主函数是静态的所以sort方法也带是静态的,静态只能访问静态
  7. </FONT>}
  8. //}写错位置了
  9. static void huanwei(int[] b,int c,int d)
  10. {int temp=b[c];
  11. b[c]=b[d];
  12. b[d]=temp;

  13. }
  14. static void sort(int[] a)//
  15. {
  16. for (int x = 0;x<a.length ;x++ )
  17. { for (int y=0;y<a.length-1;y++ )
  18. {if(a[x]<a[y])
  19. huanwei(a,x,y<FONT color=orange>);//因为sort方法要改成静态的所以,这个huanwei方法也要是静态的
  20. </FONT>}
  21. }
  22. for (int x=0;x<a.length ;x++ )
  23. {System.out.print(a[x]+",");
  24. }
  25. }
  26. }
复制代码


回复 使用道具 举报
燿陚√揚葳 发表于 2014-7-2 16:35
void huanwei(int[],b;int,c;int,d)//int[] b,int c, int d
void sort(int[],a)//是int[] a
书写上的错误, ...

class  test4
{
        public static void main(String[] args)
        {
                int[] arr=new int[]{3,12,44,67,9,7,5,8};
                sort(arr);
        }

void huanwei(int[] b,int c,int d)
{int temp=b[c];
        b[c]=b[d];
        b[d]=temp;
}
static void sort(int[] a)
{
        for (int x = 0;x<a.length ;x++ )
        {        for (int y=0;y<a.length-1;y++ )
                {if(a[x]<a[y])
                huanwei(a,x,y);

                }
        }
        for (int x=0;x<a.length ;x++ )
        {System.out.print(a[x]+",");
        }
}
把上边的错误都改了,现在编译的时候还有一个错误,标记的是最最下边的大括号 } ,说解析时已到达文件结尾。这是什么情况啊? 求指点。
回复 使用道具 举报
fantacyleo 发表于 2014-7-2 16:28
静态方法内是不能调用非静态方法的,所以你要把sort和huanwei都改成static。还有,你的huanwei和sort函数的 ...

谢谢了            
回复 使用道具 举报
本帖最后由 燿陚√揚葳 于 2014-7-2 18:57 编辑
gtf 发表于 2014-7-2 18:46
class  test4
{
        public static void main(String[] args)


在最下面少了个大括号
回复 使用道具 举报
以上指点都很详细了,我补充一点,关于算法的。其实这个程序的循环次数太多了,有点麻烦。
for (int x = 0;x<a.length ;x++ )
{
   for (int y=a.length-1;y>x;y--)
   {
       if(a[x]<a[y])
       huanwei(-------);//因为sort方法要改成静态的所以,这个huanwei方法也要是静态的
   }
}
回复 使用道具 举报
错就错在你不知道,静态方法只能调用静态方法,主函数为静态,同类中的其他方法只有被static修饰,才能被主函数调用。并且你的函数定义也不对,不能定义在类外!
回复 使用道具 举报
修改后为:class  A
{
        public static void main(String[] args)
        {
                int[] arr=new int[]{3,12,44,67,9,7,5,8};
                sort(arr);
        }

public static void huanwei(int[] b,int c,int d)
{int temp=b[c];
        b[c]=b[d];
        b[d]=temp;

}
public static void sort(int[] a)
{
        for (int x = 0;x<a.length ;x++ )
        {        for (int y=0;y<a.length-1;y++ )
                {if(a[x]<a[y])
                huanwei(a,x,y);

                }
        }
        for (int x=0;x<a.length ;x++ )
        {System.out.print(a[x]+",");
        }
}
}
回复 使用道具 举报
你好,下面是我改了的代码,改过的地方已经指出来了。有改错的地方还望见谅。
  1. class  Cg
  2. {
  3.         public static void main(String[] args)
  4.         {
  5.                 int[] arr=new int[]{3,12,44,67,9,7,5,8};
  6.                 sort(arr);//sort方法应该是静态的;
  7.         }
  8. //}多出
  9. static void huanwei(int[] b,int c,int d)//去掉逗号,将分号改为逗号,改成静态方法
  10. {int temp=b[c];
  11.         b[c]=b[d];
  12.         b[d]=temp;

  13. }
  14. static void sort(int[] a)//去掉逗号,改成静态方法
  15. {
  16.         for (int x = 0;x<a.length ;x++ )
  17.         {        for (int y=x;y<a.length-1;y++ )//排序是从小到大,改为y=x
  18.                 {if(a[x]<a[y])
  19.                 huanwei(a,x,y);

  20.                 }
  21.         }
  22.         for (int x=0;x<a.length ;x++ )
  23.         {System.out.print(a[x]+",");
  24.         }
  25. }
  26. }
复制代码
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马