私は比較的経験の浅いプログラマーで、宿題用の基本的な配列生成/検索プログラムに取り組んでいます。完全に機能していますが、検索キーを設定すると、ランダムにフリーズします(検出できる例外やエラーメッセージはスローされません)。ただし、本当の問題は、同じことをしてもエラーを常に再現できるとは限らないことです。私は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
}