Choose one of the three java projects (random number game, arithmetic operation test) Java/GUI/MySQL

Require: 

Topic selection: GUI design is used for random numbers, and MySQL is used for arithmetic operations

topic one

random number game

Topic requirements

Basic requirements for functions: the computer generates random numbers, if you guess correctly, you win, if you fail to guess, you will be prompted whether it is too big or too small, keep guessing until you guess it, and give the time spent and comments. Keep the user test results and make a ranking list. Leaderboards are stored in files or databases.

Ability requirements: ①communication and expression ability②data collection ability③autonomous learning ability④time planning ability⑤document writing ability⑥object-oriented programming thinking, software engineering thinking

Use Java knowledge

1. Input and output

2. Random function

3. Cycle Again

4. Judgment choice (if)

5. Operators (++/--/</>)

6. Classes and Objects

7. Class methods

8. Construction method

9. Interface, inheritance

10. GUI Graphical User Interface

Topic 2

Arithmetic Operations Test

Topic requirements

Basic function requirements: realize ten math problems of addition and subtraction within 100, can calculate the answer according to the problem, compare it with the input answer, judge whether the problem is correct, and finally calculate the score. (Add leaderboard function to store in file or database).

Ability requirements: ①communication and expression ability②data collection ability③autonomous learning ability④time planning ability⑤document writing ability⑥object-oriented programming thinking, software engineering thinking

Use Java knowledge

1. Input and output

2. Random function

3. Loop (while)

4. Judgment choice (if)

5. Operators +, -

6. JDBC database

1. Guess the number game:

package xiangmu11;
import java.awt.BorderLayout;//导入对等体的包(调用事件)
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;//导入组件的包(使用组件)
import javax.swing.JFrame;//顶层容器
import javax.swing.JLabel;
import javax.swing.JPanel;//常用,需放在其他容器上才能用
import javax.swing.JTextField;
//这是一个实现猜数字小游戏的窗口
public class GameFrame extends JFrame{//顶层容器
    GameManager gameManager = new GameManager();
    JLabel label1;//按钮请输入一个100以内的整数
    JTextField txt1;//按钮欢迎来到猜数字小游戏
    JButton button1;//按钮“猜 ”
    JButton button2;//按钮“重新来过”
    JButton button3;//按钮“退出游戏”
    JButton button4;//按钮“猜的次数”
    //构造方法
    public GameFrame() {
        //程序运行前先产生一个随机数
        gameManager.Again();//调用GameManager里的gameManager
        //设置Container容器
        Container con = getContentPane();
        //设置边界布局
        con.setLayout(new BorderLayout());
        //上
        JPanel jp1 = new JPanel();
        jp1.setLayout(new GridLayout(1, 1));//设置布局(网格)
        txt1 = new JTextField();
        jp1.add(txt1);
        //中
        JPanel jp2 = new JPanel();
        jp2.setLayout(new BorderLayout());//边境
        label1 = new JLabel("请输入一个100以内的整数");
        jp2.add(label1, BorderLayout.CENTER);//添加监听
        //下
        JPanel jp3=new JPanel();
        jp3.setLayout(new GridLayout(1, 4));//设置布局
        button1=new JButton("猜");       
        //添加监听
        button1.addActionListener(new Monitor1(txt1, label1));
        button2=new JButton("重新来过");
        //添加监听
        button2.addActionListener(new Monitor2(txt1, label1));
        button3=new JButton("退出游戏");
        //添加监听
        button3.addActionListener(new Monitor3());
        button4=new JButton("猜的次数");       
        //添加监听
        button4.addActionListener(new Monitor4(txt1, label1));
        jp3.add(button1);
        jp3.add(button2);
        jp3.add(button3);
        jp3.add(button4);
        con.add(jp1, BorderLayout.NORTH);//上
        con.add(jp2, BorderLayout.CENTER);//中
        con.add(jp3, BorderLayout.SOUTH);//下
         //回车监听事件  
         //键盘点击回车,实现和点击“猜”按钮一样的功能    
        txt1.addActionListener(new Monitor1(txt1, label1));
        //窗体设置
        setTitle("欢迎来到猜数字小游戏");// 设置窗口的标题
        setSize(350, 250);// 设置大小
        setLocation(750, 400);// 设置窗口出现的位置
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        txt1.requestFocus();//txt1获取光标
        setVisible(true);//可视化设置
    }
    //监听按钮一:猜
    class Monitor1 implements ActionListener {
        JTextField num = new JTextField();
        JLabel label = new JLabel();
        //构造函数
        Monitor1(JTextField num, JLabel label) {
            this.num = num;
            this.label = label;
        }
        @Override
        public void actionPerformed(ActionEvent e) {
            int number = Integer.parseInt(num.getText());
            int flag;
            int time = 0;
            flag = gameManager.Guess(number);
            if (flag == 1) {
                label.setText(number + "\t这个数太大了");
                time++;
            } else if (flag == -1) {
                label.setText(number + "\t这个数太小了");
                time++;
            } else if (flag == 0) {
                label.setText("恭喜你,猜对了!");
                     }
            num.setText("");
            num.requestFocus();
        }
    }

