gpt4 book ai didi

java - Java中使用栈和队列应用问题的回文测试器

转载 作者:行者123 更新时间:2023-12-01 18:08:50 25 4
gpt4 key购买 nike

我创建了一个应用程序来测试给定的输入字符串是否是回文。但这个程序并没有检测到真正的回文,并且每次都只返回 false。我实在不明白为什么这个方法不起作用。有人可以给我一个建议吗?谢谢。

public boolean palindromeTest()
{
// checks if the top of stack and the head of queue is the same for all elements
// if not, breaks the while loop and returns false
while (!queue.isEmpty())
{
if (stack.peek() != queue.first())
{
return false;
}
else
{
queue.dequeue();
stack.pop();
}
}

return true;
}

}

最佳答案

这看起来不对

if (stack.peek() != queue.first())

您正在尝试比较两个字符串。但是,您的代码仅检查字符串是否是同一对象。

if (!stack.peek().equals(queue.first()))

关于java - Java中使用栈和队列应用问题的回文测试器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60504795/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com