我应该为家庭作业做一个程序,它接受用户输入并显示特定的输出。我创建了一个数组队列,当我为它提供一个名称时,当我显示queue时,有时只会出现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;
}发布于 2015-11-10 03:59:05
这个ArrayQueue类是你自己的实现吗?我怀疑可能有问题,否则您的displayMenu代码看起来是正确的。下面是一个可以测试的SSCCE类。我已经为队列使用了ArrayDeque。
import java.util.ArrayDeque;
import javax.swing.*;
public class QueueTest {
public static MyQueue<String> displayMenu(MyQueue<String> 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)
switch(choice) {
case 0:
String name = JOptionPane.showInputDialog(null, "Enter person's name");
queue.offer(name);
break;
case 1:
JOptionPane.showMessageDialog(null, queue.poll() + " is next in line.");
break;
case 2:
JOptionPane.showMessageDialog(null, queue.peek() + " is in front of the line.");
break;
case 3:
String output = queue.toString();
JOptionPane.showMessageDialog(null, output);
break;
case 4:
System.out.println("Exiting program");
break;
default:
System.out.println("Choice not valid");
break;
}
}
return queue;
}//close displayMenu
static class MyQueue<T>
extends ArrayDeque<T> {
public MyQueue() {
}
}
public static void main(String[] args) {
MyQueue<String> myQueue = new MyQueue<String>();
displayMenu(myQueue);
}
}https://stackoverflow.com/questions/33601918
复制相似问题