设计思路:
1需要的工具以及实现这个算法和代码的事先条件。
(1)sql server里面建议一个叫t_result的表)(当然你起什么名字都可以),里面输入所需要的列名。id,firstnumber,secondnumber,thirdnumber,operator,result。
(2)eslisp 导入jdbc包(自己去百度找,谢谢.),连接数据库需要。
(3)导入jsp的包。
2思路:
1.连接数据库,并实现增删改查。
2 设计一个随机生成计算式的函数方法。
3 jsp页面设计4个,第一个界面输入时间以及题目数,第二个界面利用for循环,进行输入次数的随机生成计算式存入数据库,第三个界面进行遍历输入数据库中的所有计算式,在列表里面输出。
代码参上:
1连接数据库(sql server的)
package com.gao.msg.Utli;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class DBUtil {
public static Connection getConnection()
{
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();
} //try
catch (InstantiationException | IllegalAccessException | ClassNotFoundException e)
{
e.printStackTrace();
}//catch
String user="sa";
String password="123456";
String url="jdbc:sqlserver://localhost:1433;DatabaseName=Text";
Connection connection =null;
try {
connection=DriverManager.getConnection(url, user, password);
} //try
catch (SQLException e) {
e.printStackTrace();
}//catch
return connection;
}//connection
public static void close(Connection connection)
{
try {
if(connection!=null)
{
connection.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}//close connection
public static void close(PreparedStatement preparedStatement)
{
try {
if(preparedStatement!=null)
{
preparedStatement.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}//close preparedStatement
public static void close(ResultSet resultSet)
{
try {
if(resultSet!=null)
{
resultSet.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}//class
2 增删改查:
(1)接口类。
package com.gao.msg.Dao;
import java.util.List;
import com.gao.msg.model.User;
public interface IUserDao {
public void add(User user);
public void delete(int id);
public int load(int id);
public List load();
}
2实现类:
package com.gao.msg.Dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.gao.msg.Utli.DBUtil;
import com.gao.msg.model.User;
public class UserDaoImpl implements IUserDao {
@Override
public void add(User user) //添加
{
Connection connection=DBUtil.getConnection();
String sql="insert into t_result (id,firstnumber,secondnumber,thirdnumber,result,operator) values(?,?,?,?,?,?)";
PreparedStatement preparedStatement=null;
try
{
preparedStatement=connection.prepareStatement(sql);
preparedStatement.setInt(1, user.getId());
preparedStatement.setInt(2, user.getFirstnumber());
preparedStatement.setInt(3, user.getSecondnumber());
preparedStatement.setInt(4, user.getThirdnumber());
preparedStatement.setInt(5, user.getResult());
preparedStatement.setString(6, user.getOperator());
preparedStatement.executeUpdate();
}//try
catch (SQLException e)
{
e.printStackTrace();
}//catch
finally
{
DBUtil.close(preparedStatement);
DBUtil.close(connection);
}//finally
}//add
public void delete(int id)//删除
{
Connection connection=DBUtil.getConnection();
String sql="delete from t_result where id=?";
PreparedStatement preparedStatement=null;
try
{
preparedStatement=connection.prepareStatement(sql);
preparedStatement.setInt(1, id);
preparedStatement.executeUpdate();
} //try
catch (SQLException e)
{
e.printStackTrace();
}//catch
finally
{
DBUtil.close(connection);
DBUtil.close(preparedStatement);
}//finally
}//delete
public int load(int id)
{
Connection connection=DBUtil.getConnection();
String sql="select * from t_result where id = ?";
PreparedStatement preparedStatement=null;
ResultSet resultSet=null;
int result=0;
try
{
preparedStatement=connection.prepareStatement(sql);
preparedStatement.setInt(1, id);
resultSet=preparedStatement.executeQuery();
while(resultSet.next())
{
result=resultSet.getInt("result");
}//while
} //try
catch (SQLException e)
{
e.printStackTrace();
}//catch
finally
{
DBUtil.close(connection);
DBUtil.close(preparedStatement);
}//finally
return result;
}//load
public List load() {
Connection connection = DBUtil.getConnection();
//准备sql语句
String sql = "select * from t_result";
//创建语句传输对象
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
//集合中只能放入user对象
List users = new ArrayList();
User user = null;
try {
preparedStatement = connection.prepareStatement(sql);
resultSet = preparedStatement.executeQuery();
while(resultSet.next()) {
user = new User();
user.setId(resultSet.getInt("id"));
user.setFirstnumber(resultSet.getInt("firstnumber"));
user.setSecondnumber(resultSet.getInt("secondnumber"));
user.setThirdnumber(resultSet.getInt("thirdnumber"));
user.setResult(resultSet.getInt("result"));
user.setOperator(resultSet.getString("operator"));
users.add(user);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
DBUtil.close(resultSet);
DBUtil.close(preparedStatement);
DBUtil.close(connection);
}
return users;
}
}//class
生成随机算式的方法:
(1)随机数生成:
package com.gao.msg.faction;
import java.util.Random;
class Romdomnumber {
public int randomunmber(int number)
{
Random random=new Random();
int b=random.nextInt(number);
return b;
}//Randomnumber
}
(2)利用随机数生成算式:
package com.gao.msg.faction;
import com.gao.msg.Dao.UserDaoImpl;
import com.gao.msg.model.User;
public class yunsuan
{
int firstnumber;
int secondnumber;
int thirdnumber;
int operator;
int panduan;
UserDaoImpl userdao=new UserDaoImpl();
public void xfaction(int id)
{
Romdomnumber xromdomnumber=new Romdomnumber();
User user=new User();
operator=xromdomnumber.randomunmber(4)+1;
if(operator==1)
{
user.setId(id);
user.setOperator("+");
while(true){
firstnumber=xromdomnumber.randomunmber(101);
secondnumber=xromdomnumber.randomunmber(101);
panduan=xromdomnumber.randomunmber(2);
if(panduan==0)
{
thirdnumber=xromdomnumber.randomunmber(101);
}//if
else
{
thirdnumber=0;
}//else
if((firstnumber+secondnumber+thirdnumber)<=100)
{
break;
}//if
}//while
user.setFirstnumber(firstnumber);
user.setSecondnumber(secondnumber);
user.setThirdnumber(thirdnumber);
user.setResult(firstnumber+secondnumber+thirdnumber);
}//if(operator==1)
else if(operator==2)
{
user.setId(id);
user.setOperator("-");
while(true){
firstnumber=xromdomnumber.randomunmber(101);
secondnumber=xromdomnumber.randomunmber(101);
panduan=xromdomnumber.randomunmber(2);
if(panduan==0)
{
thirdnumber=xromdomnumber.randomunmber(101);
}//if
else
{
thirdnumber=0;
}//else
if((firstnumber-secondnumber-thirdnumber)>0)
{
break;
}//if
}//while
user.setFirstnumber(firstnumber);
user.setSecondnumber(secondnumber);
user.setThirdnumber(thirdnumber);
user.setResult(firstnumber-secondnumber-thirdnumber);
}//if(operator==2)
else if(operator==3)
{
user.setId(id);
user.setOperator("×");
firstnumber=xromdomnumber.randomunmber(11);
secondnumber=xromdomnumber.randomunmber(11);
while(true)
{
if((firstnumber*secondnumber)<=100)
{
break;
}
}//while
user.setFirstnumber(firstnumber);
user.setSecondnumber(secondnumber);
user.setThirdnumber(0);
user.setResult(firstnumber*secondnumber);
}//if(operator==3)
else
{
user.setId(id);
user.setOperator("÷");
while(true)
{
firstnumber=xromdomnumber.randomunmber(101);
secondnumber=xromdomnumber.randomunmber(10)+1;
if((firstnumber%secondnumber)==0)
{
break;
}//if
}//while
user.setFirstnumber(firstnumber);
user.setSecondnumber(secondnumber);
user.setThirdnumber(0);
user.setResult(firstnumber/secondnumber);
}//if(operator==4)
userdao.add(user);
}//xfaction
}//yunsuan
User类,用来储存各种数据:
package com.gao.msg.model;
public class User {
private int id;
private int firstnumber;
private int secondnumber;
private int thirdnumber;
private String operator;
public String getOperator() {
return operator;
}
public void setOperator(String operator) {
this.operator = operator;
}
public int getFirstnumber() {
return firstnumber;
}
public void setFirstnumber(int firstnumber) {
this.firstnumber = firstnumber;
}
public int getSecondnumber() {
return secondnumber;
}
public void setSecondnumber(int secondnumber) {
this.secondnumber = secondnumber;
}
public int getThirdnumber() {
return thirdnumber;
}
public void setThirdnumber(int thirdnumber) {
this.thirdnumber = thirdnumber;
}
private int result;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getResult() {
return result;
}
public void setResult(int result) {
this.result = result;
}
}
jsp的输入页面:
pageEncoding="UTf-8"%>
选择界面请输入出题数 | |
请输入答题时间 | |
题目生成:
pageEncoding="UTF-8"%>
String number=request.getParameter("number");
int num=Integer.parseInt(request.getParameter("num"));
yunsuan yun=new yunsuan();
int i=0;
for(i=1;i<=num;i++)
{
yun.xfaction(i);
}//for
response.sendRedirect("list.jsp");
%>
题目列表:
pageEncoding="UTF-8"%>
习题UserDaoImpl userDao=new UserDaoImpl();
List users = userDao.load();
%>
题目 | 请输入结果 |
for( User user : users ){
%>
=
}
%>
、
结果输出:
pageEncoding="UTF-8"%>
测试结果.container{font-family:宋体;font-size:18px;font-weight:bold;width:720px;}
H1{font-family:宋体;font-size:44px;color:#B22222;letter-spacing:12px;}
ul{list-style-type:none;margin:0;padding:0;overflow:hidden;}
UserDaoImpl UserDao=new UserDaoImpl();
List users = UserDao.load();
int m=0;int n=0;int stm=0;
for( User user : users ){
m=m+1;
}
%>
for(n=1;n<=m;n++)
{
String x=Integer.toString(n);
int id=-2;
String y=request.getParameter(x);
if(y==null||"".equals(y))
{ id=-1;}
else
{ id = Integer.parseInt(y);}
if(id==UserDao.load(n))
{
stm=stm+1;
System.out.println(stm);
}
}
%>
总题数:题
答对:题
for(User user : users )
{
int id=user.getId();
UserDao.delete(id);
}
%>
运行结果:
】