猿问

如何从Java中的堆栈接收前一行文本

我目前正在努力编写代码。问题主要发生在阵列并试图返回。

该文件只需要一个 main 方法,该方法在用户输入“退出”之前执行以下操作:

• 提示用户访问、返回(仅在可能时)或退出的 URL

• 访问并显示输入的 URL

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

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

这是一个输出示例:

输入 URL 或“退出”:返回

没有可返回的网址

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

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

输入 URL 或“退出”:返回

没有可返回的网址

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

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

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

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

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

输入 URL、“返回”或“退出”:返回

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

输入 URL、“返回”或“退出”:返回

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

输入 URL 或“退出”:quit

这是我当前的代码:

public class BrowsingHistory

{

 public static void main(String [] args)

 {


    Scanner url = new Scanner(System.in);

    String web = "";

    String currentURL = "";

    Stack<String> myStack = new Stack<>();

    System.out.print("Enter a URL or \"quit\": ");

    web = url.nextLine();

    while (!web.contains("quit"))

    {

        System.out.println();

        System.out.print("Enter a URL, \"back\", or \"quit\": ");

        web = url.nextLine();

        if(web.equals("back") && myStack.isEmpty())

        {

            System.out.println("No URL to go back to");


        }

            else if(!web.equals("back"))

            {

                myStack.push(web);

                System.out.println("Current URL: " + myStack.peek());

            }

            else

                {

                    System.out.println("No URL to go back to");

                    System.out.println("Current URL: " + myStack.pop());

        }

        }

}

}

以下是它需要通过的测试,以便澄清:


 @Test

 void testMain()

 {

     setInput("back\nhttp://www.uwec.edu\nback\nhttp://www.amazon.com\nhttp://.    w.google.com\nback\nback\nquit\n");


BrowsingHistory.main(null);


 String mainOutput = outContent.toString();


  Scanner driverOut = new Scanner(mainOutput);


     String outputLine = getNextOutputLine(driverOut);


    assertEquals("Enter a URL or \"quit\":", outputLine.substring(0, outputLine.indexOf(":")+1).trim(), "BrowsingHistory doesn't run as expected (initial prompt problem)");


慕田峪4524236
浏览 110回答 3
3回答

幕布斯7119047

使用 Stack 类而不是 ArrayList 会让你的生活更轻松。使用 push() 将新的 url 添加到堆栈中。使用 empty() 检查是否可以返回。使用 pop() 返回。编辑 - 支持前锋如果您还想支持“转发”命令,您可以使用第二个堆栈并将您从历史堆栈中弹出的 url 推送到该转发堆栈上。当输入 'forward' 命令时,检查 forward-stack 是否为空,如果不是,则从那里弹出 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; }}请注意以下几点:我们只添加不等于back的字符串在您之前的实现中,输入的第一个url没有插入到您的列表中。将元素添加到列表后,计数将重置为列表的大小。正如其他人指出的那样,使用堆栈可以更轻松地实现相同的目标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; }}

holdtom

您使用了不正确的数据结构。List可以,但Stack在这里使用更正确:您添加到末尾并从末尾检索,此 id LIFO。private 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

相关分类

Java
我要回答