我的打印语句在 while 循环后面为什么没有输出?

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

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&lt;String&gt; lineOne = new ArrayList&lt;String&gt;();
	
	    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(&quot;exit&quot;)) {
        break;
}

Please see this solution

huangapple
  • 本文由 发表于 2020年5月4日 14:05:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/61586060.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定