如何在 Java 中从堆栈接收上一行文本

我目前正在为代码而苦苦挣扎。问题主要发生在阵列和尝试回访时。

此文件只需要一个 main 方法,该方法执行以下操作,直到用户输入“quit”:

• 提示用户输入要访问、返回(仅在可能的情况下)或退出的 URL

•访问并显示输入的URL

•返回并显示以前访问过的URL(如果可能)

• 如果用户在没有要返回的页面时输入“后退”,则应显示相应的消息。

下面是一个输出示例:

输入网址或“退出”:返回

没有要返回的网址

输入网址或“退出”:http://www.wwe.com

当前网址:http://www.wwe.com

输入网址或“退出”:返回

没有要返回的网址

当前网址:http://www.wwe.com

输入网址或“退出”:http://www.amazon.com

当前网址:http://www.amazon.com

输入网址“后退”或“退出”:http://www.google.com

当前网址:http://www.google.com

输入网址“后退”或“退出”:后退

当前网址:http://www.amazon.com

输入网址“后退”或“退出”:后退

当前网址:http://www.wwe.com

输入网址或“退出”:退出


拉丁的传说
浏览 193回答 3
3回答

繁华开满天机

使用 Stack 类而不是 ArrayList 会让你的生活更轻松。使用 push() 将新的 url 添加到堆栈。使用 empty() 检查是否可以返回。使用 pop() 返回。编辑 - 支持转发如果您还想支持“转发”comand,则可以使用第二个堆栈,并将从该转发堆栈上的历史堆栈中弹出的URL推送。输入“forward”命令时,检查正向堆栈是否为空,如果没有,请从那里弹出 url 并将其推送回历史记录堆栈。编辑 2 - 示例代码以下是一些基本代码来解释 2 堆栈解决方案:Stack<String> historyStack = new Stack<>();Stack<String> forwardStack = new Stack<>();String currentUrl = null;boolean running = true;while(running) {&nbsp; &nbsp; String input = getUserInput();&nbsp; &nbsp; switch(input) {&nbsp; &nbsp; &nbsp; &nbsp; case "quit":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; running = false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; case "back":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (!historyStack.empty()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (currentUrl != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; forwardUrl.push(currentUrl);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; currentUrl = historyStack.pop();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(currentUrl);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("nothing to go back to");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; case "forward":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (!forwardStack.empty()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (currentUrl != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; historyStack.push(currentUrl);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; currentUrl = forwardStack.pop();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(url);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("nothing to go forward to");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; default:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (currentUrl != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; historyStack.push(currentUrl);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; currentUrl = input;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(url);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // entering a new url makes forward stack invalid&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; forwardStack.clear();&nbsp; &nbsp; }}

潇潇雨雨

您可以将逻辑更改为如下所示:ArrayList<String> webs = new ArrayList<String>();String web = "";Scanner url = new Scanner(System.in);int count = 0;while (!web.contains("quit")) {&nbsp; &nbsp; System.out.println("Enter a URL or \"quit\":");&nbsp; &nbsp; web = url.next();&nbsp; &nbsp; if (!web.equals("back")) {&nbsp; &nbsp; &nbsp; &nbsp; webs.add(web);&nbsp; &nbsp; &nbsp; &nbsp; count = webs.size();&nbsp; &nbsp; } else if (web.equals("back") && !webs.isEmpty()) {&nbsp; &nbsp; &nbsp; &nbsp; if (count > 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; count--;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(webs.get(count));&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("No url to go back to");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}请注意以下几点:我们只添加不等于&nbsp;back&nbsp;的字符串在之前的实现中,输入的第一个&nbsp;URL&nbsp;未插入到列表中。将元素添加到列表后,计数将重置为列表的大小。正如其他人所指出的那样,通过使用堆栈可以更容易地实现相同的目标。Scanner url = new Scanner(System.in);String web = "";Stack<String> myStack = new Stack<>();while (!web.contains("quit")) {&nbsp; &nbsp; System.out.println("Enter a URL or \"quit\":");&nbsp; &nbsp; web = url.next();&nbsp; &nbsp; if (!web.equals("back") && !web.equals("quit")) {&nbsp; &nbsp; &nbsp; &nbsp; myStack.push(web);&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; if (!myStack.isEmpty()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(myStack.pop());&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("No url to go back to");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}

幕布斯6054654

您使用了不正确的数据结构。 是可以的,但在这里使用更正确:你添加到末尾并从末尾检索,这个id LIFO。ListStackprivate static final String QUIT = "quit";private static final String BACK = "back";try (Scanner url = new Scanner(System.in)) {&nbsp; &nbsp; Deque<String> stack = new LinkedList<>();&nbsp; &nbsp; while (true) {&nbsp; &nbsp; &nbsp; &nbsp; System.out.print("Enter a URL, \"" + BACK + "\" or \"" + QUIT + "\": ");&nbsp; &nbsp; &nbsp; &nbsp; String str = url.next();&nbsp; &nbsp; &nbsp; &nbsp; if (str.equalsIgnoreCase(QUIT))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; else if (str.equalsIgnoreCase(BACK)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (!stack.isEmpty())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; stack.pop();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(stack.isEmpty() ? "No URL to go back to" : stack.element());&nbsp; &nbsp; &nbsp; &nbsp; } else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; stack.push(str);&nbsp; &nbsp; }}演示Enter a URL, "back" or "QUIT": http://www.wwe.comEnter a URL, "back" or "QUIT": http://www.amazon.comEnter a URL, "back" or "QUIT": http://www.google.comEnter a URL, "back" or "QUIT": backhttp://www.amazon.comEnter a URL, "back" or "QUIT": backhttp://www.wwe.comEnter a URL, "back" or "QUIT": backNo URL to go back toEnter a URL, "back" or "QUIT": quit
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java