软件工程概论 - 个人作业03-网页版四则运算
设计思想:
1 输入算数的题目数
2显示算式(用类封装产生算式和结果的功能)(写入数据库)
3使用者填写答案
4接收答案,与数据库中的答案比较
5返回结果
源代码:
package pers.sun.operateion;
//产生一个算式,及相应的结果
public class Operated {
private int resultx;
private String formulax;
public int getResultx() {
return resultx;
}
public void setResultx(int resultx) {
this.resultx = resultx;
}
public String getFormulax() {
return formulax;
}
public void setFormulax(String formula) {
this.formulax=formula;
}
public String calculation() {
int first=(int) (Math.random()*10+1);
int second=(int) (Math.random()*10+1);
int op=(int) (Math.random()*4+1);
char operator = 0;
switch(op) {
case 1:operator='+';resultx=first+second;break;
case 2:operator='-';resultx=first-second;break;
case 3:operator='*';resultx=first*second;break;
case 4:operator='/';break;
}
//是除
if(op==4) {
//分母不为0 且能除尽
if(second!=0) {
int res=first%second;
if(res==0) {
formulax=first+" "+operator+" "+second+" =";
resultx=first/second;
}
else
formulax=null;
}
else
formulax=null;
}
//不是除
else {
formulax=first+" "+operator+" "+second+" =";
}
return formulax;
}
}
package pers.sun.operateion;
import pers.sun.operateion.Operated;
//产生N个算式,及结果
public class ApplyIt {
private int[] result;
public String[] make(int n) {
//接收的容器
String formulas[]=new String[n];
result=new int[n];
//产生算式
Operated opera=new Operated();
for(int i=0;i<n;) {
String temp=opera.calculation();
//判断算式是否为null
if(temp!=null) {
formulas[i]=temp;
result[i]=opera.getResultx();
i++;
}
}
return formulas;
}
public int[] getResult() {
return result;
}
public void setResult(int[] result) {
this.result = result;
}
}
package pers.sun.sql;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class DB {
public static Connection getConnection() {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String root="root";
String password="sunyu";
String url="jdbc:mysql://localhost:3306/user_message";
Connection con=null;
try {
con=DriverManager.getConnection(url,root,password);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return con;
}
public static void close(Connection con) {
try {
if(con!=null)
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void close(PreparedStatement pre) {
try {
if(pre!=null)
pre.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void close(ResultSet result) {
try {
if(result!=null)
result.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
package pers.sun.sql;
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 pers.sun.operateion.Operated;
public class SqlTool {
public static void add(String formula,int result) {
if(formula!=null) {
String sql="insert into math(formula,result) value(?,?)";
Connection connection=DB.getConnection();
PreparedStatement preparedstatement=null;
try {
preparedstatement=connection.prepareStatement(sql);
preparedstatement.setString(1,formula);
preparedstatement.setInt(2,result);
preparedstatement.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
DB.close(preparedstatement);
DB.close(connection);
}
}
}
public static void deleted() {
Connection connection=DB.getConnection();
String sql="delete from math";
PreparedStatement pre=null;
try {
pre=connection.prepareStatement(sql);
pre.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static List<Operated> search() {
Connection connection=DB.getConnection();
String sql="select * from math";
PreparedStatement pre=null;
ResultSet result=null;
List<Operated> op= new ArrayList<Operated>();
try {
pre = connection.prepareStatement(sql);
result=pre.executeQuery();
while(result.next()) {
Operated temp=new Operated();
temp.setResultx(result.getInt("result"));
temp.setFormulax(result.getString("formula"));
op.add(temp);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return op;
}
public static void updata() {
}
}
<%@ page language="java" contentType="text/html; UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>计算准备</title>
<style type="text/css">
body{
font-family:"楷体" ;
font-size:"10";
}
</style>
</head>
<body background="D:\User\sunyu\Pictures\背景\人物1.jpg" >
<h1>小学生算式</h1>
<hr>
<%
request.setCharacterEncoding("utf-8");
String errorX=request.getParameter("errorX");
if(errorX!=null){
%>
<h4 style="color:red">你要输入一个整数</h4>
<%
}
%>
<form action="outputbegin.jsp" method="post">
<table align="left" border="1" width="500 " height="50">
<tr>
<td>输入你要做的题目数</td>
<td><input type="text" name="number" /></td>
<td><input type="submit" value="确定" name="submit" /></td>
<td><input type="reset" value="重置" name="reset" /></td>
</tr>
<tr>
</tr>
</table>
</form>
</body>
</html>
<%@page import="pers.sun.sql.SqlTool"%>
<%@page import="pers.sun.operateion.Operated"%>
<%@page import="java.util.*"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>判断结果</title>
</head>
<style>
body{
font-family:"楷体";
}
</style>
<body background="D:\User\sunyu\Pictures\背景\人物1.jpg">
<h1 style="color:black">你的战绩如何?</h1>
<ul>
<li><a href="begin.jsp" style="color:yellow">重来</a> </li>
<li><a href="ending.jsp" style="color:yellow">结束</a></li>
</ul>
<hr>
<table align="left" border="1" width="500" style="color:black">
<tr>
<td>算式</td>
<td>你的答案</td>
<td>对错</td>
<td>正确答案</td>
</tr>
<%
int count=0;
List<Operated> operation=new ArrayList<Operated>();
operation=SqlTool.search();
int length=operation.size();
String[] peoresult1=new String[length];
peoresult1=request.getParameterValues("i");
int i=0;
for(Operated temp:operation){
%>
<tr>
<td><%=temp.getFormulax() %></td>
<td><%=peoresult1[i] %></td>
<%
String correct=null;
if(peoresult1[i]!=null&&!"".equals(peoresult1[i].trim())){
if(Integer.parseInt(peoresult1[i])==temp.getResultx()){
correct="对";
count++;
}
else
correct="错";
}
%>
<td><%=correct %></td>
<td><%=temp.getResultx() %></td>
</tr>
<%
i++;
}
%>
<tr><td>你的得分:<%=count %></td></tr>
</table>
<%
SqlTool.deleted();
%>
</body>
</html>
<%@ page language="java" contentType="text/html; UTF-8"
pageEncoding="UTF-8"%>
<%@page import="pers.sun.operateion.*" %>
<%@page import="java.util.*" %>
<%@page import="pers.sun.sql.SqlTool"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>算式表格</title>
</head>
<body background="D:\User\sunyu\Pictures\背景\人物1.jpg" text="black">
<h2>Start your performance and brainstorming</h2>
<hr>
<%
String infor=request.getParameter("number");
//接收信息
if(infor==null||"".equals(infor.trim())){
request.setCharacterEncoding("utf-8");
%>
<jsp:forward page="begin.jsp">
<jsp:param value="你要输入一个整数" name="errorX"/>
</jsp:forward>
<%
//response.sendRedirect("begin.jsp");
}
int num=Integer.parseInt(infor);
ApplyIt apply =new ApplyIt();
//生成算式+结果
String[] suanshi=apply.make(num);
int rightresults[]=apply.getResult();
//写入数据库
for(int i=0;i<num;i++){
SqlTool.add(suanshi[i], rightresults[i]);
}
%>
<form action="handle.jsp" method="post">
<table align="left" border="1" width="500">
<tr>
<td>序号</td>
<td width="200">算式</td>
<td>结果</td>
</tr>
<%
int i=0;
for(String stemp:suanshi){
%>
<tr>
<td><%=i %></td>
<td><%=stemp %></td>
<td><input type="text" value="" name=i /></td>
</tr>
<%
i++;
}
%>
<tr>
<td colspan="3"><input type="submit" value="提交" name="submit" /></td>
</tr>
</table>
</form>
</body>
</html>
结果截图: