热土豆比赛:在这个程序中,你需要实现一个 热土豆比赛的一般模拟。在这个游戏中,儿童线 在一个圆圈内,并通过一个从邻居到邻居的物品的速度可以达到 。在游戏中的某个时刻,动作停止,并且将具有物品(马铃薯)的孩子从圈子中移除。 继续播放,直到只剩下一个孩子。在你的实现中 用户应该输入一个名字列表和一个常数num。你的程序 必须返回最后一个人的姓名,重复 以num计数。创建与收藏热土豆比赛
我需要做,但我无法找出如何停止,而在hotpotato类的enqueuer()
方法循环。如果我还有其他一些错误,请告诉我吗?
hotpotato类:
import java.util.*;
public class hotpotato
{
private static Scanner input1 = new Scanner(System.in);
private static NodeQueue<String> potato = new NodeQueue<String>();
private static Scanner input = new Scanner(System.in);
static int num;
public static void main(String[] args)
{
System.out.println("Enter names of the children.");
enqueuer(input1.next());
System.out.println("Enter the num");
num = input.nextInt();
potatothrower();
}
public static void enqueuer(String p)
{
String keyboard = input1.next();
while(!keyboard.equals("stop"))
{
potato.enqueue(p);
}
}
public static void potatothrower()
{
for(int i = 0; i< num; i++)
{
if(!potato.isEmpty()){
String tmp = potato.front();
potato.dequeue();
potato.enqueue(tmp);
}
else{
System.out.println("Queue is empty");
}
}
potato.dequeue();
}
}
节点类:
public class Node<E> {
// Instance variables:
private E element;
private Node<E> next;
/** Creates a node with null references to its element and next node. */
public Node() {
this(null, null);
}
/** Creates a node with the given element and next node. */
public Node(E e, Node<E> n) {
element = e;
next = n;
}
// Accessor methods:
public E getElement() {
return element;
}
public Node<E> getNext() {
return next;
}
// Modifier methods:
public void setElement(E newElem) {
element = newElem;
}
public void setNext(Node<E> newNext) {
next = newNext;
}
}
NodeQueue类别:
public class NodeQueue<E> implements Queue<E> {
protected Node<E> head;
protected Node<E> tail;
protected int size; // number of elements in the queue
public NodeQueue() { // constructs an empty stack
head = null;
tail = null;
size = 0;
}
public void enqueue(E elem) {
Node<E> node = new Node<E>();
node.setElement(elem);
node.setNext(null); // node will be new tail node
if (size == 0)
head = node; // special case of a previously empty queue
else
tail.setNext(node); // add node at the tail of the list
tail = node; // update the reference to the tail node
size++;
}
public E dequeue() {
if (size == 0)
System.out.println("Queue is empty.");
E tmp = head.getElement();
head = head.getNext();
size--;
if (size == 0)
tail = null; // the queue is now empty
return tmp;
}
public int size() { return size; }
public boolean isEmpty() {
return size == 0;
}
public E front() {
if (isEmpty()) System.out.println("Queue is empty.");
return head.getElement();
}
public String toString() {
Node<E> temp = head;
String s;
s = "[";
for (int i = 1; i <= size(); i++){
if(i==1)
s += temp.getElement();
else
s += ", " + temp.getElement();
temp = temp.getNext();
}
return s + "]";
}
}
队列接口:
public interface Queue<E> {
/**
* Returns the number of elements in the queue.
* @return number of elements in the queue.
*/
public int size();
/**
* Returns whether the queue is empty.
* @return true if the queue is empty, false otherwise.
*/
public boolean isEmpty();
/**
* Inspects the element at the front of the queue.
* @return element at the front of the queue.
* @exception EmptyQueueException if the queue is empty.
*/
public E front();
/**
* Inserts an element at the rear of the queue.
* @param element new element to be inserted.
*/
public void enqueue (E element);
/**
* Removes the element at the front of the queue.
* @return element removed.
* @exception EmptyQueueException if the queue is empty.
*/
public E dequeue();
}