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

今天在斗地主原有的代码基础上添加了排序的部分,具体代码如下:
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);

}

今天新讲了一个音乐管理系统,首先在数据库里创表,添加变量
添加新的类命名为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/zjgmharacterEncoding=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;

}

改好的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;

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

声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2019年8月13日
下一篇 2019年8月13日

相关推荐