我应该制作一个家庭作业程序,它接受用户输入并显示特定输出。我创建了一个数组队列,当我提供一个名称时,有时在显示队列时只会显示null。我不想直接回答,但我想了解阵列队列是如何工作的,所以如果有人能指出我正确的方向,我将非常感谢。
public static MyQueue displayMenu(MyQueue queue) {
// list of choices (array of Strings)
String[] array = { "Offer Person", "Poll Person", "Peek Person", "Display Queue", "Exit Program"};
int choice = 0;
// display loop
while (choice != array.length-1) {
choice = JOptionPane.showOptionDialog(null, // put in center of screen
"Press a Button", // message to user
"Queue (line) of People", // title of window
JOptionPane.YES_NO_CANCEL_OPTION, // type of option
JOptionPane.QUESTION_MESSAGE, // type of message
null, // icon
array, // array of strings
array[array.length - 1]); // default choice (last one)
if(choice == 0) {
String name = JOptionPane.showInputDialog(null, "Enter person's name");
queue.offer(name);
}
if(choice == 1) {
JOptionPane.showMessageDialog(null, queue.poll() + " is next in line.");
}
if(choice == 2) {
JOptionPane.showMessageDialog(null, queue.peek() + " is in front of the line.");
}
if(choice == 3) {
String output = queue.toString();
JOptionPane.showMessageDialog(null, output);
}
}
return queue;
}//close displayMenu
}
此代码是我class MyQueue<T> extends ArrayQueue<T> {
public String toString( ) {
String s = "";
for (int i = 0; i < currentSize; i++) {//loop through array and wrap around
endIndex = (endIndex + 1) % maxSize;
s += "\n"+array[i];
}
return s;
}
答案 0 :(得分:0)
这个ibSuave.Image = imgSuave * 255
类是你自己的实现吗?我怀疑可能有问题,您的ArrayQueue
代码看起来是正确的。这是一个你可以测试的SSCCE类。我已将displayMenu
用于队列。
ArrayDeque