牛骨文教育服务平台(让学习变的简单)
博文笔记

数组的两种定义方式,求数字的字符数

创建时间:2017-02-18 投稿人: 浏览次数:136
1.第一种,不知道具体的内容
         String[] names = new String[34];
        double[] scores = new double[34];
        char[] sexs = new char[34];
        int[] ages = new int[34];
      一般配合for循环来给数组赋值。
2.第二种,知道具体的内容,直接赋值
        String[] names1 = { "张三", "李四", "王五", "赵四" };
        double[] scores1 = { 85, 98, 76, 90 };
        char[] sexs1 = { "男", "男", "男", "女" };
        int[] ages1 = { 23, 24, 25, 26 };
//数组
        double[] scores = new double[10];
        for(int i = 0;i<scores.length;i++){
            scores[i] = Math.random();
        }


        String[] cities = {"武汉","上海"};


        //输出变量
        for(int i = 0;i<scores.length;i++){
            System.out.println(scores[i]);
        }


        for(int i = 0;i<cities.length;i++){
            System.out.println(cities[i]);
        }


        //输出foreach的方式
        //打印分数
        for(double score : scores){
            System.out.println(score);
        }


        //打印城市
        for(String city :cities){
            System.out.println(city);
        }
声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。