第12节:java实战练习题【多测师_王sir】

题目:
编写一个应用程序随机生成20个500以内的整数,统计其中有多少个数小于
200,并按升序在控制台打印出来这些小于200的数。

package com.xuexi;
import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
import java.util.Arrays;
public class Topic1 {
    /**
     * 1,编写一个应用程序随机生成20个500以内的整数,
     * 统计其中有多少个数小于200,
     * 并按升序在控制台打印出来这些小于200的数。
     */

    public static void main(String[] args) {
        //1、随机生成20个500以内的整数
        int a;
        int aa = 0;
        StringBuffer stringBuffer = new StringBuffer();
        int[] array = new int[20];
        for (int i = 0; i < array.length; i++) {
            array[i] = (int) (Math.random() * 499) + 1;
            a = array[i];
            //2、统计其中有多少个数小于200
            if (a < 200) {
                aa = aa + 1;
                stringBuffer.append(a + ",");
            }
        }
        System.out.println("小于200的数个数:" + aa);
//        System.out.println(stringBuffer.toString());
        //3、并按升序在控制台打印出来这些小于200的数。
        //将stringBuffer.toString()进行拆分,转换为String数组
        String[] strings = stringBuffer.toString().split(",");
        //创建一个int数组对象
        int [] arrays = new int[aa];

        for (int i = 0; i <strings.length ; i++) {
            //将String数组转换成int形
            Integer integer = new Integer(strings[i]);
            arrays[i] = integer.intValue();
        }
//        System.out.println(Arrays.toString(arrays));
        //将int数组进行排序
        Arrays.sort(arrays);
        System.out.println("这几个数字是:"+Arrays.toString(arrays));

    }

}
题目:
对26个小写字母随机组合5个长度字符串,随机组合26次,统计组成的字符串中有多少个字符串包含重复字母

package com.xuexi;
import jdk.management.resource.internal.inst.FileOutputStreamRMHooks;
import org.omg.CORBA.INTERNAL;
import java.util.Arrays;
public class Topic2 {
    public static void main(String[] args) {
        /**
         * 对26个小写字母随机组合5个长度字符串,
         * 随机组合26次,
         * 统计组成的字符串中有多少个字符串包含重复字母
         */
        int aa=0;
        int index;
        //1、建立一个数组,由26个小写字母组成
        char []cha={'a','b','c','d','e','f','g','h','i','j',
                'k','l','m','n','o','p','q','r','s','t',
                'u','v','w','x','y','z'};
        char []ch=new char[5];
        //3、随机组合26次
        for (int j = 0; j <26 ; j++) {
            //2、对26个小写字母进行随机抽取五个字符进行组合,
            for(int i=0;i<ch.length;i++) {
                index=(int)(Math.random()*(cha.length));
                ch[i]=cha[index];
            }
            System.out.println(Arrays.toString(ch));
            //4、对比组合的字符串里面的字符是否有重复
            if(ch[0]==ch[1] || ch[0]==ch[2] || ch[0]==ch[3] || ch[0]==ch[4] ||
                    ch[2]==ch[1] || ch[3]==ch[1] || ch[4]==ch[1] || ch[2]==ch[3]
                    || ch[2]==ch[4] || ch[3]==ch[4] ){
                //5、统计重复组成的数字符号
                aa=aa+1;
            }
        }
        System.out.println("一共有:"+aa+"个数组包含重复字母");
    }
}
原文地址:https://www.cnblogs.com/xiaoshubass/p/13602057.html