嗨,大家我想实现一个圆形阵列,但有些东西不完全正确。我不确定是否在添加方法或显示中。当你运行调试器时,数字在那里,但我不能让他们按顺序。请你能看看并给我一个反馈。谢谢。圆形队列阵列
public static void main (String[] args)
{
Scanner input = new Scanner (System.in);
Queue q1 = new Queue();
int choice = 0;
do
{
System.out.println ("Menu:");
System.out.println ("1: Add");
System.out.println ("2: Remove");
System.out.println ("3: Display");
System.out.println ("4: Exit");
System.out.print ("\nChoice: ");
choice = input.nextInt();
switch (choice)
{
case 1:
System.out.print ("\nEnter a number: ");
int num = input.nextInt();
q1.add (num);
break;
case 2:
q1.remove();
break;
case 3:
q1.display();
break;
case 4:
System.out.println ("Good Bye");
break;
default:
System.out.println ("Wrong choice!");
break;
}
} while (choice != 4);
}
}
公共类队列 {
private final int SIZE;
private int first;
private int last;
private int[] q;
public Queue()
{
SIZE = 5;
q = new int[ SIZE ];
first = 0;
last = 0;
}
public boolean isFull()
{
return last == SIZE;
}
public boolean isEmpty()
{
return last == first;
}
public void add (int x)
{
if ( ! isFull())
{
q[ (first + last) % q.length ] = x;
last ++;
} else
{
System.out.println ("\nThe queue is full!");
}
}
int remove()
{
int x = 0;
if ( ! isEmpty())
{
x = q[ first ];
first = (first + 1) % q.length;
last --;
} else
{
System.out.println ("The queue is empy");
}
return x;
}
public void display()
{
if ( ! isEmpty())
{
for (int i = first; i < SIZE; i ++)
{
System.out.println (q[ i ]);
}
} else
{
System.out.println ("The queue is emptry");
}
}
欢迎来到Stack Overflow!寻求调试帮助的问题(“为什么这个代码不工作?”)必须在问题本身中包含所需的行为,特定的问题或错误以及必要的最短代码**。没有明确问题陈述的问题对其他读者无益。请参阅:[如何创建最小,完整和可验证示例](http://stackoverflow.com/help/mcve)。 –