救命啊!while(true)循环跳不出去了!!

public class Solution {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
ArrayList arr = new ArrayList<>();
String s = sc.nextLine();
while (true){
arr.add(s);
if(s.equals("结束"))
break;

        arr.add(s);

    }

    for(String a : arr) {
        System.out.println(a);
    }
    //在此编写你的代码
}

}
这个循环为什么无法跳出?

String s = sc.nextLine();
你的从键盘输入的语句要放到while循环中才能实现每次都更改这个变量s的值。
造成死循环的原因就是因为s一直是你第一次输入的内容,内容没有被改变,所以除非第一次就输入“结束”,否则无法满足if的条件,从而退出。

public class Solution {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
ArrayList arr = new ArrayList<>();
while (true){
String s = sc.nextLine();
arr.add(s);
if(s.equals("结束"))
break;
}
for(String a : arr) {
      System.out.println(a);
}
}}

String s = sc.nextLine();
这个要放在循环里面,否则如果你输入了结束以外的文字,s又不会改变,当然死循环了

while前加标签,break后面加标签