java进阶打卡12

JDK9的新特性

List接口,Set接口,Map接口:里边增加了一个静态的方法of,可以给集合一次性添加多个元素
    static <E> list<E> of (E... elements)
    使用前提:
        当集合中存储的元素的个数已经确定了,不再改变时使用
注意:
    1. of方法只适用于List接口,Set接口,Map接口,不适用于接口的实现类
    2. of方法的返回值时一个不能改变的集合,集合不能再使用add、put方法添加元素,会抛出异常
    3. Set接口和Map接口在调用of方法的时候,不能有重复的元素,否则会抛出异常

List<String> list = List.of("a","b","a","c","d");
System.out.println(list); // [a, b, a, c, d]
// list.add("w"); // UnsupportedOperationException:不支持操作异常

// Set<String> set = Set.of("a", "b", "a", "c", "d");//IllegalArgumentException:非法参数异常,有重复元素
Set<String> set = Set.of("a", "b", "c", "d");
System.out.println(set);
//set.add("w");//UnsupportedOperationException:不支持操作异常

Debug

Debug调试程序:
    可以让代码逐行执行,查看代码执行的过程,调试程序中出现的bug
使用方式:
    在行号的右边,鼠标左键单击,添加断点(每个方法的第一行,哪里又有bug添加到哪里)
    右键,选择Debug执行程序,程序就会停留在添加的第一个断点处
执行程序:
    f8:逐行执行程序
    f7:进入到方法中
    shift + f8:跳出方法
    f9:跳到下一个断点,如果没有下一个断点,那么就结束程序
    ctrl + f2:退出debug模式,停止程序
    Console:切换到控制台

斗地主综合案例:有序版本

  • 分析
1. 准备牌
	特殊牌:大王、小王
	52张牌:循环嵌套遍历两个集合/数组,组装52张牌
	List<String> colors = List.of("♠","♥","♣","♦");
	List<String> numbers = List.of("2","A","K"..."3");

Map<Integer, String>	List<Integer>	   List<Integer>
牌的索引	 组装好的牌		牌的索引			   牌的索引  
key	     value			key			        key     【集合的索引】
0	     大王			0			        44	        0
1	     小王			1	shuffle(List)	22	        1
2	     ♠2		   		2	     -->		0			2
3	     ♠3		    	3			        ...			3
4	     ♠4		   	 	4		        	1			4
...	     ...			...			        9			...
53	     ♦3		    	53			        33			53

2. 洗牌
使用Collections中的方法shuffle(List)
3. 发牌
一人一张轮流发牌,每人17张	集合索引%3	剩余3张给底牌
4. 排序
使用Collections中的方法sort(List)
5. 看牌:可以使用查表法
遍历一个集合,获取另一个集合的key,通过key查找到value

遍历玩家和底牌的List集合,获取到Map集合的key,通过key找到value值

先判断底牌(i>=51)集合 底牌3张	sort(List)
(i%3==0) 集合 玩家1的牌17张		sort(List)
(i%3==1) 集合 玩家2的牌17张		sort(List)
(i%3==2) 集合 玩家3的牌17张		sort(List)
  • main方法
1. 准备牌
2. 洗牌
3. 发牌
4. 排序
5. 看牌

public static void main(String[] args) {
        HashMap<Integer, String> poker = new HashMap<>();
        ArrayList<Integer> pokerIndex = new ArrayList<>();
        List<String> colors = List.of("♠", "♥", "♣", "♦");
        List<String> numbers = List.of("2", "A", "K", "Q", "J", "10", "9", "8", "7", "6", "5", "4", "3");
        
        int index = 0;
        poker.put(index,"大王");
        pokerIndex.add(index);
        index++;
        poker.put(index,"小王");
        pokerIndex.add(index);
        index++;
       
        for (String number : numbers) {
            for (String color : colors) {
                poker.put(index,color+number);
                pokerIndex.add(index);
                index++;
            }
        }
//        System.out.println(poker);
//        System.out.println(pokerIndex);
     
        Collections.shuffle(pokerIndex);
//        System.out.println(pokerIndex);

        ArrayList<Integer> player01 = new ArrayList<>();
        ArrayList<Integer> player02 = new ArrayList<>();
        ArrayList<Integer> player03 = new ArrayList<>();
        ArrayList<Integer> diPai = new ArrayList<>();
        for (int i = 0; i < pokerIndex.size(); i++) {
            Integer in = pokerIndex.get(i);
            if(i>=51) {
                diPai.add(i);
            } else if(i%3==0) {
                player01.add(i);
            } else if(i%3==1) {
                player02.add(i);
            } else if(i%3==2) {
                player03.add(i);
            }
        }

        Collections.sort(player01);
        Collections.sort(player02);
        Collections.sort(player03);
        Collections.sort(diPai);

        lookPoker("player01",poker,player01);
        lookPoker("player02",poker,player02);
        lookPoker("player03",poker,player03);
        lookPoker("diPai",poker,diPai);
    }
  • 看牌的方法
定义一个看牌的方法,提高代码的复用性
    参数:
        String name:玩家名称
        HashMap<Integer, String> poker:存储牌的poker集合
        ArrayList<Integer> list:存储玩家和底牌的list集合
    查表法:
        遍历玩家或者底牌集合,获取牌的索引
        使用牌的索引,去Map集合中,找到对应的牌

public static void lookPoker(String name, HashMap<Integer, String> poker, ArrayList<Integer> list) {
	System.out.print(name + ":");
    for (Integer key : list) {
    	String value = poker.get(key);
        System.out.println(value + " ");
    }
    System.out.println();
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java员工打卡签到代码的实现方式有很多种,以下是其中一种可能的实现方式: ```java import java.util.Date; public class Employee { private String name; private Date lastSignIn; public Employee(String name) { this.name = name; } public void signIn() { Date now = new Date(); System.out.println(name + "签到成功,时间:" + now); lastSignIn = now; } public void signOut() { Date now = new Date(); System.out.println(name + "签退成功,时间:" + now); } public void checkInStatus() { if (lastSignIn == null) { System.out.println(name + "尚未签到"); } else { System.out.println(name + "上次签到时间:" + lastSignIn); } } } ``` 上面的代码定义了一个`Employee`类,其中包含了员工的姓名和上次签到时间。类中有三个方法:`signIn()`、`signOut()`和`checkInStatus()`。`signIn()`方法表示员工签到,会打印出员工姓名和当前时间,并将当前时间记录为上次签到时间;`signOut()`方法表示员工签退,会打印出员工姓名和当前时间;`checkInStatus()`方法表示查询员工的签到状态,会打印出员工姓名和上次签到时间(如果已经签到过),否则会提示尚未签到。 如果要使用这段代码,可以在其他类中创建`Employee`对象,并调用其中的方法来完成打卡签到功能。例如: ```java public class Main { public static void main(String[] args) { Employee emp1 = new Employee("张三"); emp1.signIn(); emp1.checkInStatus(); emp1.signOut(); } } ``` 这段代码创建了一个名为`emp1`的`Employee`对象,姓名为“张三”。接着调用了`signIn()`方法进行签到,`checkInStatus()`方法查询签到状态,最后调用了`signOut()`方法进行签退。运行这段代码后,会打印出以下结果: ``` 张三签到成功,时间:Thu Jul 22 14:47:23 CST 2021 张三上次签到时间:Thu Jul 22 14:47:23 CST 2021 张三签退成功,时间:Thu Jul 22 14:47:28 CST 2021 ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值