Java UI 程序随机冻结



我是一个相对缺乏经验的程序员,致力于一个基本的数组生成/搜索程序。 我已经让它完美运行,但在我设置搜索键后它会随机冻结(不会抛出我可以检测到的任何异常或错误消息)。 但是,真正的问题是,我不能总是通过做同样的事情来重现错误。 我正在Eclipse中编程和运行程序。

这是我的程序的基本结构;为了简单起见,我只包含似乎导致问题的setter和按钮的实际代码。 我怀疑这很简单,但我认为没有理由让这段代码锁定一个程序。

public class ArraySearcher extends JPanel
                       implements ActionListener  {

private static final long serialVersionUID = 6449138670325520140L;
/**
 * A program description.
 */

// Fields (array and search parameters)
static int key;
static int arraySize;
static int min;
static int max;
static int midpoint;
// (Number of search steps taken by each search algorithm)
static int linSteps;
static int binSteps;
// (JButtons, JLabels, JTextFields, and the log for displaying results)
static JButton runButton, chKeyButton, newArrayButton, exitButton;
static JTextField lStepField, bStepField;
static JTextField keyField;
static JTextField arraySizeField;
static JTextField time;
static JTextArea log;
// (The arrays to be used)
static int[] randArray, sortArray;
// (Makes the output formatting a little easier to read)
public static String newline = "n", twolines = "nn";

// The constructor
public ArraySearcher() {
// Setting up the fields and GUI
    }

// Getters and setters
protected static int getKey() {
    return key;
}

protected static void setKey() {
    boolean success = false;
    // loop and try catch block to deal with the potential parsing exception
    while (success == false) {
        try {
            key = Integer.parseInt(JOptionPane.showInputDialog(
                    "Please enter the number younwish to search for:"));
            keyField.setText(Integer.toString(getKey()));
            success = true;
        }
        catch (NumberFormatException e) {
            JOptionPane.showMessageDialog(null, 
                    "There was a number format error.  Pleasen" +
                    "input only positive, whole numbers.");
        }
    }
}
    // More getters and setters...

public static void main(String[] args) {
    // Implement the GUI, all other work is handled from
    // there and from within the constructor
    theGUI();
}

private static void theGUI() {
// Set up the GUI and allow user to set min and
// max values for the random number generator
    }

@Override
public void actionPerformed(ActionEvent e) {
    //Handling of the run/restart button.
    if (e.getSource() == runButton) {
    }
    // Handling the Change Key button
    else if (e.getSource() == chKeyButton) {
        setKey();
        chKeyButton.setText("Change Key");
        linSearch(getRandArray());          // Implicit searches triggered by
        binSearch(getRandArray());          // selecting a new search key
    }
    // Handling the New Array button
    else if (e.getSource() == newArrayButton) {
    }
    // Handling of the exit button.
    else if (e.getSource() == exitButton) {
    }
}

// Method for building the array of random numbers; includes an implicit search
// which can be canceled (i.e. just build and return the array) by passing it
// a false condition when it's called
private void arrayBuilder(boolean fullRun) {
}

private void linSearch(int[] arrayIn) {
    // Linear search method
}

private void binSearch(int[] arrayIn) {
    // Binary search method
    int result = -1;            // Location of a positive match; initialized to an impossible result
    int tempMax = arraySize;    // Dynamic maximum index for progressive midpoint calculations
    int tempMin = 0;            // Dynamic minimum index
    int newMid = 0;             // Dynamic midpoint
    int count = 0;              // Counts the steps required to find value
    boolean success = false;    // A loop escape boolean
    log.append("RUNNING BINARY SEARCH" + newline);      
    log.append("SORTING ARRAY..." + twolines);
    sortArray = sort(arrayIn);              // Sort the array prior to searching
    // Array midpoint index calculation
    midpoint = tempMax/2 - tempMin/2;   // Calculation prevents buffer overflow; allows for nonzero minimum
    newMid = midpoint;
    // Search loop
    while (tempMin != tempMax && success == false) {
        if (sortArray[newMid] == key) {
            success = true;
            result = newMid;
            count++;
        }
        else if (sortArray[newMid] < key) {
            tempMin = newMid;
            newMid = tempMax/2 - tempMin/2;
            count++;
        }
        else if (sortArray[newMid] > key) {
            tempMax = newMid;
            newMid = tempMax/2 - tempMin/2;
            count++;
        }
    }
    binSteps = count;
    bStepField.setText(Integer.toString(binSteps));
    log.append(twolines);
    if (result != -1) {
        log.append("Success!  The number " + Integer.toString(key) + " was found " +
                "at array location " + result + "." + newline);
    }
    else if (result == -1) {
        log.append("Failure.  The number " + Integer.toString(key) + 
                " was not found in the array." + newline);
    }
    log.append("The binary search was completed in " + Integer.toString(binSteps) + 
            " steps." + newline + newline);
    log.setCaretPosition(log.getDocument().getLength());
}

private int[] sort(int[] arrayIn) {
    // Method for sorting the random array before
    // performing a binary search
}

tempMax/2 - tempMin/2不会让你得到中点。考虑一个简单的例子:如果tempMin = 2tempMax = 5,则tempMax/2 - tempMin/2 = 5/2 - 2/2 = 2 - 1 = 1

获得中点而不溢出的典型方法是 mid = (min + max) >>> 1

最新更新