【发布时间】:2021-12-20 10:37:30
【问题描述】:
此程序将字符串列表逐行拆分。我想在输入“暂停”后停止/终止程序。但是现在它只在单独输入“halted”时终止,而不是在字符串列表中时终止。非常感谢任何帮助:)
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter words ");
while (true) {
String userText = scanner.nextLine();
if (userText.equals("halted")) {
break;
}
else {
String[] pieces = userText.split(" ");
for (int i = 0; i < pieces.length; i++) {
System.out.println(pieces[i]);
if (pieces[i].equals("halted")) {
System.out.println(" Stop Program...");
break;
}
}
}
}
}
【问题讨论】: