简体   繁体   English

我使用scan read的java程序不会停止执行

[英]my java program using scan read does not stop execution

I am a newbee in the Java world. 我是Java世界的新手。 I wrote this program which reads in string array... When I run it, it never stops?!! 我写了这个程序,它读取字符串数组......当我运行它时,它永远不会停止?!! what shall I add /change to make it end scanning? 我应该添加/更改什么才能使其结束扫描?

import java.util.*;

public class Ex21 {
public static void main(String[] args) {
    int i, n = 5;
    String c;
    ArrayList<String>words = new ArrayList<>();
    System.out.println("Enter multi strings: ");
    Scanner input = new Scanner(System.in);
    boolean loop = true;
    while(loop) {
        words.add(input.next());
        Collections.sort(words);
        System.out.println("The sorted list is: " + words);
    }
    }

}

A while loop by definition continues executing its body until its condition (in this case the variable loop ) evaluates to false . 根据定义, while循环继续执行其主体,直到其条件(在本例中为变量loop )计算为false You never set loop to false in the body of the while -loop, hence the condition will never evaluate to false and the loop will never end. 你永远不会在while -loop的主体中将loop设置为false ,因此条件永远不会计算为false ,循环将永远不会结束。

Additionally, it seems like you want to sort a list of words entered by the user. 此外,您似乎想要对用户输入的单词列表进行排序。 I wouldn't advise calling Collections.sort on every iteration of the loop. 我不建议在循环的每次迭代中调用Collections.sort Maybe look into using a data structure that keeps its elements sorted on its own, such as a TreeSet . 也许考虑使用一种数据结构来保持其元素自己排序,例如TreeSet Or, at least, only call the sort method once, directly after the loop. 或者,至少在循环之后直接调用sort方法一次。

while(condition) {
     /* do something */
}

means /* do something */ happens unless condition == false, in your case it alwats true, that is why it doesn't stop. 表示/ *做某事* /发生,除非条件==假,在你的情况下它是真的,这就是为什么它不会停止。 So Java behaves ok in your case. 所以Java在你的情况下表现良好。

while(loop) with loop always having the value true in your program is a so called endless loop , as the name says it never ends and this is what you are experiencing. while(loop) with loop在程序中始终具有值true是一个所谓的endless loop ,因为名称它永远不会结束,这就是你正在经历的。

To make a loop stop you have to set the value of loop to false if some condition is fulfilled or terminate the loop using the key word break . 要进行循环停止,必须在满足某些条件时将loop的值设置为false ,或者使用关键字break来终止循环。

A condition may be for example having a certain word witch lets your loop terminate when it is entered, something like "exit" 一个条件可能是例如有一个单词,让你的循环在输入时终止,比如"exit"

Here is an example how you could set loop to false 这是一个如何将loop设置为false的示例

String word = input.next();
boolean loop = ! "exit".equalsIgnoreCase(word);
while (loop) {
    words.add(word);
    Collections.sort(words);
    System.out.println("The sorted list is: " + words);
    word = input.next();
    loop = ! "exit".equalsIgnoreCase(word);
}
System.out.println("Bye!");

Here is an other example how you could cancel the loop using break 这是另一个如何使用break取消循环的示例

while (true) {
    String word = input.next();
    if("exit".equalsIgnoreCase(word)) {
        break;
    }
    words.add(word);
    Collections.sort(words);
    System.out.println("The sorted list is: " + words);
}
System.out.println("Bye!");

Note that the word exit is banned from the set that your arraylist can contain, You can change the program though to have it saved too. 请注意,单词exit将从您的arraylist可以包含的集合中被禁止,您可以更改程序,但也可以保存它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM