2014-11-01 54 views
-2

热土豆比赛:在这个程序中,你需要实现一个 热土豆比赛的一般模拟。在这个游戏中,儿童线 在一个圆圈内,并通过一个从邻居到邻居的物品的速度可以达到 。在游戏中的某个时刻,动作停止,并且将具有物品(马铃薯)的孩子从圈子中移除。 继续播放,直到只剩下一个孩子。在你的实现中 用户应该输入一个名字列表和一个常数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(); 
} 

回答

0

电话:

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

你正在做的读取数据一次,然后进入一个无限while循环,因为你没有办法写“停止”。因此,该功能将是:

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

的问题是很容易的,只是看你的代码更好:

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

你的,而之前获得新的输入。这意味着键盘将是第一个参数,爱丽丝说。

所以,虽然爱丽丝!=停止,做马铃薯.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! 
     } 
}