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

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

掃描儀的操作歷史記錄

掃描儀的操作歷史記錄

慕雪6442864 2023-10-12 17:23:37
大家好,我正在做作業,我想了解如何正確完成代碼中的第三部分 - 3.查看操作歷史記錄。(用戶執行的所有操作的列表,使用數組以格式存儲此信息:“[操作索引]。[操作名稱] - [用戶輸入]”。我還有一個問題 - 應用程序應該工作并接受用戶輸入,直到他想要退出程序。  Scanner in = new Scanner(System.in);System.out.println("Please select operation");        System.out.println("1.Encode");        System.out.println("2.Decode");        System.out.println("3.View operation history");        System.out.println("4.Exit program");        Integer userInput = Integer.valueOf(in.nextLine());        if (userInput.equals(1)) {            System.out.println("You chosen encoding operation");            System.out.println("Please choose codec name  - caesar or morse");            String userInputEncode = in.nextLine().toLowerCase();            if (userInputEncode.equals("caesar")) {                System.out.println("Enter text to encode");                String userInputEncode2 = in.nextLine().toLowerCase();                System.out.println("Encoded text: " + caesarEncoder.encode(userInputEncode2));            } else if (userInputEncode.equals("morse")) {                System.out.println("Enter text to encode");                String userInputEncode3 = in.nextLine().toLowerCase();                System.out.println("Encoded text: " + morseEncoder.encode(userInputEncode3));            } else                System.out.println("You entered wrong codec name, try one more time");        } else if (userInput.equals(2)) {            System.out.println("You chosen decoding operation");            System.out.println("Please choose codec name  - caesar or morse");            String userInputDecode = in.nextLine().toLowerCase();            if (userInputDecode.equals("caesar")) {                System.out.println("Enter text to decode");                String userInputDecode2 = in.nextLine().toLowerCase();                System.out.println("Decoded text: " + caesarDecoder.decode(userInputDecode2));            } 
查看完整描述

1 回答

?
千萬里不及你

TA貢獻1784條經驗 獲得超9個贊

您可能需要一個循環來檢查用戶是否鍵入了數字 4。


然后,您可以在循環之前創建一個列表,其中將存儲用戶在程序執行期間鍵入的所有歷史記錄。


然后在選擇數字3的情況下,您可以使用該列表來獲取歷史數據,然后打印您需要的所有數據


下面是一個簡單而快速的示例(不是最好的方法):


import java.util.List;

import java.util.ArrayList;

import java.util.Scanner;


Scanner in = new Scanner(System.in);

List<String> operationHistory=new ArrayList<String>();

Integer userInput = null;

do{

    System.out.println("Please select operation");

    System.out.println("1. aaa");

    System.out.println("2. bbb");

    System.out.println("3. print selection history");

    System.out.println("4. exit");

    userInput = Integer.valueOf(in.nextLine());


    if (userInput.equals(1)) {

        System.out.println("Please type something");

        String userInputEncode = in.nextLine().toLowerCase();

        operationHistory.add("1. aaa - " + userInputEncode);

    }else if (userInput.equals(2)) {.

        System.out.println("Please type something");

        String userInputEncode = in.nextLine().toLowerCase();

        operationHistory.add("2. bbb - " + userInputEncode);

    }else if (userInput.equals(3)) {

        System.out.println("Operation History:");

        for(String operationitem:operationHistory){

            System.out.println(operationitem);

        }

        System.out.println("-------------");

    }


}while(!userInput.equals(4));

in.close();

System.out.println("Program was closed by user");

這只是一個與您的代碼類似的示例,您可以使用它來了解如何解決您的問題以及循環和列表如何工作


然后您可能想要管理一些意外的輸入


查看完整回答
反對 回復 2023-10-12
  • 1 回答
  • 0 關注
  • 132 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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