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

该博客介绍了如何使用Java、JSP和SQL Server创建一个网页版的四则运算系统。首先在数据库中创建了一个用于存储计算信息的表,然后通过Java代码实现了数据库连接、增删改查操作以及随机生成计算题目的功能。在JSP页面中,用户可以输入题目数量,系统将自动生成相应题目并存储,之后用户在列表中看到题目并作答,最后系统会展示答题正确数量和清空数据库的选项。
摘要由CSDN通过智能技术生成

设计思路:

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

}

%>

继续答题

运行结果:

6589af642096b8aea7f933edbf86ede5.png

85b2a89923432e7542b3582e13b46f6d.png

7fadf78342d527480f84fe4d5de6e756.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值