1. 程式人生 > >軟件工程個人作業03-網頁版四則運算

軟件工程個人作業03-網頁版四則運算

ktr delete dex earch encoding ava cte nts idt

設計思想:

  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>&nbsp;&nbsp;</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>

結果截圖:

技術分享圖片

技術分享圖片

技術分享圖片

技術分享圖片

軟件工程個人作業03-網頁版四則運算