Java 为什么我的冒泡排序方法不起作用?

Java 为什么我的冒泡排序方法不起作用?,java,arrays,sorting,Java,Arrays,Sorting,对不起,我还在学习编程。Java只是停止了,似乎正在处理一些东西。它说“构建java应用程序Javaapplication2”,然后坐在那里什么也不做。我做了什么导致了这一切 package javaapplication2; public class JavaApplication2 { public static void main(String[] args) { int a [] = {1,2,3}; int c [] = Sortarray.s

对不起,我还在学习编程。Java只是停止了,似乎正在处理一些东西。它说“构建java应用程序Javaapplication2”,然后坐在那里什么也不做。我做了什么导致了这一切

package javaapplication2;

public class JavaApplication2 {

    public static void main(String[] args) {
       int a [] = {1,2,3};
       int c [] =  Sortarray.sortlowhigh(a);

       int i = 0;
      while (i<c.length){

          System.out.println("array is" + c[i]);
        i++;
      }
    }

}

package javaapplication2;
public class Sortarray {
public static int[] sortlowhigh(int a[])
    {
        int i = 0;
       int j = 0;
      while(j<a.length){

        while(i<a.length){
            if (a[i]>a[i+1]){
          /* store low  value in temp*/
             int temp = a[i+1];
          /* assign low value  to be the higher value*/
             a[i+1] = a[i];
          /* assign the old higher value to be the lower value stored in temp*/
             a[i]=temp;

            }
            j++;
        }

    } 
       return a;
}
}
PackageJavaApplication2;
公共类JavaApplication2{
公共静态void main(字符串[]args){
int a[]={1,2,3};
int c[]=Sortarray.sortlowhigh(a);
int i=0;

而(i当j的值增加时,应重置i的值。 数组的大小为3。但i的值已经是3。因此它在[i+1]中获取垃圾值。
希望这有帮助。我还没有尝试过代码。

在sortlowhigh while循环中,I=a.length,但我从未更改它始终保持0。

/*根据给定值更改代码*/
/*change your code as given*/
public static int[] sortlowhigh(int a[]){     
int i = 0;
 int j = 0; int temp=0;
 while(j<(a.length-1))
   {
           i=0;
           while(i<a.length-j-1)
           {
               if(a[i]>a[i+1])/* For descending order use < */
               {

                   temp = a[i];
                   a[i]=a[i+1];
                   a[i+1] = temp;
               }
               i++;
           }
           j++;
   }
 return a;
}
公共静态int[]sortlowhigh(int a[]){ int i=0; int j=0;int temp=0;
而(j我尝试了你的代码,我可以看到Sortaray类中有一个无限循环。变量I从未增加。 请尝试以下代码:

    public static int[] sortlowhigh(int a[])
    {
        int i = 0;
        int j = 0;
        int temp;
        while(j< (a.length -1) ){
           i = 0;
            while(i< (a.length - j - 1)){
                if (a[i] > a[i+1]){
      /* store low  value in temp*/
                    temp = a[i];
      /* assign low value  to be the higher value*/
                    a[i] = a[i+1];
      /* assign the old higher value to be the lower value stored in temp*/
                    a[i+1]=temp;
                }
               i++;
            }
            j++;
        }
        return a;
    }
public static int[]sortlowhigh(int a[])
{
int i=0;
int j=0;
内部温度;
而(j<(a.长度-1)){
i=0;
而(i<(a.长度-j-1)){
如果(a[i]>a[i+1]){
/*在温度中存储低值*/
温度=a[i];
/*将低值指定为高值*/
a[i]=a[i+1];
/*将旧的较高值指定为存储在temp中的较低值*/
a[i+1]=温度;
}
i++;
}
j++;
}
返回a;
}

这是在编译过程中发生的,对吗?是的。我按了“运行”按钮。谢谢。我做了更改,但它仍然停留在那里。这只会影响运行时行为。OP在编译时遇到问题。好的,谢谢,在[I]之后=temp我添加了行I++,但它仍然停留在那里!您必须将j++;移出I循环,并在其位置添加I++;,以使其工作