亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何從Java中的堆棧接收前一行文本

如何從Java中的堆棧接收前一行文本

慕田峪4524236 2022-07-27 21:59:42
我目前正在努力編寫代碼。問題主要發生在陣列并試圖返回。該文件只需要一個 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)");
查看完整描述

3 回答

?
幕布斯7119047

TA貢獻1794條經驗 獲得超8個贊

使用 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) {

    String input = getUserInput();

    switch(input) {

        case "quit":

            running = false;

            break;

        case "back":               

            if (!historyStack.empty()) {

                if (currentUrl != null) {

                    forwardUrl.push(currentUrl);

                }

                currentUrl = historyStack.pop();

                System.out.println(currentUrl);

            } else {

                System.out.println("nothing to go back to");

            }

            break;

        case "forward":

            if (!forwardStack.empty()) {

                if (currentUrl != null) {

                    historyStack.push(currentUrl);

                }

                currentUrl = forwardStack.pop();

                System.out.println(url);

            } else {

                System.out.println("nothing to go forward to");

            }

            break;

        default:

            if (currentUrl != null) {

                historyStack.push(currentUrl);

            }

            currentUrl = input;

            System.out.println(url);

            // entering a new url makes forward stack invalid

            forwardStack.clear();

    }

}


查看完整回答
反對 回復 2022-07-27
?
手掌心

TA貢獻1942條經驗 獲得超3個贊

您可以將邏輯更改為如下:


ArrayList<String> webs = new ArrayList<String>();

String web = "";

Scanner url = new Scanner(System.in);


int count = 0;

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

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

    web = url.next();

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

        webs.add(web);

        count = webs.size();

    } else if (web.equals("back") && !webs.isEmpty()) {

        if (count > 0) {

            count--;

            System.out.println(webs.get(count));

        } else {

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

        }

    }

}

請注意以下幾點:

  1. 我們只添加不等于back的字符串

  2. 在您之前的實現中,輸入的第一個url沒有插入到您的列表中。

  3. 將元素添加到列表后,計數將重置為列表的大小。


正如其他人指出的那樣,使用堆棧可以更輕松地實現相同的目標

Scanner url = new Scanner(System.in);

String web = "";

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

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

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

    web = url.next();

    if (!web.equals("back") && !web.equals("quit")) {

        myStack.push(web);

    } else {

        if (!myStack.isEmpty()) {

            System.out.println(myStack.pop());

        } else {

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

        }

    }

}


查看完整回答
反對 回復 2022-07-27
?
holdtom

TA貢獻1805條經驗 獲得超10個贊

您使用了不正確的數據結構。List可以,但Stack在這里使用更正確:您添加到末尾并從末尾檢索,此 id LIFO。


private static final String QUIT = "quit";

private static final String BACK = "back";


try (Scanner url = new Scanner(System.in)) {

    Deque<String> stack = new LinkedList<>();


    while (true) {

        System.out.print("Enter a URL, \"" + BACK + "\" or \"" + QUIT + "\": ");

        String str = url.next();


        if (str.equalsIgnoreCase(QUIT))

            break;

        else if (str.equalsIgnoreCase(BACK)) {

            if (!stack.isEmpty())

                stack.pop();

            System.out.println(stack.isEmpty() ? "No URL to go back to" : stack.element());

        } else

            stack.push(str);

    }

}

演示


Enter a URL, "back" or "QUIT": http://www.wwe.com

Enter a URL, "back" or "QUIT": http://www.amazon.com

Enter a URL, "back" or "QUIT": http://www.google.com

Enter a URL, "back" or "QUIT": back

http://www.amazon.com

Enter a URL, "back" or "QUIT": back

http://www.wwe.com

Enter a URL, "back" or "QUIT": back

No URL to go back to

Enter a URL, "back" or "QUIT": quit


查看完整回答
反對 回復 2022-07-27
  • 3 回答
  • 0 關注
  • 124 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號