猜数字游戏

猜数字游戏:

原代码:
package logfet;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Label;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
import java.util.StringTokenizer;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.border.EmptyBorder;
@SuppressWarnings(“serial”)
public class TWOE extends JFrame implements ActionListener {

	static Connection con=null;
	private	static String driver = "com.mysql.jdbc.Driver";
	private	static String url = "jdbc:mysql://localhost:3306/twoe";
	private	static String user = "root";
	private	static String password = "LOGFET";
private JPanel contentPane;
JButton btn = new JButton("确定");
JLabel jb = new JLabel("请输入一百以内的整数竞猜");
JTextArea re=new JTextArea();
Label jt = new Label();
Label je = new Label();
Label jw = new Label();
Label ju = new Label();
Label jv = new Label();
Random r = new Random();
int t=r.nextInt(100)+1;
int flag=0;
 static int i=0;
 static int j=0;
public TWOE() {
	setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	setBounds(600, 600,600, 600);
	contentPane = new JPanel();
	contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
	contentPane.setLayout(new BorderLayout(0, 0));
	contentPane.setBackground(Color.pink);
	setContentPane(contentPane);
	this.setTitle("计算器");
	getContentPane().setLayout(null);
	jb.setBounds(150, 50, 300, 60);
	jb.setFont(new Font("楷体",Font.PLAIN,25));
	btn.setBounds(440, 460, 100, 40);
	btn.addActionListener(this);
	re.setBounds(150, 150, 300, 40);
	re.setFont(new Font("宋体",Font.PLAIN,32));
	jt.setBounds(150, 240, 300, 60);
	jt.setFont(new Font("楷体",Font.PLAIN,25));
	je.setBounds(150, 300, 300, 60);
	je.setFont(new Font("楷体",Font.PLAIN,25));
	jw.setBounds(50, 420, 320, 80);
	jw.setFont(new Font("楷体",Font.PLAIN,22));
	ju.setBounds(50, 360, 420, 80);
	ju.setFont(new Font("楷体",Font.PLAIN,28));
	jv.setBounds(10, 30, 120, 40);
	jv.setFont(new Font("楷体",Font.PLAIN,18));
	//jv.setBackground(Color.BLUE);
	getContentPane().add(jb);
	getContentPane().add(jt);
	getContentPane().add(btn);
	getContentPane().add(re, BorderLayout.NORTH);
	getContentPane().add(je);
	getContentPane().add(jw);
	getContentPane().add(ju);
	getContentPane().add(jv);
	//System.out.println(t);
	try {
		Class.forName(driver);
		con = DriverManager.getConnection(url,user,password);
		Statement statement = con.createStatement();
		String sql = "select * from two";
		ResultSet rs = statement.executeQuery(sql);
		String sec = null;
		while(rs.next())			
				sec=rs.getString("sec");
		
		jv.setText("上次用时:"+sec+"s");
	} catch (ClassNotFoundException e) {
		// TODO 自动生成的 catch 块
		e.printStackTrace();
	} catch (SQLException e) {
		// TODO 自动生成的 catch 块
		e.printStackTrace();
	}
	finally {
		try {
			con.close();
		} catch (SQLException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
	}
}
	static Date date=new Date();
	static SimpleDateFormat formater=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	static String now=formater.format(date);
	long time=0;
@Override
public void actionPerformed(ActionEvent e) {
	// TODO 自动生成的方法存根
	 if(e.getSource()==btn&flag!=1){
		 ju.setText("");
		 jv.setText("");
		 je.setText("");
     	jw.setText("");
     	jt.setText("");
            String s=re.getText();
            int y=Integer.parseInt(s);
            i++;
            if(y<t)
            {
            	jt.setText("抱歉!猜数过小,请继续:");
            	re.getText();
            }
            else if(y==t)
            {
            	
            	Date data=new Date();
            	
            			time=data.getTime()-date.getTime()-time; 
        		long we=time/1000;
        		if(we<15)
        			ju.setText("评语:哇哦!你是我的偶像!");
        		else if(we<30)
        			ju.setText("评语:哇哦!不错哦!");
        		else 
        			ju.setText("评语:小朋友,继续努力哦!");
        		String st=String.valueOf(we);
            	re.getText();
            	t=r.nextInt(100)+1;
            	jt.setText("恭喜你,猜对了!");
            	je.setText("共猜了"+i+"次");
            	jw.setText("用时"+st+"s  输入数可继续猜数哦!");
            	j=i;
            	i=0;
            	try {
			Class.forName(driver);
		} catch (ClassNotFoundException w) {
			// TODO 自动生成的 catch 块
			w.printStackTrace();
		}
	try {
		con = DriverManager.getConnection(url,user,password);
	} catch (SQLException w) {
		// TODO 自动生成的 catch 块
		w.printStackTrace();
	}
	String sql ="insert into two values(?,?,?)";
	PreparedStatement p = null;
	try {
		p=con.prepareStatement(sql);
	} catch (SQLException w) {
		// TODO 自动生成的 catch 块
		w.printStackTrace();
	}
	try {
		p.setString(3, st);
	} catch (NumberFormatException e2) {
		// TODO 自动生成的 catch 块
		e2.printStackTrace();
	} catch (SQLException e2) {
		// TODO 自动生成的 catch 块
		e2.printStackTrace();
	}
	try {
		p.setInt(2, j);
	} catch (SQLException e1) {
		// TODO 自动生成的 catch 块
		e1.printStackTrace();
	}
	try {
		p.setString(1, now);
	} catch (SQLException w) {
		// TODO 自动生成的 catch 块
		w.printStackTrace();
	}
	try {
		p.executeUpdate();
	} catch (SQLException w) {
		// TODO 自动生成的 catch 块
		w.printStackTrace();
	}
            }
            else
            {
            	re.getText();
            	this.jt.setText("抱歉!猜数过大,请继续:");
            }
          
           @SuppressWarnings("unused")
			StringTokenizer tokens=new StringTokenizer(s);
		}
	}

public static void main(String[] args) {	
	
	EventQueue.invokeLater(new Runnable() {		
		public void run() {
			TWOE frame = new TWOE();
			frame.setVisible(true);
			} 
	});
}

}

程序截图:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值