英文:
Why is my print statement producing no output after a while loop?
问题
什么地方出错了这个代码版本?(我读到了关于“不正确的退出条件”的内容,但我不明白可能出在哪里?)
import java.util.*;
public class First {
public static void main(String[] args) {
ArrayList<String> lineOne = new ArrayList<String>();
Scanner scan = new Scanner(System.in);
while(scan.hasNext()){
String lineOneVar = scan.next();
lineOne.add(lineOneVar);
/*System.out.println(lineOneVar);
System.out.println(lineOne);
System.out.println(lineOne.get(0));*/
}
System.out.println(lineOne);
//没有产生输出。
}
}
英文:
What's wrong with this version of this code? (I read something about an "incorrect exit condition" but I don't see how that could be?)
import java.util.*;
public class First {
public static void main(String[] args) {
ArrayList<String> lineOne = new ArrayList<String>();
Scanner scan = new Scanner(System.in);
while(scan.hasNext()){
String lineOneVar = scan.next();
lineOne.add(lineOneVar);
/*System.out.println(lineOneVar);
System.out.println(lineOne);
System.out.println(lineOne.get(0));*/
}
System.out.println(lineOne);
//Does not produce an output.
}
}
答案1
得分: 1
你应该使用以下概念
if(s1.equals("exit")) {
break;
}
英文:
You should use the below concept
if(s1.equals("exit")) {
break;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论