斗地主排序以及音乐管理系统

这篇博客介绍了斗地主游戏的代码实现,包括拿牌、洗牌、留三张底牌、发牌和看牌的过程。同时,还讲解了一个简单的音乐管理系统,涉及到数据库连接、SQL查询以及音乐类的设计。最后提到了静态方法与非静态方法的调用规则。
摘要由CSDN通过智能技术生成

今天在斗地主原有的代码基础上添加了排序的部分,具体代码如下:
import java.awt.;
import java.awt.List;
import java.util.
;

public class DiZhu2 {
public static void main(String[] args){
//1.拿牌
Map<Integer,String> poker=new HashMap<>();
String[] nums={“A”,“2”,“3”,“4”,“5”,“6”,“7”,“8”,“9”,“10”,“J”,“Q”,“K”};
String[] colors={“黑桃”,“红心”,“梅花”,“方块”};
int index=0;
for(String color:colors){
for(String num:nums){
String pai=color+num;
index=index+1;
poker.put(index,pai);
}
}
System.out.println(index);
index++;
poker.put(index,“大王”);
index++;
poker.put(index,“小王”);
System.out.println(poker);
//2.洗牌
List pokerIndexs=new ArrayList<>();
Set integers = poker.keySet();
// System.out.println(integers);
for(Integer i:poker.keySet()){
pokerIndexs.add(i);
}
Collections.shuffle(pokerIndexs);
System.out.println(pokerIndexs);
//3.留三张牌
Set dipaiInds=new TreeSet<>();
dipaiInds.add(pokerIndexs.remove(0));
dipaiInds.add(pokerIndexs.remove(0));
dipaiInds.add(pokerIndexs.remove(0));
System.out.println(dipaiInds);
System.out.println(pokerIndexs);
//4.发牌
Set ct=new TreeSet<>();
Set wwc=new TreeSet<>();
Set sj=new TreeSet<>();
for(int i=0;i <pokerIndexs.size();i++){
int pi=pokerIndexs.get(i);
int mod=i%3;
if(mod0){
ct.add(pi);
}else if(mod
1){
wwc.add(pi);
}else{
sj.add(pi);
}
}
System.out.println(ct);
System.out.println(wwc);
System.out.println(sj);
//5.看牌
look(poker,ct);
look(poker,wwc);
look(poker,sj);

}
public static void look(Map<Integer,String> poker,Set<Integer> indexs) {
    List<String> p = new ArrayList<>();
    for (Integer i : indexs) {
        String pai = poker.get(i);
        p.add(pai);
    }
    System.out.println(p);

}

}

今天新讲了一个音乐管理系统,首先在数据库里创表,添加变量
添加新的类命名为Test,添加DBUtil 写好后替换Test中的相应内容。
DBUtil的代码如下
public class DBUtil {
public static Connection getConnection() throws SQLException, ClassNotFoundException{
Class.forName(“com.mysql.jdbc.Driver”);
Connection connection= DriverManager.getConnection(“jdbc:mysql://127.0.0.1:3306/zjgm?characterEncoding=utf-8&user=root&password=123456”);
System.out.println(“操作成功!”);
return connection;
}
public static void closeAll(ResultSet resultSet,Statement statement, Connection connection) throws SQLException {
if (resultSet!=null){
try {
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (connection!=null){
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (statement!=null){
try {
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
在创建一个命名为Music的文件,代码如下:
public class Music {
private int id;
private String name;
private String author;

    public int getId(int anInt) {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName(String string) {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;

}

改好的Test代码为
public class Test {
public List findMusic() throws SQLException {
ResultSet resultSet=null;
PreparedStatement statement=null;
Connection connection=null;
List musics=new ArrayList<>();
try {
connection= DBUtil.getConnection();
String sql=“select * from music”;
statement=connection.prepareStatement(sql);
resultSet=statement.executeQuery();
while(resultSet.next()){
Music music=new Music();
music.setId(resultSet.getInt(1));
music.setName(resultSet.getString(2));
music.setAuthor(resultSet.getString(3));
musics.add(music);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
DBUtil.closeAll(resultSet,statement,connection);
}
return musics;

}
public static void main(String[] args) throws SQLException {
    Test test=new Test();
    List<Music> musics=test.findMusic();
    System.out.println(musics);

}

}
今天还讲了一个知识点
静态方法不可以调用非静态方法

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值