如何在获取输入时停止while循环,而无需输入单词:“exit”等。

huangapple go评论103阅读模式
英文:

how to stop while loop when taking input without words: "exit" etc

问题

我必须一直获取输入直到EOF(空格或行末)。例如:

  1. 1
  2. 2
  3. 3
  4. 4
  5. // 结束输入

我正在使用 Scanner 来获取输入。我尝试了这个解决方案。

  1. public static void main(String[] args) throws IOException {
  2. Scanner sc = new Scanner(System.in);
  3. ArrayList<String> password = new ArrayList<>();
  4. String input;
  5. while (!(input = sc.nextLine()).equals("")) {
  6. password.add(input);
  7. }
  8. boolean[] lengthValidation = lengthCheck(password);
  9. boolean[] passwordContainCheck = containCheck(password);
  10. print(lengthValidation, passwordContainCheck);
  11. }

我不能使用这个,因为uri不接受它的变体回答。

  1. while(true) {
  2. String input = sc.nextLine();
  3. if (input.equals(""))
  4. break;
  5. }
英文:

I have to take inputs until EOF (white space or end of line). For example:

  1. 1
  2. 2
  3. 3
  4. 4
  5. // end of taking input

I'm using Scanner to take inputs. I tried this solution.

  1. public static void main(String[] args) throws IOException {
  2. Scanner sc = new Scanner(System.in);
  3. ArrayList&lt;String&gt; password = new ArrayList&lt;&gt;();
  4. String input;
  5. while (!(input = sc.nextLine()).equals(&quot;&quot;)) {
  6. password.add(input);
  7. }
  8. boolean[] lengthValidation = lengthCheck(password);
  9. boolean[] passwordContainCheck = containCheck(password);
  10. print(lengthValidation, passwordContainCheck);
  11. }

I can't use this, due to uri does not take it variant of answer

  1. while(true) {
  2. String input = sc.nextLine();
  3. if (input.equals(&quot;&quot;))
  4. break;
  5. }

答案1

得分: 1

我必须持续获取输入,直到遇到 EOF(空白或行末)。

尝试以下方法。每行输入一次。以空行结束。您可以将它们存储在一个列表中,在循环结束时使用。

  1. Scanner input = new Scanner(System.in);
  2. String v;
  3. List<String> items = new ArrayList<>();
  4. while (!(v = input.nextLine()).isBlank()) {
  5. items.add(v);
  6. }
  7. System.out.println(items);
英文:

>I have to take inputs until EOF (white space or end of line).

Try the following. One entry per line. Terminate with an empty line. You can store them in a list and use them when the loop terminates.

  1. Scanner input = new Scanner(System.in);
  2. String v;
  3. List&lt;String&gt; items = new ArrayList&lt;&gt;();
  4. while (!(v = input.nextLine()).isBlank()) {
  5. items.add(v);
  6. }
  7. System.out.println(items);
  8. </details>
  9. # 答案2
  10. **得分**: 0
  11. 也许这就是你正在寻找的内容,Scanner会接收输入,直到遇到空行,然后循环停止。
  12. ```java
  13. Scanner sc = new Scanner(System.in);
  14. ArrayList<String> password = new ArrayList<>();
  15. String input;
  16. while (true) {
  17. input = sc.nextLine();
  18. if (input.equals("")) break;
  19. password.add(input);
  20. }
英文:

Maybe this is what you are looking for, Scanner take input until it meets an empty line, then the loop stop

  1. Scanner sc = new Scanner(System.in);
  2. ArrayList&lt;String&gt; password = new ArrayList&lt;&gt;();
  3. String input;
  4. while (true) {
  5. input = sc.nextLine();
  6. if (input.equals(&quot;&quot;)) break;
  7. password.add(input);
  8. }

huangapple
  • 本文由 发表于 2020年9月23日 23:51:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/64031767.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定