1
希望標題是解釋性的。Java:將列表<String>轉換爲方法(String,String,String)的輸入
1 public void commander(){
2 Scanner input = new Scanner(System.in);
3 System.out.println("\nWelcome to my application."
4 + "\nType the command you want to use,"
5 + "or type 'exit' to close the program.");
6 while (input.next() != "exit") {
7 List<String> userInput = Arrays.asList(input.next().split(" "));
8 Class<? extends RunnableCommand> command = Commands.commandsMap().get(userInput.get(0));
9 userInput.remove(0);
10 // command.run(userInput); ?!
11 }
第8行: RunnableCommand是提供run()方法,通過每一個表示命令(由政策圖案啓發)類實現的接口。
第10行:這是問題所在。每個「命令類」的run()方法可以有1個,2個或3個字符串作爲輸入。
有沒有辦法給用戶輸入的每個元素作爲run()方法的輸入?
'input.next()= 「退出」' - > [?我如何在Java中比較字符串(http://stackoverflow.com/questions/513832/next-string-in-java) – Pshemo
另外'next'只能返回一個令牌,所以'next()。split(「」)'沒有意義。你可能對'nextLine'感興趣。 – Pshemo
'input.next()!=「exit」'不會按照您(可能)期望的方式工作。試試'!「exit」.equals(input.next())'。 –