队列加分项

队列加分项

作业要求

实现代码

public class yhsj {
    public static void main(String[] args) {
        Scanner sca = new Scanner(System.in);
        CircularArrayQueue<Integer> y = new CircularArrayQueue<Integer>();
        y.enqueue(0);
        y.enqueue(1);
        System.out.println("输入你想要的行数:");
        int count = sca.nextInt();
        for (int i = 0;i<=count;){
            int a = y.dequeue();
            int b = y.first();
            if(a==0){
                i++;
                y.enqueue(0);
            }
            y.enqueue(a+b);
            if (a==0)
                System.out.println();
            else
                System.out.print(a+"  ");
        }
    }
}

成果截图

单步跟踪

遇到的问题

  1. 当要求的行数过大时会打印出负数
  • 问题详情:测试中我把行数设置的较大,然后就会出现打印出负数的情况
  • 解决过程:在请教同学后,我们初步断定是数据溢出,然后我又查了一下Intege类型的最大值极限Integer.MAX_VALUE = 2147483647,这样便证实了我们的想法。
posted @ 2017-10-22 23:08  20162304张浩林  阅读(102)  评论(0编辑  收藏  举报