    /*
     * 监听按钮二:重新开始
     */
    class Monitor2 implements ActionListener {
        JTextField num = new JTextField();
        JLabel label = new JLabel();
        // 构造函数
        Monitor2(JTextField num, JLabel label) {
            this.num = num;
            this.label = label;
        }
        @Override
        public void actionPerformed(ActionEvent e) {
            gameManager.Again();
            int time = 0;
            label.setText("请再次输入一个100以内的整数!");
            num.setText("");
        }
    }

    //监听按钮三:退出游戏
    class Monitor3 implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            System.exit(0);
        }
    }
    //监听按钮四:猜的次数
    class Monitor4 implements ActionListener {
    	JTextField time = new JTextField();
        JLabel label = new JLabel();  
        Monitor4(JTextField time, JLabel label) {
            this.time = time;
            this.label = label;
        }
		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub
			label.setText("您一共猜了6次!");	
		}
    }
    
    //主函数
    public static void main(String[] args) {
        GameFrame Frame = new GameFrame();
    }
}
package xiangmu11;
import java.util.Random;
//一个产生随机数和比较猜的对不对的函数
public class GameManager{
    private int number=0;// 随机数

    public int getNumber(){
        return number;
    }
    public void setNumber(int number){
        this.number=number;
    }
    
    //number玩家猜的数
    //return大了返回1,小了返回-1,相等返回0
    public int Guess(int number){
        if(number>this.number){
            //System.out.println("大了");
            return 1;
        }else if(number<this.number){
            //System.out.println("小了");
            return -1;
        }else{
            //System.out.println("相等");
            return 0;
        }
    }

    //再来一次
    public void Again(){
        Random random = new Random();
        //产生一个1-100的一个随机数
        int num=random.nextInt(99)+1;
        setNumber(num);//得到猜的随机数
        System.out.print("随机数字为:"+num);//显示,在一定程度上节省时间
    }
}

operation result: 

2. Arithmetic operation test 

