即墨市网站建设,科技守护者,东莞樟木头做网站哪家好,工商工事上哪个网站做|小学生四则运算| 设计思想#xff1a; 1 程序生成数据#xff0c;并存到数据库Mysql; 2用户输入数据 3程序接收数据#xff0c;并判断数据 4返回信息。 源程序代码#xff1a; java文件 package pers.sun.operateion;//产生一个算式#xff0c;及相应的结果public class … |小学生四则运算| 设计思想 1 程序生成数据并存到数据库Mysql; 2用户输入数据 3程序接收数据并判断数据 4返回信息。 源程序代码 java文件 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.formulaxformula; } public String calculation() { int first(int) (Math.random()*101); int second(int) (Math.random()*101); int op(int) (Math.random()*41); char operator 0; switch(op) { case 1:operator;resultxfirstsecond;break; case 2:operator-;resultxfirst-second;break; case 3:operator*;resultxfirst*second;break; case 4:operator/;break; } //是除 if(op4) { //分母不为0 且能除尽 if(second!0) { int resfirst%second; if(res0) { formulaxfirst operator second ; resultxfirst/second; } else formulaxnull; } else formulaxnull; } //不是除 else { formulaxfirst 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]; resultnew int[n]; //产生算式 Operated operanew Operated(); for(int i0;in;) { String tempopera.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 rootroot; String passwordsunyu; String urljdbc:mysql://localhost:3306/user_message; Connection connull; try { conDriverManager.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 sqlinsert into math(formula,result) value(?,?); Connection connectionDB.getConnection(); PreparedStatement preparedstatementnull; try { preparedstatementconnection.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() { } public static ListOperated search() { Connection connectionDB.getConnection(); String sqlselect * from math; PreparedStatement prenull; ResultSet resultnull; ListOperated op new ArrayListOperated(); try { pre connection.prepareStatement(sql); resultpre.executeQuery(); while(result.next()) { Operated tempnew 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() { }} JSP文件 % page languagejava contentTypetext/html; UTF-8 pageEncodingUTF-8%!DOCTYPE html PUBLIC -//W3C//DTD HTML 4.01 Transitional//EN http://www.w3.org/TR/html4/loose.dtdhtmlheadmeta http-equivContent-Type contenttext/html; charsetUTF-8title计算准备/title/headbody form actionoutputbegin.jsp methodpost table aligncenter border1 width100 tr !-- 没有判断输入的是否为整数 或 不为空 -- td输入你要做的题/td /tr tr td input typetext namenumber / /td /tr tr td input typesubmit value确定 namesubmit / input typereset value重置 namereset / /td /tr /table /form/body/html %page importpers.sun.sql.SqlTool%% page languagejava contentTypetext/html; UTF-8 pageEncodingUTF-8%%page importpers.sun.util.* %%page importpers.sun.operateion.* %%page importjava.util.* %!DOCTYPE html PUBLIC -//W3C//DTD HTML 4.01 Transitional//EN http://www.w3.org/TR/html4/loose.dtd!-- 接收用户数字--htmlhead title算式表格/title/headbody% //接收信息 int numInteger.parseInt(request.getParameter(number)); ApplyIt apply new ApplyIt(); //生成算式结果 String[] suanshiapply.make(num); int rightresults[]apply.getResult(); //写入数据库 for(int i0;inum;i){ SqlTool.add(suanshi[i], rightresults[i]); }% form actionhandle.jsp methodpost table aligncenter border1 width500 tr td算式/td td结果/td /tr % for(String stemp:suanshi){ % tr td%stemp %/td tdinput typetext value namepeoresult //td /tr % } % tr tdinput typesubmit value提交 namesubmit //td /tr /table /form /body %page importpers.sun.sql.SqlTool%%page importpers.sun.operateion.Operated%% page languagejava contentTypetext/html; charsetUTF-8 pageEncodingUTF-8%%page importjava.util.*%!DOCTYPE html PUBLIC -//W3C//DTD HTML 4.01 Transitional//EN http://www.w3.org/TR/html4/loose.dtdhtmlheadmeta http-equivContent-Type contenttext/html; charsetUTF-8title判断结果/title/headbody ul li a hrefbegin.jsp 重来/anbsp;nbsp;/li li a hrefending.jsp结束/a/li /ul hr table aligncenter border1 width500 tr td算式/td td你的答案/td td对错/td td正确答案/td /tr% ListOperated operationnew ArrayListOperated(); int lengthoperation.size(); //String[] peoresultnew String[length]; //peoresultrequest.getParameterValues(peoresult); operationSqlTool.search(); int count0; System.out.print(request.getParameter(peoresult)); for(Operated temp:operation){ int i0; String peoresultxrequest.getParameter(peoresult);% tr td%temp.getFormulax() %/td td%peoresultx %/td % String correctnull; if(peoresultx!null!.equals(peoresultx.trim())){ if(Integer.parseInt(peoresultx)temp.getResultx()){ correct对; count; } else correct错; } % td%correct %/td td%temp.getResultx() %/td /tr% }% trtd你的得分%count %/td/tr /table /body/ht 运行结果截图: 此处不正确“你的答案” 出问题。 学习进度loading............转载于:https://www.cnblogs.com/floakss/p/7956173.html