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

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

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

问题

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

1
2
3
4
// 结束输入

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

    public static void main(String[] args) throws IOException {
        Scanner sc = new Scanner(System.in);
        ArrayList<String> password = new ArrayList<>();
        String input;

        while (!(input = sc.nextLine()).equals("")) {
            password.add(input);
        }

        boolean[] lengthValidation = lengthCheck(password);
        boolean[] passwordContainCheck = containCheck(password);

        print(lengthValidation, passwordContainCheck);
    }

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

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

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

1
2
3
4
// end of taking input

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

    public static void main(String[] args) throws IOException {
        Scanner sc = new Scanner(System.in);
        ArrayList&lt;String&gt; password = new ArrayList&lt;&gt;();
        String input;

        while (!(input = sc.nextLine()).equals(&quot;&quot;)) {
            password.add(input);
        }

        boolean[] lengthValidation = lengthCheck(password);
        boolean[] passwordContainCheck = containCheck(password);

        print(lengthValidation, passwordContainCheck);
    }

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

while(true) {
   String input = sc.nextLine();
   
   if (input.equals(&quot;&quot;))
       break;
}

答案1

得分: 1

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

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

Scanner input = new Scanner(System.in);
String v;
List<String> items = new ArrayList<>();
while (!(v = input.nextLine()).isBlank()) {
    items.add(v);
}
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.

Scanner input = new Scanner(System.in);
String v;
List&lt;String&gt; items = new ArrayList&lt;&gt;();
while (!(v = input.nextLine()).isBlank()) {
	items.add(v);
}
System.out.println(items);
	

</details>



# 答案2
**得分**: 0

也许这就是你正在寻找的内容,Scanner会接收输入,直到遇到空行,然后循环停止。

```java
Scanner sc = new Scanner(System.in);
ArrayList<String> password = new ArrayList<>();
String input;

while (true) {
    input = sc.nextLine();
    if (input.equals("")) break;
    password.add(input);
}
英文:

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

Scanner sc = new Scanner(System.in);
    ArrayList&lt;String&gt; password = new ArrayList&lt;&gt;();
    String input;

    while (true) {
        input = sc.nextLine();
        if (input.equals(&quot;&quot;)) break;
        password.add(input);
    }

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:

确定