我是一名相对缺乏经验的程序员,从事基本数组生成/搜索程序的家庭作业。我已经完美地工作了,但是在我设置搜索键后它会随机冻结(不会抛出我可以检测到的任何异常或错误消息)。但真正的问题是,我不能总是通过做同样的事情来重现错误。我正在编程并在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 = "\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
}
答案 0 :(得分:1)
执行tempMax/2 - tempMin/2
不会让你获得中点。考虑一个简单的示例:如果tempMin = 2
和tempMax = 5
,则tempMax/2 - tempMin/2 = 5/2 - 2/2 = 2 - 1 = 1
。
获得没有溢出的中点的典型方法是mid = (min + max) >>> 1
。