本文主要是介绍斗地主修改版和music增删改查,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
今天我们在上次的斗地主上做了一点修改
代码如下:
package com.didi;
import java.util.*;
public class DouDiZhu2 {
public static void main(String[] args){
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);
List<Integer> pokerIndex=new ArrayList<>();Set<Integer> integers =poker.keySet();for (Integer i:poker.keySet()){pokerIndex.add(i);}Collections.shuffle(pokerIndex);System.out.println(pokerIndex);Set<Integer> dipaiInds=new TreeSet<>();dipaiInds.add(pokerIndex.remove(0));dipaiInds.add(pokerIndex.remove(0));dipaiInds.add(pokerIndex.remove(0));System.out.println(pokerIndex);Set<Integer> wyb=new TreeSet<>();Set<Integer> xz=new TreeSet<>();Set<Integer> yyk=new TreeSet<>();for (int i=0;i< pokerIndex.size();i++){int pi=pokerIndex.get(i);int mod=i%3;if(mod==0){wyb.add(pi);}else if(mod==1){xz.add(pi);}else{yyk.add(pi);}}System.out.println(wyb);System.out.println(xz);System.out.println(yyk);}
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);
}
}
结果如下:
Music增删改查
先创建三个java文件,Test、Music、DBUtil
在Text中输入代码:
package com.didi;
import com.didi.model.Music;
import com.didi.util.DBUtil;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
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);
}}
DBUtil中输入:
package com.didi.util;
import java.sql.*;
import static java.lang.Class.forName;
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中输入代码:
package com.didi.model;
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;
}@Override
public String toString() {return "Music{" +"id=" + id +", name='" + name + '\'' +", author='" + author + '\'' +'}';
}
}
输入这些代码就ok啦
今天还学了一点理论知识
静态的方法 不用new对象 可以直接通过类名调用 main() 类BL
非静态方法 不可以直接通过类名调用 getAA()
如果 静态的方法 调用 非静态方法
BL.main()
BL. getAA()* 错
证明:静态方法不可以调用非静态方法
假设:静态方法可以调用非静态方法
类 BL
静态方法main()-----》getAA()
非静态方法getAA()
BL.main() —>getAA()
BL. getAA() 错
这篇关于斗地主修改版和music增删改查的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!