用收藏品创建一个热土豆游戏

时间:2014-11-01 22:28:28

标签: java collections

  

Hot Potato Game:在这个程序中,你应该实现一个   热土豆游戏的一般模拟。在这个游戏儿童行   在一个圆圈中,并将项目从邻居传递给邻居,速度最快   他们能。在游戏的某个时刻,动作停止了   将具有该项目(马铃薯)的孩子从圆圈中移除。   继续游戏,直到只剩下一个孩子。在您的实施中   用户应输入名称列表和常量num。你的计划   必须返回重复后剩余的最后一个人的姓名   按数量计算

我需要这样做,但我无法找到如何在hotpotato类的 enqueuer() 方法中停止while循环。如果我有其他一些错误你能告诉我吗?

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(); 
}

2 个答案:

答案 0 :(得分:0)

问题非常简单,只需更好地查看代码:

public static void enqueuer(String p)
{
    String keyboard = input1.next();
    while(!keyboard.equals("stop"))
        {
        potato.enqueue(p);
        }
}

你之前得到了新的输入。这意味着键盘将成为第一个参数,比如爱丽丝。

所以,虽然alice!=停止,请执行potato.enqueue。(p)。

但是由于你内部没有新的输入,键盘将永远!=然后停止!

奖金:

我认为有一个错误:

public static void enqueuer(String p)
{
    String keyboard = input1.next();
    while(!keyboard.equals("stop"))
        {
        potato.enqueue(p); //you dont insert the keyboard value, but you insert every time p!
        }
}

答案 1 :(得分:0)

呼叫:

System.out.println("Enter names of the children.");
enqueuer();

您只执行一次读取数据,然后进入无限WHILE循环,因为您无法写入&#34;停止&#34;。因此,函数将是:

public static void enqueuer()
{
    String p;
    do {
        p = input1.next();
        if (!p.equals("stop"))
           potato.enqueue(p);
    } while(!p.equals("stop"));
}