1.设计一个敏感词过滤程序
WordFilter类
属性:数组类型[]存放敏感词
设计一个方法,调用这个方法(传参,可能会包含敏感词的字符串),返回过滤后的新的字符串
public static String filter(String word){
//过滤代码
return word;
}
要求:可以添加多个要过滤的敏感词{"枪","死","打劫","共产党"}
如果是一个字符就替换一个*,如果两个字符就替换**,以此类推
package demo4;
/**
* 1.设计一个敏感词过滤程序
* WordFilter类
* 属性:数组类型[]存放敏感词
* 设计一个方法,调用这个方法(传参,可能会包含敏感词的字符串),返回过滤后的新的字符串
* public static String filter(String word){
* //过滤代码
* return word;
* }
* 要求:可以添加多个要过滤的敏感词{"枪","死","打劫","共产党"}
* 如果是一个字符就替换一个*,如果两个字符就替换**,以此类推
*/
public class Demo01 {
private static String m(String s) {
String [] str ={"枪","死","打劫","共产党"};
for (int i = 0; i < str.length; i++) {
if (s.contains(str[i])) {
s = s.replace(str[i],"*");
s = s.replace(str[2],"**");
s = s.replace(str[3],"***");
System.out.println(s);
}
}
return s;
}
public static void main(String[] args) {
String s ="不要动!打劫!再动一枪崩死你!!!" + "\n" + "共产党万岁";
m(s);
}
}
2.国庆倒计时
创建一个国庆节2022年10月1日的日期对象gdDate
gdDate获取从1970年过了多少毫秒数
while(){
创建一个当前日期对象nowDate
nowDate获取从1970年过滤多少毫秒数
gdDate的毫秒数-nowDate的毫秒数,得到毫秒差
把毫秒数转为:xx天xx小时xx分钟xx秒
sout打印输出拼接好的倒计时
Thread.sleep(1000);
}
package demo4;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* .国庆倒计时
* 创建一个国庆节2022年10月1日的日期对象gdDate
* gdDate获取从1970年过了多少毫秒数
* while(){
* 创建一个当前日期对象nowDate
* nowDate获取从1970年过滤多少毫秒数
* gdDate的毫秒数-nowDate的毫秒数,得到毫秒差
* 把毫秒数转为:xx天xx小时xx分钟xx秒
* sout打印输出拼接好的倒计时
* Thread.sleep(1000);
*/
public class Demo02 {
public static void main(String[] args) {
Date date1 = new Date();
System.out.println(date1);
int year = date1.getYear()+1900;
int month = date1.getMonth()+1;
int date = date1.getDate();
int hours = date1.getHours();
int minutes = date1.getMinutes();
int seconds = date1.getSeconds();
long nowtime= date1.getTime();
System.out.println(year+"年"+month+"月"+date+"日"+hours+":"+minutes+":"+seconds);
System.out.println("从1978年至今的毫秒数:" + date1.getTime());
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date2= null;
try {
date2 = sdf.parse("2022-10-01 00:00:00");
} catch (ParseException e) {
e.printStackTrace();
}
long endtime=date2.getTime();
try {
long midtime=endtime-nowtime;
long nd = 1000*24*60*60;
long nh = 1000*60*60;
long nm = 1000*60;
long ns = 1000;
long midday=midtime/nd;
long hh=midtime%nd/nh;
long mm=midtime%nd%nh/nm;
long ss=midtime%nd%nh%nm/ns;
System.out.println("离国庆假期还有:"+"\n"+midday+"天"+hh+"时"+mm+"分"+ss+"秒");
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
3.使用Map集合来做一个不同姓氏人数的统计
有一个String数组保存着10个人的姓名{"张三","李四","王二"...}
通过程序设计,把不同姓氏的姓氏和人数保存到Map集合中
package demo4;
import java.util.HashMap;
import java.util.Map;
/**
* 使用Map集合来做一个不同姓氏人数的统计
* 有一个String数组保存着10个人的姓名{"张三","李四","王二"...}
* 通过程序设计,把不同姓氏的姓氏和人数保存到Map集合中
*/
public class Demo03 {
public static void main(String[] args) {
String[] name = new String[]{"孙悟空","鲁班七号","鲁班大师","孙尚香","蒙恬","蒙牙","孙策","韩信","百里玄策","百里守约"};
Map map = new HashMap<>();
String sun = null;
String lu = null;
String han = null;
String bai = null;
String meng = null;
int num = 0;
int num1 = 0;
int num2 = 0;
int num3 = 0;
int num4 = 0;
for (int i =0 ; i<name.length;i++) {
if (name[i].contains("孙")){
sun = name[i];
num++;
}else if (name[i].contains("鲁")){
lu = name[i];
num1++;
}else if (name[i].contains("百")){
bai = name[i];
num2++;
}else if (name[i].contains("韩")){
han = name[i];
num3++;
}else if (name[i].contains("蒙")){
meng = name[i];
num4++;
}
}
map.put(sun.charAt(0),num);
map.put(lu.charAt(0),num1);
map.put(bai.charAt(0),num2);
map.put(han.charAt(0),num3);
map.put(meng.charAt(0),num4);
System.out.println(map);
}
}
4.模拟扑克牌
花色:♠ ♥ ♦ ♣
* 牌号:A 2 3 4 5 6 7 8 9 10 J Q K
* 大王、小王
*
* 1.生成一副牌
* 2.然后发牌
package demo4.puke;
/**
* 一张扑克牌类(一张牌就是一个对象)
* 花色:♠ ♥ ♦ ♣
* 牌号:A 2 3 4 5 6 7 8 9 10 J Q K
* 大王、小王
*/
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 demo4.puke;
import demo4.puke.PokerCard;
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.然后发牌
*/
public class Poker {
//一副牌
private static List<PokerCard> pokerList;
private static ArrayList<PokerCard> A;
private static ArrayList<PokerCard> B;
private static ArrayList<PokerCard> C;
//无参构造器,初始化一副牌
public Poker() {
String[]colors={"♠","♥","♣","♦"};
pokerList = new ArrayList<>();
A = new ArrayList<>();
B = new ArrayList<>();
C = 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 static void xipai(){
Collections.shuffle(pokerList);
}
//发牌
public static void fapai(){
A.clear();
B.clear();
C.clear();
for (int i = 0; i < pokerList.size(); i++) {
if (i % 3 == 0){
A.add(pokerList.get(i));
}
if (i % 3 == 1){
B.add(pokerList.get(i));
}
if (i % 3 == 2){
C.add(pokerList.get(i));
}
}
}
public List<PokerCard> getPokerList() {
return pokerList;
}
public void setPokerList(List<PokerCard> pokerList) {
this.pokerList = pokerList;
}
public static void main(String[] args) {
Poker poker = new Poker();
System.out.println(poker.getPokerList());
xipai();
fapai();
System.out.println("小伟A" + A);
System.out.println("小肖B" + B);
System.out.println("小刘C" + C);
}
}