1.设计一个敏感词过滤程序
WordFilter类
属性:数组类型[]存放敏感词
设计一个方法,调用这个方法(传参,可能会包含敏感词的字符串),返回过滤后的新的字符串
public static String filter(String word){
//过滤代码
return word;
}
要求:可以添加多个要过滤的敏感词{"枪","死","打劫","共产党"}
如果是一个字符就替换一个*,如果两个字符就替换**,以此类推
package com.hp.damo.demo01;
import java.util.Scanner;
/*
* 要求:可以添加多个要过滤的敏感词{"枪","死","打劫","共产党"}
如果是一个字符就替换一个*,如果两个字符就替换**,以此类推
* */
public class WordFilter {
public static String filter(String word){
String[] a={"枪","死","打劫","共产党"};
//获取用户键盘输入的内容
Scanner scanner=new Scanner(System.in);
System.out.println("请输入:");
String b=scanner.next();
//让数组中的元素一个一个进行比较
for(int i=0;i<a.length;++i){
if(b.contains(a[i])) {
b=b.replace(a[1],"*");
b=b.replace(a[2],"**");
b=b.replace(a[3],"***");
System.out.println(b);
}
}
return b;
}
public static void main(String[] args) {
WordFilter h = new WordFilter();
String s="";
filter(s);
filter(s);
}
}
2.国庆倒计时
创建一个国庆节2022年10月1日的日期对象gdDate
gdDate获取从1970年过了多少毫秒数
while(){
创建一个当前日期对象nowDate
nowDate获取从1970年过滤多少毫秒数
gdDate的毫秒数-nowDate的毫秒数,得到毫秒差
把毫秒数转为:xx天xx小时xx分钟xx秒
sout打印输出拼接好的倒计时
Thread.sleep(1000);
}
package com.hp.damo.demo02;
import javax.swing.*;
import java.util.Calendar;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
/*
* 创建一个国庆节2022年10月1日的日期对象gdDate
gdDate获取从1970年过了多少毫秒数
while(){
创建一个当前日期对象nowDate
nowDate获取从1970年过滤多少毫秒数
gdDate的毫秒数-nowDate的毫秒数,得到毫秒差
把毫秒数转为:xx天xx小时xx分钟xx秒
sout打印输出拼接好的倒计时
Thread.sleep(1000);
}*/
public class NationalDay {
long gdDate;
long nowDate;
long distTime;
long day, hour, minutes, seconds;
public NationalDay() {
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, 00, 00, 00);
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 NationalDay();
}
}
3.使用Map集合来做一个不同姓氏人数的统计
有一个String数组保存着10个人的姓名{"张三","李四","王二"...}
通过程序设计,把不同姓氏的姓氏和人数保存到Map集合中
package com.hp.damo.demo04;
import java.util.HashMap;
/*
* 使用Map集合来做一个不同姓氏人数的统计
有一个String数组保存着10个人的姓名{"张三","李四","李四"...}
通过程序设计,把不同姓氏的姓氏和人数保存到Map集合中
* */
public class h {
public HashMap<String,Integer> gather(String[] arr){
//创建一个map集合
HashMap<String, Integer> hm = new HashMap<>();
for (String s:arr) {
String s1 = String.valueOf(s.charAt(0));
hm.put(s1,hm.containsKey(s1) ? hm.get(s1)+1 : 1);
}
return hm;
}
public static void main(String[] args) {
h x = new h();
HashMap<String, Integer> hm = x.gather(new String[]{"张三","李四", "李四", "王五","赵六","孙钱","小小怪","小小怪","小小怪","小小怪"});
System.out.println(hm);
}
}
4.模拟扑克牌
花色:♠ ♥ ♦ ♣
* 牌号:A 2 3 4 5 6 7 8 9 10 J Q K
* 大王、小王
*
* 1.生成一副牌
* 2.然后发牌
package com.hp.damo.demo03;
import javax.smartcardio.Card;
import java.util.*;
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 a = r.nextInt(54);
System.out.print(total[a]+" ");
}
System.out.println();
}
//发牌
public void Deal(){
//获取牌
List<PokerCard> pkcList = this.getPokerList();
Object[] total = pkcList.toArray();
//存储4个玩家的牌
Object[][] plays = new Object[4][12];
//存储当前剩余牌的数量
int leftNum = 54;
//发牌是随机的
Random r = new Random();
for (int i = 0; i < 12; 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 < 6;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.damo.demo03;
public class PokerCard {
private String color; //花色
private String cardNum; //牌号
public PokerCard() {
}
public PokerCard(String color, String cardNum) {
this.color = color;
this.cardNum = cardNum;
}
@Override
public String toString() {
return color +""+ cardNum ;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public String getCardNum() {
return cardNum;
}
public void setCardNum(String cardNum) {
this.cardNum = cardNum;
}
}
package com.hp.damo.demo03;
public class Test {
public static void main(String[] args) {
Poker poker = new Poker();
System.out.println(poker.getPokerList());
poker.Shuffle();
poker.Deal();
}
}