一、java斗地主
代码:
package text.zhongruan.text;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
public class dapai{
public static void main(String[] args){
List<String>pokers=new ArrayList<>();
String[] colors=new String[]{"黑桃","红桃","方块","梅花"};
String[] numbers=new String[]{"3","4","5","6","7","8","9","10","J","B","K","A","2"};
for(String color:colors){
for(String number:numbers){
String poker=number+color;
pokers.add(poker);
}
}
pokers.add("大王");
pokers.add("小王");
System.out.println(pokers);
Collections.shuffle(pokers);
System.out.println(pokers);
List<String>dipai=new ArrayList<>();
Random random=new Random();
for (int i=0;i<3;i++){
int Index=random.nextInt(pokers.size()-1);
String pai=pokers.remove(Index);
dipai.add(pai);
}
System.out.println("底牌"+dipai);
List<String>a=new ArrayList<>();
List<String>b=new ArrayList<>();
List<String>c=new ArrayList<>();
for(int i=0;i<pokers.size();i++){
int mod=i%3;
String pai=pokers.get(i);
if(mod==0){
a.add(pai);
}
else if(mod==1){
b.add(pai);
}
else{
c.add(pai);
}
}
System.out.println("a: "+a);
System.out.println("b: "+b);
System.out.println("c: "+c);
int dizhu=random.nextInt(2);
if(dizhu==0){
a.addAll(dipai);
}
else if(dizhu==1){
b.addAll(dipai);
}
else{
c.addAll(dipai);
}
System.out.println("a: "+a);
System.out.println("b: "+b);
System.out.println("c: "+c);
Collections.sort(a);
Collections.sort(b);
Collections.sort(c);
System.out.println("a: "+a);
System.out.println("b: "+b);
System.out.println("c: "+c);
}
}
}
![在这里插入图片描述](https://img-blog.csdnimg.cn/2019080910293614.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2hhaWd1bzZoYW8=,size_16,color_FFFFFF,t_70)
二、jdbc
代码:
package text.zhongruan.text;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class TestJdb {
public static void main(String[] args) throws Exception{
//1.加载驱动
Class.forName("com.mysql.jdbc.Driver");
//2.创建链接
Connection connection= DriverManager.getConnection
("jdbc:mysql://127.0.0.1:3306/nbcj?useSSL=true&characterEncoding=utf-8&user=root&password=123456"
);
System.out.println("链接成功");
//3.写SQL语句
String sql="select * from studer";
//4.得到statement对象
PreparedStatement preparedStatement = connection.prepareStatement(sql);
//5.执行sql得到结果集
ResultSet resultSet=preparedStatement.executeQuery();
//6.处理结果集
while(resultSet.next()){
System.out.println("序号:"+resultSet.getInt(
1));
System.out.println("姓名:"+resultSet.getString(2));
System.out.println("密码:"+resultSet.getString(3));
}
//7.关闭资源
resultSet.close();
preparedStatement.close();
connection.close();
}
}
三、new的对象调用
A a=new A();
A a=new B();
new可以随时新建,而且可以不定长度。
四、static的调用方法
static方法就是没有this的方法。在static方法内部不能调用非静态方法,反过来是可以的。而且可以在没有创建任何对象的前提下,仅仅通过类本身来调用static方法。这实际上正是static方法的主要用途。
可以直接通过它的类名进行调用。