加一个控制输入结束的功能,你这个程序一直处于输入状态,无法跳出while循环。例如下面的输入-1表示输入完成。Scanner flat = new Scanner(System.in);
        ArrayList<Integer> b = new ArrayList<Integer>();
        while (flat.hasNext()) {
            int in = flat.nextInt();
            if (in == -1) {
                break;
            } else {
                b.add(in);
            }
        }
        for (int i = 0; i < b.size(); i++) {
            System.out.println(b.get(i));
        }

解决方案 »

  1.   

    while死循环,所以不肯能执行打印输出语句,加一行跳出循环语句;
      

  2.   

    不好意思,今天才看到大家的回答。2楼的朋友给出的跳出循环语句是对的。可是我还是有个疑问:Scanner的hasNext方法不是可以判断是否有数据么?为什么是死循环啊。比如我输入:6 7 8,然后8后面没有数据了,返回false。为什么不可以跳出循环啊.......还有我想问下,这个帖子难道不可以指定对某人回复嘛...还要自己建一楼回复啊
      

  3.   

    flat.hasNext()刚开始是指向第一条数据之前的,所以如果没有有数据你需要执行一次
    flat.next()-->这时候就是取得当前的元素 然后移向下一个元素.
      

  4.   

    public boolean hasNext()
    Returns true if this scanner has another token in its input. This method may block while waiting for input to scan. The scanner does not advance past any input.
    它会阻塞等着你输入下一个int
      

  5.   

    楼主的问题我感觉看一下api你就懂了,关于你说的数组我没有看见啊只看到了list,list的arraylist底层就是数组,它和数组的区别在于数组定长,arraylist的封装的数组的长度会按照某种算法不断加长,随着你的数据也越来越多,……应该已经满足你的需求了,,如果对那个算法感兴趣,自己看看源码
      

  6.   

    public boolean hasNext()
    Returns true if this scanner has another token in its input. This method may block while waiting for input to scan. The scanner does not advance past any input.
    它会阻塞等着你输入下一个int
    import java.util.*;
    public class Polynomial {
       public static void main (String[]args){
       Scanner flat = new Scanner(System.in);  
      while (flat.hasNext()){
      System.out.println(flat.nextInt());
      }
       }}
    输出结果
    这个到底分什么情况呢?
      

  7.   

    API哪里有下么?能告诉我去哪有看的么?
      

  8.   

    虽然分数已经分完了,还是来答一下吧。java API,百度就能下到。看Scanner 对象部分。