综合案例
1. 随机点名
班级里有n个学生
学生属性:姓名,年龄,性别
实现随机点名器
package com. test ;
import java. util. ArrayList ;
import java. util. Collections ;
import java. util. Random ;
public class Test01 {
public static void main ( String [ ] args) {
ArrayList < String > list = new ArrayList < > ( ) ;
Collections . addAll ( list, "杨志飞" , "马紫雨" , "翁蕊" , "杨坤" , "刘柏良" , "王兴旺" , "张梓然" , "张宏涛" ,
"李文帅" , "程军帅" , "宫和坤" , "马健" , "王安" , "付立臣" , "高瑞" , "张泽坤" , "韩立帅" ) ;
Collections . shuffle ( list) ;
String name = list. get ( 0 ) ;
System . out. println ( name) ;
}
}
2.有概率的随机点名
班级有N个学生
要求:
70%概率男生
30%概率女生
package com. test ;
import java. util. ArrayList ;
import java. util. Collections ;
import java. util. Random ;
public class Test02 {
public static void main ( String [ ] args) {
ArrayList < Integer > list = new ArrayList < > ( ) ;
Collections . addAll ( list, 1 , 1 , 1 , 1 , 1 , 1 , 1 ) ;
Collections . addAll ( list, 0 , 0 , 0 ) ;
Collections . shuffle ( list) ;
System . out. println ( list) ;
Random r = new Random ( ) ;
int index = r. nextInt ( list. size ( ) ) ;
int number = list. get ( index) ;
System . out. println ( number) ;
ArrayList < String > boyList = new ArrayList < > ( ) ;
ArrayList < String > girlList = new ArrayList < > ( ) ;
Collections . addAll ( boyList, "程军帅" , "宫和坤" , "马健" , "王安" , "付立臣" , "高瑞" , "张泽坤" , "韩立帅" ) ;
Collections . addAll ( girlList, "李沁" , "李一桐" , "杨钰莹" , "杨幂" , "杨颖" , "白梦妍" , "杨超越" , "赵丽颖" ) ;
if ( number== 1 ) {
System . out. println ( boyList. get ( r. nextInt ( boyList. size ( ) ) ) ) ;
} else {
System . out. println ( girlList. get ( r. nextInt ( girlList. size ( ) ) ) ) ;
}
}
}
3. 不重复的点名
班级里有N个学生
要求:
被点到的学生不会再被点到
但是如果班级里的学生都点完了,需要重新开启第二轮点名
package com. test ;
import java. util. ArrayList ;
import java. util. Collections ;
import java. util. Random ;
public class Test03 {
public static void main ( String [ ] args) {
ArrayList < String > list1 = new ArrayList < > ( ) ;
Collections . addAll ( list1, "杨志飞" , "马紫雨" , "翁蕊" , "杨坤" , "刘柏良" , "王兴旺" , "张梓然" , "张宏涛" ,
"李文帅" , "程军帅" , "宫和坤" , "马健" , "王安" , "付立臣" , "高瑞" , "张泽坤" , "韩立帅" ) ;
ArrayList < String > list2 = new ArrayList < > ( ) ;
for ( int j = 1 ; j <= 10 ; j++ ) {
System . out. println ( "================第" + j+ "轮点名开始了================" ) ;
int size = list1. size ( ) ;
Random r = new Random ( ) ;
for ( int i = 0 ; i < size; i++ ) {
int index = r. nextInt ( list1. size ( ) ) ;
String name = list1. remove ( index) ;
list2. add ( name) ;
System . out. println ( name) ;
}
list1. addAll ( list2) ;
list2. clear ( ) ;
}
}
}
4. Map集合
需求:
定义一个Map集合,键表示省份名称province ,值表示市city,但是市会有多个
遍历格式:
江苏省 = 南京市,扬州市,苏州市,无锡市,常州市
湖北省 = 武汉市,孝感市,十堰市,宜昌市,鄂州市
河北省 = 沧州市,保定市,唐山市,衡水市,邢台市
package com. test ;
import java. util. * ;
public class Test04 {
public static void main ( String [ ] args) {
HashMap < String , ArrayList < String > > hm = new HashMap < > ( ) ;
ArrayList < String > city1 = new ArrayList < > ( ) ;
Collections . addAll ( city1, "南京市" , "扬州市" , "苏州市" , "无锡市" , "常州市" ) ;
ArrayList < String > city2 = new ArrayList < > ( ) ;
Collections . addAll ( city2, "武汉市" , "孝感市" , "十堰市" , "宜昌市" , "鄂州市" ) ;
ArrayList < String > city3 = new ArrayList < > ( ) ;
Collections . addAll ( city3, "沧州市" , "保定市" , "唐山市" , "衡水市" , "邢台市" ) ;
hm. put ( "江苏省" , city1) ;
hm. put ( "湖北省" , city2) ;
hm. put ( "河北省" , city3) ;
System . out. println ( hm) ;
for ( String key : hm. keySet ( ) ) {
ArrayList < String > value = hm. get ( key) ;
StringJoiner stringJoiner = new StringJoiner ( ", " , "" , "" ) ;
for ( String city : value) {
stringJoiner. add ( city) ;
}
System . out. println ( key + "=" + stringJoiner) ;
}
for ( Map. Entry < String , ArrayList < String > > entry : hm. entrySet ( ) ) {
String key = entry. getKey ( ) ;
ArrayList < String > value = entry. getValue ( ) ;
StringBuilder stringBuilder = new StringBuilder ( ) ;
stringBuilder = stringBuilder. append ( key) . append ( "=" ) . append ( value) ;
System . out. println ( stringBuilder) ;
}
}
}
斗地主1(控制台版)
完成控制台的三步:
1. 准备牌
2. 洗牌
3. 发牌
package com. doudizhu1 ;
import java. util. ArrayList ;
import java. util. Collections ;
public class PokerGame {
static ArrayList < String > list = new ArrayList < > ( ) ;
static {
String [ ] color = { "♦" , "♥" , "♣" , "♠" } ;
String [ ] number = { "3" , "4" , "5" , "6" , "7" , "8" , "9" , "10" , "J" , "Q" , "K" , "A" , "2" } ;
for ( String c : color) {
for ( String n : number) {
list. add ( c+ n) ;
}
}
list. add ( "小王" ) ;
list. add ( "大王" ) ;
}
public PokerGame ( ) {
Collections . shuffle ( list) ;
ArrayList < String > bottomCard = new ArrayList < > ( ) ;
ArrayList < String > player1 = new ArrayList < > ( ) ;
ArrayList < String > player2 = new ArrayList < > ( ) ;
ArrayList < String > player3 = new ArrayList < > ( ) ;
for ( int i = 0 ; i < list. size ( ) ; i++ ) {
String poker = list. get ( i) ;
if ( i<= 2 ) {
bottomCard. add ( poker) ;
continue ;
}
if ( i % 3 == 0 ) {
player1. add ( poker) ;
} else if ( i % 3 == 1 ) {
player2. add ( poker) ;
} else {
player3. add ( poker) ;
}
}
lookPoker ( "底牌" , bottomCard) ;
lookPoker ( "钢脑壳" , player1) ;
lookPoker ( "大帅比" , player2) ;
lookPoker ( "蛋筒" , player3) ;
}
public void lookPoker ( String name, ArrayList < String > list) {
System . out. print ( name + ": " ) ;
for ( String poker : list) {
System . out. print ( poker+ " " ) ;
}
System . out. println ( ) ;
}
}
斗地主2(控制台版)
完成控制台的三步:
1. 准备牌
2. 洗牌
3. 发牌
4. 排序(两种方式排序) 方式一:利用序号排序
方式二:给每一张牌计算价值
方式一:利用序号排序
package com. doudizhu2 ;
import java. util. ArrayList ;
import java. util. Collections ;
import java. util. HashMap ;
import java. util. TreeSet ;
public class PokerGame {
static HashMap < Integer , String > hm = new HashMap < > ( ) ;
static ArrayList < Integer > list = new ArrayList < > ( ) ;
static {
String [ ] color = { "♦" , "♥" , "♣" , "♠" } ;
String [ ] number = { "3" , "4" , "5" , "6" , "7" , "8" , "9" , "10" , "J" , "Q" , "K" , "A" , "2" } ;
int serialNumber = 1 ;
for ( String n : number) {
for ( String c : color) {
hm. put ( serialNumber, c+ n) ;
list. add ( serialNumber) ;
serialNumber++ ;
}
}
hm. put ( serialNumber, "小王" ) ;
list. add ( serialNumber) ;
serialNumber++ ;
hm. put ( serialNumber, "大王" ) ;
list. add ( serialNumber) ;
System . out. println ( hm) ;
}
public PokerGame ( ) {
Collections . shuffle ( list) ;
TreeSet < Integer > bottomCard = new TreeSet < > ( ) ;
TreeSet < Integer > player1 = new TreeSet < > ( ) ;
TreeSet < Integer > player2 = new TreeSet < > ( ) ;
TreeSet < Integer > player3 = new TreeSet < > ( ) ;
for ( int i = 0 ; i < list. size ( ) ; i++ ) {
Integer serialNumber = list. get ( i) ;
if ( i <= 2 ) {
bottomCard. add ( serialNumber) ;
continue ;
}
if ( i % 3 == 0 ) {
player1. add ( serialNumber) ;
} else if ( i % 3 == 1 ) {
player2. add ( serialNumber) ;
} else {
player3. add ( serialNumber) ;
}
}
lookPoker ( "底牌" , bottomCard) ;
lookPoker ( "钢脑壳" , player1) ;
lookPoker ( "大帅比" , player2) ;
lookPoker ( "蛋 筒" , player3) ;
}
public void lookPoker ( String name, TreeSet < Integer > treeSet) {
System . out. print ( name+ ": " ) ;
for ( Integer serialNumber : treeSet) {
String poker = hm. get ( serialNumber) ;
System . out. print ( poker+ " " ) ;
}
System . out. println ( ) ;
}
}
方式二:给每一张牌计算价值
package com. doudizhu3 ;
import java. util. ArrayList ;
import java. util. Collections ;
import java. util. Comparator ;
import java. util. HashMap ;
public class PokerGame {
static ArrayList < String > list = new ArrayList < > ( ) ;
static HashMap < String , Integer > hm = new HashMap < > ( ) ;
static {
String [ ] color = { "♦" , "♥" , "♣" , "♠" } ;
String [ ] number = { "3" , "4" , "5" , "6" , "7" , "8" , "9" , "10" , "J" , "Q" , "K" , "A" , "2" } ;
for ( String c : color) {
for ( String n : number) {
list. add ( c+ n) ;
}
}
list. add ( " 小王" ) ;
list. add ( " 大王" ) ;
hm. put ( "J" , 11 ) ;
hm. put ( "Q" , 12 ) ;
hm. put ( "K" , 13 ) ;
hm. put ( "A" , 14 ) ;
hm. put ( "2" , 15 ) ;
hm. put ( "小王" , 50 ) ;
hm. put ( "大王" , 100 ) ;
}
public PokerGame ( ) {
Collections . shuffle ( list) ;
ArrayList < String > bottomCard = new ArrayList < > ( ) ;
ArrayList < String > player1 = new ArrayList < > ( ) ;
ArrayList < String > player2 = new ArrayList < > ( ) ;
ArrayList < String > player3 = new ArrayList < > ( ) ;
for ( int i = 0 ; i < list. size ( ) ; i++ ) {
String poker = list. get ( i) ;
if ( i<= 2 ) {
bottomCard. add ( poker) ;
continue ;
}
if ( i % 3 == 0 ) {
player1. add ( poker) ;
} else if ( i % 3 == 1 ) {
player2. add ( poker) ;
} else {
player3. add ( poker) ;
}
}
order ( bottomCard) ;
order ( player1) ;
order ( player2) ;
order ( player3) ;
lookPoker ( "底牌" , bottomCard) ;
lookPoker ( "钢脑壳" , player1) ;
lookPoker ( "大帅比" , player2) ;
lookPoker ( "蛋 筒" , player3) ;
}
public void order ( ArrayList < String > list) {
Collections . sort ( list, new Comparator < String > ( ) {
@Override
public int compare ( String o1, String o2) {
String color1 = o1. substring ( 0 , 1 ) ;
int value1 = getValue ( o1) ;
String color2 = o2. substring ( 0 , 1 ) ;
int value2 = getValue ( o2) ;
int i = value1 - value2;
return i == 0 ? color1. compareTo ( o2) : i;
}
} ) ;
}
public int getValue ( String poker) {
String number = poker. substring ( 1 ) ;
if ( hm. containsKey ( number) ) {
return hm. get ( number) ;
} else {
return Integer . parseInt ( number) ;
}
}
public void lookPoker ( String name, ArrayList < String > list) {
System . out. print ( name + ": " ) ;
for ( String poker : list) {
System . out. print ( poker+ " " ) ;
}
System . out. println ( ) ;
}
}
斗地主3(页面版)