有时程序运行正常,有时程序不做任何事情

时间:2018-02-03 22:30:40

标签: 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[] 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));
  }
 }

1 个答案:

答案 0 :(得分:1)

你的意思是总是将Diver1的第二个位置设置为临时变量吗?  因为这就是你所说的try { // register driver Class.forName("org.h2.Driver"); // open connection, in-memory database Connection conn = DriverManager.getConnection("jdbc:h2:mem:"); conn.setAutoCommit(false); // create table PreparedStatement createSample = conn.prepareStatement("CREATE TABLE sample (id int not null auto_increment, txt varchar(128))"); createSample.executeUpdate(); createSample.close(); // prepare insert statement PreparedStatement insertStatement = conn.prepareStatement("INSERT INTO sample (txt) VALUES (?)", Statement.RETURN_GENERATED_KEYS); // dummy list with texts List<String> dummyTexts = Arrays.asList("Entry A", "Entry B", "Entry C", "Entry D", "Entry E"); // insert data for (String dummyText : dummyTexts) { insertStatement.setString(1, dummyText); insertStatement.executeUpdate(); // get generated key ResultSet generatedKeys = insertStatement.getGeneratedKeys(); if ((generatedKeys != null) && (generatedKeys.next())) { int generatedKey = generatedKeys.getInt(1); System.out.println("generated key " + generatedKey + " for entry '" + dummyText + "'"); } } // commit conn.commit(); insertStatement.close(); // select data PreparedStatement selection = conn.prepareStatement("SELECT id, txt FROM sample"); ResultSet selectionResult = selection.executeQuery(); while (selectionResult.next()) { System.out.println("id: " + selectionResult.getInt(1) + ", txt: '" + selectionResult.getString(2) + "'"); } selectionResult.close(); selection.close(); // close connection conn.close(); } catch (Exception ex) { ex.printStackTrace(); } 。也许试着说Diver1[1] = temp之类的东西。这适用于您尝试排序的两个点。

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

更新: 您的代码永远不会到达打印语句,因为它会卡在您的Diver1[positionCheck2] = temp循环中。 while仍然是假的,所以它从不存在循环。正如我上面提到的,修复你的“交换”,最终应该解决。