加减法和计算器

选题一

算术运算测试

题目要求

实现十道100以内加减法数学题,能根据题目计算出答案,与输入答案对比,判断做题是否正确,最后计算分数。(添加排行榜功能存放到文件或数据库中)

使用Java知识

Java基本输入输入出、运算符、循环、选择分支;GUI、JDBC可选

选题二

简易计算器模拟程序

题目要求

仿照Windows计算器,编写一具有GUI的计算器,能实现整数的加、减、乘、除四则运算。历次计算需存放在文件或数据库中,下回启动可重复使用。功能具体参考windows计算器(查看菜单选择:标准型即可)

使用Java知识

GUI图形用户界面编程(布局、组件、事件处理)、类和对象、封装继承等。JDBC可选

 

 

 

 

2.2 系统需求分析

一、算术运算测试

(1)需求分析:

第一个让它用random生成两个随数,然后运算符用random生成一个个位数,如果这个个位数是奇数就表示加号,是偶数就是减号,然后控制台显示这个计算题,如果答对了给它一个提示,并给他加10分,如果没有答对,也给他一个提示,最后把这十道计算题存到数据表中,把分数存到另一个数据表中。并把分数以生序的方式输出到控台。

二、简易计算器模拟程序

(1)需求分析:

用JFrame  JTextField  JButton,这些东西弄,我特意搜了一下,我做这个计算器,让它有0到9和+ - * / = .这几个键,可以在这个简单的计算器上进行计算

    

3.2 数据库设计

一、算术运算测试

表3-2-1 calculator(存储计算题)

序号

字段名

数据类型

约束

是否为空

说明

1

id

int

主键

计算题条数

2

Num1

int

 

第一个操作数

3

operator

char

 

运算符

4

Num2

int

 

第二个操作数

5

result

int

 

结果

表3-2-2 scoer(存储分数)

序号

字段名

数据类型

约束

是否为空

说明

1

id

int

主键

分数条数

2

score

int

 

分数

 

 

一、算术运算测试(util)

(1)功能实现:

    1.让算机产生随机的计算题

package util;

import java.util.Random;

import java.util.Scanner;

public class Add {

    public void add() {

       Utils util = new Utils();

       Scanner input = new Scanner(System.in);

       Random ran = new Random();

       int score = 0;

       int result = 0;

       int num1;

       int num2;

       int num3;

       String type=null;

       for (int i = 0; i < 10; i++) {

           System.out.println("第" + (i + 1) + "题");

           num1 = ran.nextInt(100);

           num2 = ran.nextInt(100);

           num3 = ran.nextInt(10);

              if (num3 % 2 != 0) {

                  type="+";

                  System.out.println(num1 +type + num2 + "=" + "?");

                  System.out.println("请输入你的答案:");

                  result = input.nextInt();

                  if ((num1 + num2) == result) {

                     System.out.println("恭喜你答对了,可以加10哟!");

                     score += 10;

                     System.out.println("得分为:" + score);

                  } else {

                     System.out.println("很遗憾,你答错了");

                  }

                  util.insert(num1,type,num2,result);

                 

              } else {

                  type="-";

                  System.out.println(num1 + type + num2 + "=" + "?");

                  System.out.println("请输入你的答案:");

                  result = input.nextInt();

                  if ((num1 - num2) == result) {

                     System.out.println("恭喜你答对了,可以加10哟!");

                     score += 10;

                     System.out.println("得分为:" + score);

                  } else {

                     System.out.println("很遗憾,你答错了");

                  }

                  util.insert(num1,type,num2,result);

              }

}

       System.out.println("你的总分为:" + score);

       System.out.println("游戏结束!");

       util.inserts(score);

       util.select();

    }

      

}

2.数据库建立

package util;

 

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.util.Random;

 

public class Utils {

