gpt4 book ai didi

Java UI 程序随机卡住

转载 作者:行者123 更新时间:2023-11-30 11:37:50 25 4
gpt4 key购买 nike

我是一个相对缺乏经验的程序员,正在为家庭作业开发一个基本的数组生成/搜索程序。我让它工作得很好,但在我设置搜索键后它会随机卡住(不会抛出任何我能检测到的异常或错误消息)。然而,真正的问题是我不能总是通过做同样的事情来重现错误。我正在 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
}

最佳答案

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

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

关于Java UI 程序随机卡住,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13885113/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com