我是一个相对缺乏经验的程序员,从事家庭作业的基本数组生成/搜索程序。我让它工作得很好,但是在我设置搜索键后它会随机冻结(不会抛出任何我可以检测到的异常或错误消息)。然而,真正的问题是我不能总是通过做同样的事情来重现错误。我正在 Eclipse 中编程和运行程序。
这是我的程序的基本结构;为简单起见,我只包括似乎导致问题的设置器和按钮的实际代码。我怀疑这很简单,但我认为这段代码没有理由锁定程序。
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 = "\n\n";
// 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 you\nwish to search for:"));
keyField.setText(Integer.toString(getKey()));
success = true;
}
catch (NumberFormatException e) {
JOptionPane.showMessageDialog(null,
"There was a number format error. Please\n" +
"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
}