第1题
随机验证码。
随机生成十组六位字符组成的验证码。
验证码由大小写字母、数字字符组成。
代码实现效果如图所示:
代码实现:
public class Test01 { public static void main(String[] args) { char[] arr = new char[26+26+10]; //使用字符数组保存原始字符 for(int i = 0; i < arr.length; i++){ if(i < 10){ //前10个放数字 arr[i] = (char)(i + 48); }else if(i < 10 + 26){ //中间26个放大写字母 arr[i] = (char)(i + 65 -10); }else { //剩下的放小写字母 arr[i] = (char)(i + 97 -10 -26); } } //随机生成10组验证码 ArrayList
list = new ArrayList<>(); Random rand = new Random(); for(int i = 0; i < 10; i++){ String str = ""; for(int j = 0; j < 6; j++){ int index = rand.nextInt(arr.length); str += arr[index]; } list.add(str); } Iterator iterator = list.iterator(); while(iterator.hasNext()){ System.out.println("随机验证码:"+iterator.next()); } } }
第2题
案例:
1、用一个String[]数组存点数
2、用一个String[]数组存花色
3、用一个String[]数组存大王、小王
4、用上面的数组,生成一副扑克牌
5、遍历显示全副扑克牌
6、模拟给4个人随机发牌,每个人11张牌
7、显示每个人的牌和剩余的
效果如图:
代码实现:
public class Test02 { public static void main(String[] args) { String[] dian = {"A","2","3","4","5","6","7","8","9" ,"10","J","Q","K"}; String[] hua = {"黑桃","红桃","方片","梅花"}; String[] wang = {"大王","小王"}; List
list = new ArrayList (); for(int i = 0; i < 4; i++){ for(int j = 0; j < 13; j++){ list.add(hua[i]+dian[j]); } } list.add(wang[0]); list.add(wang[1]); // Iterator iterator = list.iterator(); // while(iterator.hasNext()){ // System.out.print(iterator.next()+"\t"); // } //5、遍历显示全副扑克牌 for(int i = 0 ; i < list.size(); i++){ System.out.print(list.get(i)+" "); if((i+1)%10 == 0){ System.out.println(); } } //6、模拟给4个人随机发牌,每个人11张牌 //7、显示每个人的牌和剩余的牌 System.out.println("\n======================="); System.out.println("发牌:"); Random rand = new Random(); ArrayList one = new ArrayList (); for(int i = 0; i < 11; i++){ one.add(list.remove(rand.nextInt(list.size()))); } ArrayList two = new ArrayList (); for(int i = 0; i < 11; i++){ two.add(list.remove(rand.nextInt(list.size()))); } ArrayList three = new ArrayList (); for(int i = 0; i < 11; i++){ three.add(list.remove(rand.nextInt(list.size()))); } ArrayList four = new ArrayList (); for(int i = 0; i < 11; i++){ four.add(list.remove(rand.nextInt(list.size()))); } System.out.println("第1个人:"+one); System.out.println("第2个人:"+two); System.out.println("第3个人:"+three); System.out.println("第4个人:"+four); System.out.println("剩余:"+list); } }
第3题
案例:
1、随机生成10个[1,100]之间的整数,放到List集合中,遍历显示
2、找出前3名最大值,删除它们,注意可能重复
3、显示删除后的结果
效果如下:
代码实现:
public class Test05 { public static void main(String[] args) { Random rand = new Random(); List
list= new ArrayList<>(); for(;;){ if(list.size() == 10){ break; } int num = rand.nextInt(100)+1; list.add(num); } System.out.println("10随机值:"+list.toString()); ArrayList maxList = new ArrayList<>(); for(int i = 0; i < 3 ; i++){ Integer max = list.get(0); for(int j = 0; j < list.size(); j ++){ Integer num = list.get(j); if(max < num){ max = num; } } maxList.add(max); while(list.contains(max)){ list.remove(max); } } System.out.println("前3个最大的:"+maxList.toString()); System.out.println("删除后:"+list.toString()); } }
第4题
案例:键盘录入一个字符串,去掉其中重复字符,打印出不同的那些字符,必须保证顺序。例如输入:aaaabbbcccddd,打印结果为:abcd。
效果如图:
代码实现:
public class Test08 { public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.println("请输入一个字符串:"); String srcStr = scan.next(); // char[] arr = srcStr.toCharArray(); // String str = srcStr.charAt(0)+""; // for(int i = 0; i < arr.length; i++){ // boolean flag = true; // for(int j = 0; j < str.length(); j++){ // if(arr[i] == str.charAt(j)){ // flag = false; // break; // } // } // if(flag){ // str += arr[i]; // } // } System.out.println(srcStr); LinkedHashSet
set = new LinkedHashSet<>(); for(int i = 0; i < srcStr.length(); i++){ set.add(srcStr.charAt(i)+""); } System.out.println("去重后:"); String result = ""; for(Object obj:set){ result += obj; } System.out.println(result); scan.close(); } }
第5题
案例:双色球规则:双色球每注投注号码由6个红色球号码和1个蓝色球号码组成。红色球号码从1—33中选择;蓝色球号码从1—16中选择;请随机生成一注双色球号码。(要求同色号码不重复)
效果如图:
代码实现:
public class Test05 { public static void main(String[] args) { TreeSet
red = new TreeSet<>(); Random rand = new Random(); while(red.size() < 6){ red.add(rand.nextInt(33)+1); } ArrayList list = new ArrayList<>(); list.addAll(red); list.add(rand.nextInt(16)+1); System.out.println("双色球所有号码:"+list); System.out.print("红色号码:"); for(int i = 0; i < list.size()-1; i++){ System.out.print(list.get(i)+" "); } System.out.println("蓝色号码:"+list.get(list.size()-1)); } }
第6题
案例:有如下四个学生的成绩
(1)用Comparable接口对下列四位同学的成绩做降序排序,如果成绩一样,那在成绩排序的基础上按照年龄由小到大排序。
(2)用Comparator实现按照姓名排序。
效果如下:
代码实现:
public class Test06 { public static void main(String[] args) { System.out.println("按照成绩和年龄排序:"); TreeSet
set = new TreeSet (); set.add(new Students("liusan",20,90.0)); set.add(new Students("lisi",22,90.0)); set.add(new Students("wangwu",20,99.0)); set.add(new Students("sunliu",22,100.0)); for(Object obj : set){ System.out.println(obj); } System.out.println("按照姓名排序:"); TreeSet all = new TreeSet (new Comparator (){ @Override public int compare(Students o1, Students o2) { Students s1 = (Students)o1; Students s2 = (Students)o2; return s1.getName().compareTo(s2.getName()); } }); for(Students stu:set){ all.add(stu); } for(Students stu1:all){ System.out.println(stu1); } } } class Students implements Comparable { private String name; private int age; private double souce; public Students() { super(); } public Students(String name, int age, double souce) { super(); this.name = name; this.age = age; this.souce = souce; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public double getSouce() { return souce; } public void setSouce(double souce) { this.souce = souce; } @Override public String toString() { return "Sutdent [name=" + name + ", age=" + age + ", souce=" + souce + "]"; } @Override public int compareTo(Students o) { if(this.getSouce()>o.getSouce()){ return -1; }else if(this.getSouce() < o.getSouce()){ return 1; } return this.getAge() - o.getAge(); } }
第7题
案例: 一个字符串,包含了空格等标点符号,写一个函数计算出出现每个字母和对应字母出现的次数。
给定字符串:Your。future:depends #@¥on your dreams, so go to sleep.
效果如图:
代码实现:
public class Test07 { public static void main(String[] args) { String str = "Your。future:depends #@¥on your dreams, so go to sleep."; HashMap
map = new HashMap<>(); str = str.replaceAll("[^a-zA-Z]", ""); // System.out.println(str); char[] arr = str.toCharArray(); for(int i = 0; i < arr.length; i++){ if(map.containsKey(arr[i])){ Integer count = map.get(arr[i]); map.put(arr[i], count+1); }else{ map.put(arr[i], 1); } } Set > entrySet = map.entrySet(); for(Object obj : entrySet){ System.out.println(obj); } } }
第8题
案例:模拟斗地主洗牌和发牌并对牌进行排序的代码实现.
提示:
(1)用String[] nums = { "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2" };存储数字
String[] colors = { "方片", "梅花", "红桃", "黑桃" };存储花色
(2)单独处理大王、小王
(3)将54张牌的索引[1,54]数字依次存储到一个ArrayList中
(4)用nums和colors数组的组合,将54张牌存储的一个Map中,key是索引,范围是1-54,value是牌面,例如:(1,黑桃3),...(54,小王)
(5)对ArrayList进行洗牌打乱顺序
(6)依次取list中的索引,发给四位牌友,四位牌友的牌可以用TreeSet存储,这样可以按照索引大小排序,索引大小就是牌的大小
(7)遍历结果,TreeSet中存储的是牌的索引,显示的时候从Map中取出牌显示
效果如图:
代码实现:
public class Test08 { public static void main(String[] args) { String[] dian = {"3","4","5","6","7","8","9","10","J","Q","K","A","2"}; String[] color = {"方片","梅花","红桃","黑桃"}; String[] wang = {"大王","小王"}; //54索引--->key List
list = new ArrayList<>(); for(int i = 1; i < 55; i++){ list.add(i); } //将花色和点数组合放入列表中---->value List pai = new ArrayList<>(); for(int i = 0; i < color.length; i++){ for(int j = 0; j < dian.length; j++){ pai.add(color[i] + dian[j]); } } pai.add(wang[0]); pai.add(wang[1]); //key-value对放入map集合中 HashMap map = new HashMap<>(); for(int i = 0; i < list.size(); i++){ map.put(list.get(i), pai.get(i)); } // System.out.println(list.get(0).getClass()); // System.out.println(pai.get(0).getClass()); // Set entrySet = map.entrySet(); // for(Object obj : entrySet){ // System.out.println(obj); // } //洗牌 Collections.shuffle(list); // for (String string : pai) { // System.out.println(string); // } //发牌 TreeSet left = new TreeSet<>(); TreeSet right = new TreeSet<>(); TreeSet up = new TreeSet<>(); TreeSet me = new TreeSet<>(); TreeSet lastCards = new TreeSet<>(); for(int i = 0; i < 13; i++){ left.add(list.remove(0)); right.add(list.remove(0)); up.add(list.remove(0)); me.add(list.remove(0)); } lastCards.addAll(list); // for(Object obj : left){ // System.out.println(obj); // } //看牌 lookPoker("左边玩家", left , map); lookPoker("右边玩家", right , map); lookPoker("上边玩家", up , map); lookPoker("我", me , map); lookPoker("底牌", lastCards , map); } public static void lookPoker(String name, TreeSet ts , HashMap map){ System.out.println(name + "的牌是:"); for(Integer index : ts){ System.out.print(map.get(index) + " "); } System.out.println("\n---------------------------------------------------------------------------"); } }
今天就到这里吧,写了几个小时了,下个周末继续喔。