    //与数据库的连接

    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/calculator_size", "root", "ycf");

       } 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 insert(Object... objects) {

       int result = 0;

       // 获取链接

       Connection conn = getConnection();

       //向数据库插入计算题的语句

      String sql = " insert into calculator(num1,operator,num2,result) values(?,?,?,?)";

       try {

           //创建预处理对象

           ps = conn.prepareStatement(sql);

           if (objects != null) {

              for (int i = 0; i < objects.length; i++) {

                  //使用给定对象设置指定参数的值

                  ps.setObject(i + 1, objects[i]);

              }

           }

           result = ps.executeUpdate();

       } catch (SQLException e) {

           e.printStackTrace();

       } finally {

           closeAll();

       }

       return result;

    }

    //插入分数的方法

    public int inserts(Object... objects) {

           int result = 0;

           // 获取链接

           Connection conn = getConnection();

           //向数据库插入分数的语句

           String sql = " insert into score(score) values(?)";

           try {

              //创建预处理对象

              ps = conn.prepareStatement(sql);

              if (objects != null) {

                  for (int i = 0; i < objects.length; i++) {

                     ps.setObject(i + 1, objects[i]);

                  }

              }

              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()) {

                   String id=rs.getString("id");

                   String score=rs.getString("score");

                   System.out.print("编号:"+id);

                   System.out.println("分数:"+score);

               }

           } catch (SQLException e1) {

              e1.printStackTrace();

           }finally {

              closeAll();

           }

          

       }

 

}

二、简易计算器(calculators)

(1)功能实现:

    package calculators;

 

import java.awt.BorderLayout;

import java.awt.Dimension;

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.JPanel;

import javax.swing.JTextField;

//小型计算器

public class Jsq implements ActionListener {//ActionListener接收操作事件的监听

    JTextField tf;//小显示窗口

    JButton bt;//按键

    // 用来存放按键

    String[] btText = { "7", "8", "9", "/", "4", "5", "6", "*", "1", "2", "3", "+", "0", ".", "=", "-", };

    String strA = "";

    String strB = "";

    char operator = '~';

    public void go() {

       // 一个窗口

       JFrame frame = new JFrame("Calcultator");

       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

       frame.setSize(300, 400);

 

       // 计算器输入窗口

       tf = new JTextField();

       // 调节显示窗口的大小

       tf.setPreferredSize(new Dimension(300, 60));

       frame.getContentPane().add(tf, BorderLayout.NORTH);

 

       // 按键

       bt = new JButton();

       // 创建新的 JPanel 双缓冲区与流布局

       JPanel p = new JPanel();

       p.setLayout(new GridLayout(4, 4));

       for (int i = 0; i < 16; i++) {

           JButton bt = new JButton(btText[i]);

           p.add(bt);

           //对按键加处理  监听

           bt.addActionListener(this);

       }

       frame.getContentPane().add(p, BorderLayout.CENTER);

 

       // 显示窗口

       frame.setVisible(true);

    }

    public void actionPerformed(ActionEvent e) {

       String action = e.getActionCommand();

       char act = action.charAt(0);

       if (act >= '0' && act <= '9') {

           if (operator == '~') {

               if(act=='0') {

                  if(strA.length()>0) {

                     strA += action;

                     tf.setText(strA);

                  }

              }else {

                  strA += action;

                  tf.setText(strA);

              }

           } else {

              if(act=='0') {

                  if(strB.length()>0) {

                     strB += action;

                     tf.setText(strB);

                  }

              }else {

                  strB += action;

                  tf.setText(strB);

              }

             

           }

 

       } else if (act == '+' || act == '-' || act == '*' || act == '/') {

           operator = act;

 

       } else if (act == '=') {

           if (operator == '+') {

              int a = Integer.parseInt(strA);

              int b = Integer.parseInt(strB);

              int c = a + b;

              tf.setText(c + "");

           } else if (operator == '-') {

              int a = Integer.parseInt(strA);

              int b = Integer.parseInt(strB);

              int c = a - b;

              tf.setText(c + "");

           } else if (operator == '*') {

              int a = Integer.parseInt(strA);

              int b = Integer.parseInt(strB);

              int c = a * b;

              tf.setText(c + "");

           } else if (operator == '/') {

              int a = Integer.parseInt(strA);

              int b = Integer.parseInt(strB);

              int c = a / b;

              tf.setText(c + "");

           }

           operator = '~';

           strA = "";

           strB = "";

       }

    }

 

    public static void main(String[] args) {

       Jsq jsq = new Jsq();

       jsq.go();

    }

}

 

你可能感兴趣的:(加减法和计算器)