我正在嘗試允許僅使用字符 a-zA-Z 并且還具有空格的輸入。示例:“查克諾里斯”。 下面的代碼發生的情況是掃描器input.next()不允許上述示例輸入字符串。我期待這個輸出:Success!但這是實際輸出:Again: try { String input_nome = input.next(); // Source of the problem if (input_nome.matches("[a-zA-Z ]+")) { System.out.print("Success!"); break; } else { System.err.print("Again: "); }} catch (Exception e) { e.printStackTrace();}解決方案問題的根源是使用的掃描儀方法。String input_nome = input.next(); // Incorrect scanner methodString input_nome = input.nextLine(); // Correct scanner method說明: https://stackoverflow.com/a/22458766/11860800
添加回答
舉報
0/150
提交
取消