【问题标题】:Printing datastructure from input doesn't work从输入打印数据结构不起作用
【发布时间】:2016-01-07 01:32:52
【问题描述】:

我正在尝试创建一个从输入打印数据结构的程序。输入和输出如下所示:http://puu.sh/kDMc9/2d46462d4d.png。例如,在第一个测试用例中:第一行表示在这种情况下将跟随多少行。那么如果它是数字 1 作为一行中的第一个数字,则表示您要向堆栈/队列/优先队列添加元素,而 2 表示您要取出一个元素,因此一行中的第二个数字是值.然后输出是否是堆栈、队列、优先级队列、不可能或不确定(可以多个)

这是我现在的代码:

import java.util.PriorityQueue;
import java.util.Scanner;


public class DataStructure {

public static void main(String[] args)
{

    while(calculate());
}

private static boolean calculate()
{
    Scanner input = new Scanner(System.in);
    int numberOfRowsPerCase = input.nextInt();

    Stack<Integer> stack = new Stack<Integer>();
    Queue<Integer> queue = new Queue<Integer>();
    PriorityQueue<Integer> prioQueue = new PriorityQueue<Integer>();
    boolean stackBool = true;
    boolean queueBool = true;
    boolean prioQueueBool = true;

    int next;

    for(int i = 0; i < numberOfRowsPerCase; i++)
    {
                next = input.nextInt();
                if(next == 1)
                {
                    next = input.nextInt();

                    stack.push(next);
                    queue.enqueue(next);
                    prioQueue.add(next);

                }

                else if(next == 2)
                {
                    next = input.nextInt();
                    if(!stack.pop().equals(next))
                    {
                        stackBool = false;
                    }

                    else if(!queue.dequeue().equals(next))
                    {
                        queueBool = false;
                    }

                    else if(!prioQueue.poll().equals(next))
                    {
                        prioQueueBool = false;
                    }


                }


                if(stackBool == true)
                {
                    System.out.println("stack");
                }

                else if(queueBool == true)
                {
                    System.out.println("queue");
                }

                else if(prioQueueBool == true)
                {
                    System.out.println("priority queue");
                }

                else if((stackBool == true && queueBool == true) || (queueBool == true && prioQueueBool == true) || (stackBool == true && prioQueueBool == true))
                {
                    System.out.println("not sure");
                }

                else
                {
                    System.out.println("impossible");
                }

    }


    //Check EOF 
    String in;
    in = input.nextLine();
    in = input.nextLine();
    if(in.equals(""))
    {
        return false;
    }


    return true;

}

}

但是当我在上图中运行测试用例时,我的程序会打印出这个:https://ideone.com/mIO1bs 这是错误的。我不知道为什么会这样,这里的其他人可能会看到吗?

【问题讨论】:

    标签: java data-structures stack queue


    【解决方案1】:

    假设您设置布尔标志的逻辑是正确的,那么这部分

    if(stackBool == true)
    {
        System.out.println("stack");
    }
    ...
    else if((stackBool == true && queueBool == true) || (queueBool == true && prioQueueBool == true) || (stackBool == true && prioQueueBool == true))
    {
        System.out.println("not sure");
    }
    

    将永远不会按预期工作,因为第二个条件的一部分已经被第一个条件捕获。

    更好的建议是想出一种更清晰的方式来表示这一点。一个可能仍然有效的建议是将更复杂的 if 语句放在 if-else 链的开头。


    忽略上述假设:

    if(!stack.pop().equals(next))
    {
        stackBool = false;
    }
    
    else if(!queue.dequeue().equals(next))
    {
        queueBool = false;
    }
    
    else if(!prioQueue.poll().equals(next))
    {
        prioQueueBool = false;
    }
    

    这些不应该是elses,它们都是完全独立的。

    【讨论】:

    • 好吧,这种带有布尔值的方法是我唯一想到的。有没有办法改变 else if 让它工作?
    • 我将引用回答者的“将您更复杂的 if 语句放在 if-else 链的开头。”
    • 对不起,我错过了,读得太快了。我做了那个改变,它打印了这个:ideone.com/tOSv2p 所以没有任何改变。它似乎总是运行第一个 if 语句,所以我将它们设置为 false 的地方可能是错误的。
    • 好的,还有另一个问题...我将编辑我的答案。编辑:更新。
    • @user2597001 已更新。这是一条通知您的评论。
    猜你喜欢
    • 1970-01-01
    • 2012-08-24
    • 1970-01-01
    • 1970-01-01
    • 2020-11-08
    • 2014-03-24
    • 1970-01-01
    • 1970-01-01
    • 2014-07-10
    相关资源
    最近更新 更多