【发布时间】:2018-02-03 22:30:39
【问题描述】:
我正在尝试编写一个程序来制作 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));
}
}
【问题讨论】:
-
您是否尝试过调试以缩小问题范围?写一个测试套件?至少缩进代码,以便于查看。
-
如果您使用调试器逐步完成此操作,您将立即看到发生了什么。
-
如何在 Eclipse 中使用调试器来查看问题所在。
-
设置断点。单步执行代码。观察程序流程。检查您需要检查的任何变量。与调试任何其他程序一样。
-
@ThomasP 请看我的回答,我也刚刚更新了。
标签: java arrays bubble-sort