Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 有时程序运行正常,有时程序运行不正常';我什么也不做_Java_Arrays_Bubble Sort - Fatal编程技术网

Java 有时程序运行正常,有时程序运行不正常';我什么也不做

Java 有时程序运行正常,有时程序运行不正常';我什么也不做,java,arrays,bubble-sort,Java,Arrays,Bubble Sort,我试图得到一个程序,使2个随机数数组,然后使用气泡法排序,这样以后我可以很容易地挑出3个中间数。当我延迟eclipse并第一次运行它时,但每当我尝试再次运行它时,控制台都不会显示任何内容,我假设程序仍在运行,因为我可以选择终止 import java.util.Arrays; import java.util.Random; public class DiversCalc { public static void main(String[] args){ int[] Dive

我试图得到一个程序,使2个随机数数组,然后使用气泡法排序,这样以后我可以很容易地挑出3个中间数。当我延迟eclipse并第一次运行它时,但每当我尝试再次运行它时,控制台都不会显示任何内容,我假设程序仍在运行,因为我可以选择终止

import java.util.Arrays;
import java.util.Random;

public class DiversCalc {
    public static void main(String[] args){

    int[] Diver1 = new int[7];
    int[] Diver2 = new int[7];

    Random rand = new Random();

    for (int positionInArray = 0; positionInArray < Diver1.length; 
        positionInArray++) {
       int diverScore1 = rand.nextInt(10);
       Diver1[positionInArray] = diverScore1;
    }

    for (int positionInArray2 = 0; positionInArray2 < Diver2.length; 
        positionInArray2++) {
       int diverScore1 = rand.nextInt(10);
       Diver2[positionInArray2] = diverScore1;
    }

    int temp = 0;

    boolean checker = false;
    boolean checker2 = false;

    while(checker==false){
       checker=true;

       for(int positionCheck1 = 0; positionCheck1 < Diver1.length-1; 
           positionCheck1++){

          if(Diver1[positionCheck1] > Diver1[positionCheck1+1]){

              temp = Diver1[positionCheck1+1];

              Diver1[positionCheck1+1] = Diver1[positionCheck1];

              Diver1[1] = temp;

              checker=false; 

           }
       }
    }

    while(checker2==false){
       checker2=true;

       for(int positionCheck2 = 0; positionCheck2 < Diver2.length-1; 
           positionCheck2++){

          if(Diver2[positionCheck2] > Diver2[positionCheck2+1]){

               temp = Diver2[positionCheck2+1];

               Diver2[positionCheck2+1] = Diver2[positionCheck2];

               Diver2[1] = temp;
               checker2=false; 
          }
       }
    }

    System.out.println(Arrays.toString(Diver1));

    System.out.println(Arrays.toString(Diver2));
  }
 }
导入java.util.array;
导入java.util.Random;
公共级潜水员{
公共静态void main(字符串[]args){
int[]1=新的int[7];
int[]2=新的int[7];
Random rand=新的Random();
对于(int-positionInArray=0;positionInArray<1.1长度;
(光线++){
int-1=兰德·奈克斯汀(10);
潜水员1[PositionArray]=潜水员1;
}
对于(int PositionArray2=0;PositionArray2<2.2长度;
位置(RAY2++){
int-1=兰德·奈克斯汀(10);
潜水员2[PositionArray 2]=潜水员1;
}
内部温度=0;
布尔检查器=假;
布尔checker2=false;
while(checker==false){
checker=true;
对于(int positionCheck1=0;positionCheck1<1.length-1;
位置检查(1++){
如果(潜水员1[positionCheck1]>潜水员1[positionCheck1+1]){
温度=潜水员1[位置检查1+1];
潜水员1[positionCheck1+1]=潜水员1[positionCheck1];
潜水员1[1]=温度;
checker=false;
}
}
}
while(checker2==false){
checker2=true;
对于(int positionCheck2=0;positionCheck2<2.length-1;
位置检查(2++){
如果(潜水员2[positionCheck2]>潜水员2[positionCheck2+1]){
温度=潜水员2[位置检查2+1];
潜水员2[positionCheck2+1]=潜水员2[positionCheck2];
潜水员2[1]=温度;
checker2=假;
}
}
}
System.out.println(Arrays.toString(Diver1));
System.out.println(Arrays.toString(Diver2));
}
}

您的意思是始终将Diver1的第二个位置设置为温度变量吗? 因为这就是你说的
Diver1[1]=temp
。也许可以试着说一些类似于
Diver1[positionCheck2]=temp
的话。这适用于您尝试排序的两个点

另外,我不相信你的冒泡排序是完全冒泡排序。它似乎只对数字数组进行一次迭代,将最大值冒泡到数组末尾,然后停止。完全冒泡排序将继续这些迭代,将max元素一直移动到末尾,然后将第二个max元素从末尾移动到第二个点,然后将第三个max元素从末尾移动到第三个点,等等,直到排序为止

更新:
您的代码永远不会到达打印语句,因为它卡在
while
循环中
while(checker==false)
继续为false,因此它在循环中不存在。正如我上面提到的,修复您的“交换”,最终这应该会解决。

您是否尝试过调试以缩小问题范围?编写测试套件?至少对代码进行缩进,以便更容易查看。如果使用调试器逐步执行此操作,您将立即看到发生了什么。我将如何在eclipse中使用调试器来查看出错的地方。设置断点。逐步完成代码。观察程序流程。检查您需要检查的任何变量。和调试其他程序一样。@ThomasP请看我的答案,我刚刚也更新了它。