java+jsp+sql server实现网页版四则运算.

设计思路:

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<User> 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<User> load() {
	Connection connection = DBUtil.getConnection();
	//准备sql语句
	String sql = "select * from t_result";
	//创建语句传输对象
	PreparedStatement preparedStatement = null;
	ResultSet resultSet = null;
	//集合中只能放入user对象
	List<User> users = new ArrayList<User>();
	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的输入页面:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<%@ page language="java" contentType="text/html; charset=UTf-8"
    pageEncoding="UTf-8"%>
<meta http-equiv="Content-Type" content="text/html; charset=UTf-8">
<title>选择界面</title>
<style type="text/css">
<!--
.STYLE2 {color: #0000FF}
.STYLE3 {color: #FFFF00}
-->
</style>
</head>
<body 	background=C:\Users\111\Pictures\22.jpg>
<form action="select.jsp" >
   <table width="400" height="189" border="1" align="center">
    <tr align="center" > </tr>
    <tr align="center">
      <td align="left" bgcolor="#CCE8CF"><span class="STYLE2">请输入出题数</span></td>
      <td><input type="text" name="num2"/></td>
    </tr>
    <tr align="center">
      <td align="left"><span class="STYLE3">请输入答题时间</span></td>
      <td><input type="text" name="num"/>
      </td>
    </tr>
    <tr align="center">
      <td colspan="2"><input name="submit" type="submit" value="确定" />
      </td>
    </tr>
  </table>
</form>
</body>
</html>

  题目生成:

<%@page import="com.gao.msg.faction.yunsuan"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>

<%
   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");
%>

</html>

  题目列表:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%@page import="java.util.List"%>
<%@page import="com.gao.msg.Dao.UserDaoImpl"%>
<%@page import="com.gao.msg.model.User"%>
<html>
<head>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>习题</title>
<style type="text/css">
<!--
.STYLE3 {
	font-family: "宋体";
	font-size: 24px;
}
body {
	background:C:\Users\111\Pictures\333.jpg;
}
-->
</style>
</head>

<%
UserDaoImpl userDao=new UserDaoImpl();
List<User> users = userDao.load();
%>
<body 	background="C:\Users\111\Pictures\333.jpg">
<form action="judge.jsp">
<table align="left" border="1" width="464">
<tr>
<td width="216" height="70" bgcolor="#FFCC33">题目</td>
<td width="232" bgcolor="#CC6699">请输入结果</td>
</tr>
<%            int ni=0;
			for( User user : users ){
%>
<tr>
<td height="96" bgcolor="#00FF00"> <span class="STYLE3"><%=user.getFirstnumber()%>
    <%=user.getOperator()%>
    <%=user.getSecondnumber()%>
    <%if(user.getThirdnumber()!=0){%>
    <%=user.getOperator()%>
    <%=user.getThirdnumber() %>
    <%} %>
=</span></td>
<td bgcolor="#0066FF">
<%  ni=ni+1; %>
 <input type="text" name="<%=ni%>"/>
</td>
</tr>
<%
			}
%>
<tr align="center">
    			<td height="91" colspan="2" bgcolor="#FF0000">
    				<input type="submit" value="确定提交" />
	  </td>
 </tr>
</table>
、
</form>
</body>
</html>

  结果输出:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%@page import="org.apache.coyote.http11.NpnHandler"%>
<%@page import="java.util.List"%>
<%@page import="com.gao.msg.Dao.UserDaoImpl"%>
<%@page import="com.gao.msg.model.User"%>
<html>
<head>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>测试结果</title>
<style type="text/css">
.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;}
</style>
</head>
<body background=C:\Users\111\Pictures\777.jpg>
<%
UserDaoImpl UserDao=new UserDaoImpl();
List<User> 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);
  }
  }
%>
<h1 align="center">总题数:<%=m %>题</h1>

<h1 align="center">答对:<%=stm %>题</h1>

 

<%
for(User user : users )
{
int id=user.getId();
UserDao.delete(id);
}
%>
<blockquote>
  <blockquote>
    <blockquote>
      <blockquote>
        <blockquote>
          <blockquote>
            <blockquote>
              <blockquote>
 <a href="InputTitle.jsp" >继续答题</a>
             </blockquote>
            </blockquote>
          </blockquote>
        </blockquote>
      </blockquote>
    </blockquote>
  </blockquote>
</blockquote>
</body>
</html>

  运行结果:

 

转载于:https://www.cnblogs.com/miria-486/p/7995354.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值