A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© autumnforest 中级黑马   /  2015-11-9 22:16  /  916 人查看  /  6 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文


import java.util.ArrayList;
import java.util.Scanner;

public class Scanner_ArrayList {

        /*
         * 程序启动后, 可以从键盘输入接收多个整数, 直到输入quit时结束输入. 把所有输入的整数倒序排列打印。
         */
        public static void main(String[] args) {
                print(getArr());
        }

        // 键盘录入整数的集合,碰到quit结束
        public static ArrayList<Integer> getArr() {
                Scanner sc = new Scanner(System.in);
                System.out.println("请输入整数(录入以quit完结)");
                ArrayList<Integer> list = new ArrayList<>();
                while (true) {
                        String s = sc.nextLine();
                        if ("quit".equals(s)) {
                                break;
                        } else if (!s.matches("[0-9]+")) {
                                System.out.println("输入有误,请重新输入!");
                                continue;
                        } else {
                                list.add(Integer.parseInt(s));
                        }
                }
                System.out.println(list);
                return list;
        }

        // 集合倒序打印
        public static void print(ArrayList<Integer> list) {
                int index = list.size() - 1;
                for (Integer i : list) {
                        if (index == 0) {
                                System.out.print(list.get(index));
                                break;
                        }
                        System.out.print(list.get(index--) + ", ");
                        
                }
        }
}

6 个回复

倒序浏览
zhichiyixia 支持一下
回复 使用道具 举报
如果注释再详细些就更好了!!
回复 使用道具 举报
6666666  我跟你写的差不多   哈哈
回复 使用道具 举报
支持下,写的不错!
回复 使用道具 举报
李志慧 发表于 2015-11-9 22:34
如果注释再详细些就更好了!!

没注释,自己看
回复 使用道具 举报
学习他人经验,助自我成长!
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马