package xiangmu11;
//算术运算测试
import java.util.Scanner;
public class calculater{
	public static void main(String[] args){
		Shujuku s=new Shujuku();
		Scanner input=new Scanner(System.in);
		System.out.println("欢迎进入算数游戏测试");
		System.out.println("游戏规则:");
		System.out.println("计算下列10道算术题,每题十分,共计一百分,答对得分答错不扣分");
		System.out.println("**************加油*****************");
		int a=0,b=0,c;
		String op="+";
		int score=01;
		int count=0;//用来计算用户得出的成绩
		int count1=0;//用来计算用户得出的成绩1
		boolean again=true;//大循环
		String choose;//是否选择继续游戏
		int sum=0;//记录用户获得的总成绩
		c=0;//接收a+b的和c
		String id;//用户名便于存储
		int times=0;//计算用户第几次游戏
		System.out.println("请您输入您的用户名:");
		id=input.next();
		if(id.length()>4){//四位及以内
			System.out.println("输入用户名有误,请重新输入!");	
			id=input.next();
		}else{
			System.out.println("游戏开始!");
		}
		//随机产生10道题
		while(true){
		System.out.println();
		sum=0;
		for(int i=1;i<=10;i++){
			//产生100以内随机数
			a=(int)(Math.random()*100);
			b=(int)(Math.random()*100);
			int x=(int)(Math.random()*10);//随机产生加减法
			if(x%2==0){
			System.out.println("第"+i+"题"+":"+a+"+"+b+"=?");
			System.out.print("第"+i+"题答案是:");
			int result=input.nextInt();
			c=a+b;
			System.out.print("正确答案是:"+c);
			if(result==c){
				count+=10;
				System.out.println("恭喜答对了!");				 
			}else{
				System.out.println("很遗憾答错了,继续努力!");
			}
		}
		else{
			if(a<b){//避免减法中出现负数(交换位置)
				int K;
				K=a;
				a=b;
				b=K;
			}
			 System.out.println("第"+i+"题"+":"+a+"-"+b+"=?");
		  	 System.out.print("第"+i+"题答案是:");
			int result=input.nextInt();
			c=a-b;
			System.out.print("正确答案是:"+c);
			if(result==c){
				count1+=10;
				System.out.println("恭喜答对了!");
			}else{
				System.out.println(" 很遗憾答错了,继续努力!");
			}
		}
		}
		sum=count+count1;//累加和
		
		s.inserts(a,b,op,c,score);
		System.out.println("******************************");
		System.out.println("游戏完成,您的总成绩是:"+sum);
		if(sum>80&&sum<100){
			System.out.println("哇哦,太棒了!");
		}else if(sum>60&& sum<=80) {
			System.out.println("超过了大部分玩家!");
		}else{
			System.out.println("成绩不及格,继续努力!");
		}
		
		System.out.println("退出游戏输入 0,继续参加输入0以外任意值");
		  choose=input.next();
		  
		    if(choose.equals("0")){
		    	System.exit(1);
		    	System.out.println("回见");
		    	}else{
		           System.out.println("继续游戏,开始答题!");
		           System.out.println("重复游戏规则:");
		           System.out.println("计算下列10道算术题,每题十分,共计一百分,答对得分答错不扣分");
		   			System.out.println("*************加油!***************");
		    	}
		}
	}
}
package xiangmu11;
//将其存储于数据库内
//包括用户的用户名和其分数
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Shujuku{
	//数据连接
	Connection conn=null;
	//创建预处理对象
	PreparedStatement ps=null;
	ResultSet rs=null;
	Statement statement=null;
	public Connection getConnection(){
	try{
		// 加载驱动
		Class.forName("com.mysql.jdbc.Driver");
		}catch(ClassNotFoundException e){
		e.printStackTrace();
		}
		try{
		// 获取连接
		conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/user", "root","root");
		}catch (SQLException e){
		e.printStackTrace();
		}
		return conn;
		}
		//释放资源
	public void closeAll(){
		if (rs!=null){
			try{
				rs.close();
			} catch (SQLException e){
				e.printStackTrace();
			}
		}
		if(ps!=null){
			try{
				ps.close();
			}catch(SQLException e){
				e.printStackTrace();
			}
		}
		if (conn!=null) {
			try{
				conn.close();
			}catch (SQLException e){
				e.printStackTrace();
			}
		}
	}
		//插入分数的方法
	public int inserts(int a,int b,String op,int sum,int score){
		int result = 0;
			// 获取链接
			Connection conn=getConnection();
			//向数据库插入分数的语句
		String sql=" insert into user(op1,op2,op,sum,score) values(?,?,?,?,?)";
			try{
			//创建预处理对象
			ps=conn.prepareStatement(sql);	
			
			ps.setInt(1, a);
			ps.setInt(2, b);
			ps.setString(3, op);
			ps.setInt(4, sum);
			ps.setInt(5, score);
			result=ps.executeUpdate();
			}catch (SQLException e){
			e.printStackTrace();
			}finally{
		closeAll();
			}
		return result;
		}
		public void select(){
			System.out.println("用户的分数记录:");
			// 获取链接
				Connection conn=getConnection();
		//向数据库查找分数等
		String sql=" select *from score where id!=0 order by score";
			try{
			ps= conn.prepareStatement(sql);
			//执行给定的SQL语句,返回一个 ResultSet对象
			 rs =ps.executeQuery(sql);
			 while(rs.next()){
			 
			 int id=rs.getInt("id");//"op2"			
			 int op1=rs.getInt("op1");
			 int op2=rs.getInt("op2");
			 String op=rs.getString("op");//2
			 int sum=rs.getInt("sum");
			 int score=rs.getInt("score");
		     System.out.print("用户名:"+id+"运算符:"+op+"操作数1:"+op1+"操作数2:"+op2+"和:"+sum+"成绩:"+score);
			 }
			}catch (SQLException e1){
			e1.printStackTrace();
			}finally{
			closeAll();
				}
			}
	}	

 operation result:

 Use the database table:

Guess you like

Origin blog.csdn.net/m0_55651139/article/details/124456617