美文网首页
JavaSE work004

JavaSE work004

作者: 23b57d72cde7 | 来源:发表于2018-04-12 20:37 被阅读0次
import java.util.ArrayList;
import java.util.Random;

public class work004 {
//  第一题:要求产生10个随机的字符串,每一个字符串互相不重复,每一个字符串中组成的字符(a-zA-Z0-9)也不
//  相同,每个字符串长度为10;
//
//  分析:*1.看到这个题目,或许你脑海中会想到很多方法,比如判断生成的字符串是否包含重复,在判断长度是不
//  是10,等等.
//
//   *2.其实这题我们可以培养一个习惯,大问题分解小问题解决.
//
//              (1).10个字符串,我们先产生一个10个字符不重复的字符串,
//
//     (2).怎么去重复呢?集合中的HashSet就可以,这题不适合用包含方法做,代码复杂
//
//     (3).字符组成是由(a-zA-Z0-9)  难道我们在随机他们的码表一一判断吗?-------->可以把们放到一个
//        容器中ArrayList 在集合的随机索引
//      public static String ziFuYuan(int len) {
//          //字符源,可以根据需要删减
//          String ziFuYuan = "0123456789abcdefghigklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
//          String r = "";
//          for (int i = 0; i < len; i++) {
//              //循环随机获得当次字符,并移走选出的字符
//              String s = String.valueOf(ziFuYuan.charAt((int) Math.floor(Math.random() * ziFuYuan.length())));
//              r += s;
//              ziFuYuan = ziFuYuan.replaceAll(s, "");
//          }
//          return r;
//      }
//       
//      public static void main(String[] args) {  
//              System.out.println(ziFuYuan(10));
//  }
//  2.第二题 已知有十六支男子足球队参加2008 北京奥运会。写一个程序,把这16 支球队随机分为4 个组。
//  采用List集合和随机数
//
//  2008 北京奥运会男足参赛国家: 
//
//  科特迪瓦,阿根廷,澳大利亚,塞尔维亚,荷兰,尼日利亚、日本,美国,中国,新西 兰,巴西,比利时
//  ,韩国,喀麦隆,洪都拉斯,意大利
    public static void main(String[] args) {
        ArrayList<String> ls = new ArrayList<String>();
        String ls0;
        ls.add(new String("科特迪瓦队"));
        ls.add(new String("阿根廷队"));
        ls.add(new String("澳大利亚队"));
        ls.add(new String("塞尔维亚队"));
        ls.add(new String("荷兰队"));
        ls.add(new String("尼日利亚队"));
        ls.add(new String("日本队"));
        ls.add(new String("美国队"));
        ls.add(new String("中国队"));
        ls.add(new String("新西兰队"));
        ls.add(new String("巴西队"));
        ls.add(new String("比利时队"));
        ls.add(new String("韩国队"));
        ls.add(new String("喀麦隆队"));
        ls.add(new String("洪都拉斯队"));
        ls.add(new String("意大利队"));

        Random ran = new Random();

        for (int i = 1; i <= 4; i++) {
            System.out.println(i + "组:");
            for (int j = 0; j < 4; j++) {
                ls0 = ls.get(ran.nextInt(ls.size()));
                System.out.print("   " + ls0);
                ls.remove(ls0);
            }
            System.out.println("\n");
        }
    }
}

相关文章

网友评论

      本文标题:JavaSE work004

      本文链接:https://www.haomeiwen.com/subject/upwfkftx.html