1.设计一个敏感词过滤程序
WordFilter类
属性:数组类型[]存放敏感词
设计一个方法,调用这个方法(传参,可能会包含敏感词的字符串),返回过滤后的新的字符串
public static String filter(String word){
//过滤代码
return word;
}
要求:可以添加多个要过滤的敏感词{"枪","死","打劫","共产党"}
如果是一个字符就替换一个*,如果两个字符就替换**,以此类推
package com.hp.zuoye;
/*
* 过滤字符串中的敏感词汇
* @param content 文本
* @param sensitiveWord 敏感词汇
* @return
*/
public class Demo1 {
public String filterSensitiveWords(String content, String sensitiveWord) {
if (content == null || sensitiveWord == null) {
return content;
}
//获取和敏感词汇相同数量的星号
String starChar = getStarChar(sensitiveWord.length());
//替换敏感词汇
return content.replace(sensitiveWord, starChar);
}
//大部分敏感词汇在10个以内,直接返回缓存的字符串
public static String[] starArr={"*","**","***","****","*****","******","*******","********","*********","**********"};
/**
* 生成n个星号的字符串
* @param length
* @return
*/
private static String getStarChar(int length) {
if (length <= 0) {
return "";
}
//大部分敏感词汇在10个以内,直接返回缓存的字符串
if (length <= 10) {
return starArr[length - 1];
}
//生成n个星号的字符串
char[] arr = new char[length];
for (int i = 0; i < length; i++) {
arr[i] = '*';
}
return new String(arr);
}
}
package com.hp.zuoye;
public class Test1 {
public static void main(String[] args) {
Demo1 d1 = new Demo1();
String a = d1.filterSensitiveWords("打劫!我有枪死", "枪死");
System.out.println("a="+a);
}
}
运行结果
a=打劫!我有**
国庆倒计时
创建一个国庆节2022年10月1日的日期对象gdDate
gdDate获取从1970年过了多少毫秒数
while(){
创建一个当前日期对象nowDate
nowDate获取从1970年过滤多少毫秒数
gdDate的毫秒数-nowDate的毫秒数,得到毫秒差
把毫秒数转为:xx天xx小时xx分钟xx秒
sout打印输出拼接好的倒计时
Thread.sleep(1000);
}
package com.hp.zuoye;
import java.util.Calendar;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.JLabel;
public class daojishi {
long gdDate;
long nowDate;
long distTime;
long day, hour, minutes, seconds;
public daojishi() {
CDown();
}
public void CDown() {
Timer timer = new Timer();
final JLabel jl = new JLabel();
timer.schedule(new TimerTask() {
@Override
public void run() {
Calendar cal = Calendar.getInstance();
cal.set(2022, 10, 1, 0, 0, 0);
gdDate = cal.getTimeInMillis();//设置的时间
nowDate = new Date().getTime();//当前时间
distTime = gdDate - nowDate;//毫秒差
day = ((distTime / 1000) / (3600 * 24));
hour = ((distTime / 1000) - day * 86400) / 3600;
minutes = ((distTime / 1000) - day * 86400 - hour * 3600) / 60;
seconds = (distTime / 1000) - day * 86400 - hour * 3600 - minutes * 60;
System.out.println((" 国庆倒计时还有" + day + " 天 " + hour + "小时 :" + minutes + "分钟 :" + seconds + "秒"));
}
}, 0, 1000);
}
public static void main(String[] args) {
new daojishi();
}
}
运行结果
国庆倒计时还有110 天 3小时 :34分钟 :19秒
使用Map集合来做一个不同姓氏人数的统计
有一个String数组保存着10个人的姓名{"张三","李四","王二"...}
通过程序设计,把不同姓氏的姓氏和人数保存到Map集合中
4.模拟扑克牌
花色:♠ ♥ ♦ ♣
* 牌号:A 2 3 4 5 6 7 8 9 10 J Q K
* 大王、小王
*
* 1.生成一副牌
* 2.然后发牌
package com.hp.zuoye;
import javax.smartcardio.Card;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
/**
* 扑克
* 花色:♠ ♥,⭐,♣
* 牌号:A 2 3 4 5 6 7 8 9 10 J Q K
* 大王 下王
*
* 1.生成一副牌
* 2.然后发牌
* 3.
*/
public class Poker {
//一副牌
private List<PokerCard> pokerList;
//无参构造器,初始化一副牌
public Poker() {
String[]colors={"♠","♥","♣","♦"};
pokerList = new ArrayList<>();
//接下来生成牌
//A牌的四种花色
for (int i = 0; i < colors.length; i++) {//四种花色
PokerCard pc = new PokerCard(colors[i], "A");
pokerList.add(pc);
}
//2-10的牌
for (int i = 2; i <= 10; i++) {
//2-10牌的四种花色
for (int j = 0; j < colors.length; j++) {//四种花色
PokerCard pc = new PokerCard(colors[j], i+"");
pokerList.add(pc);
}
}
//JQK牌的四种花色
for (int i = 0; i < colors.length; i++) {//四种花色
PokerCard pc = new PokerCard(colors[i], "J");
pokerList.add(pc);
}
for (int i = 0; i < colors.length; i++) {//四种花色
PokerCard pc = new PokerCard(colors[i], "Q");
pokerList.add(pc);
}
for (int i = 0; i < colors.length; i++) {//四种花色
PokerCard pc = new PokerCard(colors[i], "K");
pokerList.add(pc);
}
//大小王
PokerCard pc1 = new PokerCard(null, "大王");
pokerList.add(pc1);
PokerCard pc2 = new PokerCard(null, "小王");
pokerList.add(pc2);
}
//洗牌
public void Shuffle(){
//获取牌
List<PokerCard> pkcList = this.getPokerList();
Object[] total = pkcList.toArray();
//洗牌是随机的
Random r = new Random();
for (int j = 0; j < total.length; j++) {
int sj = r.nextInt(54);
System.out.print(total[sj]+" ");
}
System.out.println();
}
//发牌
public void Deal(){
//获取牌
List<PokerCard> pkcList = this.getPokerList();
Object[] total = pkcList.toArray();
//存储三个玩家的牌
Object[][] plays = new Object[3][17];
//存储当前剩余牌的数量
int leftNum = 54;
//发牌是随机的
Random r = new Random();
//分别把牌分给3个人,留三张底牌,也就是说一个人能分到17张牌
for (int i = 0; i < 17; i++) {
//为每个人发牌
for (int j = 0; j < plays.length; j++) {
//System.out.println(j);
int ranNumber = r.nextInt(leftNum);
//发牌
plays[j][i] = total[ranNumber];
//移动发的牌
total[ranNumber] = total[leftNum - 1];
leftNum--;
}
}
for (int i = 0; i < plays.length; i++) {
System.out.print("玩家:"+i+"的牌");
for (int j = 0; j < plays[i].length; j++) {
System.out.print(plays[i][j]);
}
System.out.println();
}
System.out.print("底牌:");
for(int i = 0;i < 3;i++){
System.out.print(total[i]);
}
System.out.println();
}
public List<PokerCard> getPokerList() {
return pokerList;
}
public void setPokerList(List<PokerCard> pokerList) {
this.pokerList = pokerList;
}
}
package com.hp.zuoye;
/**
* 一张扑克牌类
* 花色:♠ ♥,⭐,♣
* 牌号:A 2 3 4 5 6 7 8 9 10 J Q K
* 大王 下王
*/
public class PokerCard {
private String color;//花色
private String caredNum;//牌号
public PokerCard() {
}
public PokerCard(String color, String caredNum) {
this.color = color;
this.caredNum = caredNum;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public String getCaredNum() {
return caredNum;
}
public void setCaredNum(String caredNum) {
this.caredNum = caredNum;
}
@Override
public String toString() {
return color +
"," + caredNum ;
}
}
package com.hp.zuoye;
public class Test {
public static void main(String[] args) {
Poker poker = new Poker();
System.out.println(poker.getPokerList());
System.out.println(poker.getPokerList());
poker.Shuffle();
poker.Deal();
}
}
运行结果
玩家:0的牌♠,2null,小王♣,9♠,Q♠,9♥,8♠,J♠,5♣,J♠,K♣,A♥,10♣,Q♥,6♥,9♦,A♦,4
玩家:1的牌♥,7♦,9♣,8♥,2♣,10♠,10♦,K♥,Q♣,3♦,5♦,J♠,4♥,A♣,5♦,Q♦,3♣,7
玩家:2的牌♥,5♥,4♦,7♠,8♦,8♣,2♦,6null,大王♣,4♣,K♠,3♦,2♥,3♥,K♠,6♥,J♠,7
底牌:♠,A♣,6♦,10