简单的jsp四则运算
js 正则表达式 四则运算

js 正则表达式四则运算【实用版】目录1.JavaScript 正则表达式的概念和用途2.JavaScript 正则表达式的基本语法3.JavaScript 正则表达式的四则运算4.实例:使用 JavaScript 正则表达式进行四则运算正文一、JavaScript 正则表达式的概念和用途正则表达式(Regular Expression),简称 regex,是一种强大的文本处理工具,主要用于匹配和查找文本字符串中的模式。
在 JavaScript 中,正则表达式被广泛应用于验证表单、过滤数据、提取信息等场景。
二、JavaScript 正则表达式的基本语法1.字符类:用于匹配某一类字符- [abc]:匹配 a、b 或 c- [^abc]:匹配除方括号内字符以外的任意字符- [a-zA-Z]:匹配所有英文字母- d:匹配数字,等价于 [0-9]- D:匹配非数字,等价于 [^0-9]- s:匹配空白字符(空格、制表符、换行符等)- S:匹配非空白字符- w:匹配单词字符(字母、数字、下划线),等价于 [a-zA-Z0-9_] - W:匹配非单词字符2.量词:用于指定字符或字符类出现的次数-?:出现 0 次或 1 次- *:出现 0 次或多次- +:出现 1 次或多次- {n}:出现 n 次- {n,}:出现 n 次或多次- {n,m}:出现 n 到 m 次3.边界匹配符:用于指定匹配的位置- ^:匹配字符串的开头- $:匹配字符串的结尾- b:匹配单词边界- B:匹配非单词边界4.分组和捕获:用于将正则表达式的一部分组合在一起,以便进行特定操作- (pattern):匹配 pattern 并捕获结果,可以通过引用组号获取- (?:pattern):匹配 pattern 但不捕获结果5.修饰符:用于修改正则表达式的匹配方式- i:不区分大小写- g:全局匹配(默认为局部匹配)- m:多行匹配(默认为单行匹配)三、JavaScript 正则表达式的四则运算在 JavaScript 中,正则表达式的四则运算与数学中的加减乘除类似,可以进行字符串的拼接和替换。
js的四则运算

js的四则运算JS是一种广泛应用于网页开发的脚本语言,它具备强大的四则运算能力。
在本文中,我们将探讨JS中的四则运算,并介绍一些常见的运算符和操作。
四则运算是数学中最基本的运算方式,包括加法、减法、乘法和除法。
在JS中,我们可以使用加号(+)进行加法运算,减号(-)进行减法运算,星号(*)进行乘法运算,斜杠(/)进行除法运算。
让我们来看看加法运算。
在JS中,加法运算可以用于两个数值的相加,也可以用于字符串的连接。
例如,我们可以使用加法运算符将两个数字相加,如下所示:```javascriptlet num1 = 10;let num2 = 20;let sum = num1 + num2;console.log(sum); // 输出30```加法运算符也可以用于字符串的连接。
例如:```javascriptlet str1 = "Hello";let str2 = "World";let result = str1 + " " + str2;console.log(result); // 输出 "Hello World"```接下来,让我们来看看减法运算。
在JS中,减法运算可以用于两个数值的相减。
例如:```javascriptlet num1 = 10;let num2 = 5;let difference = num1 - num2;console.log(difference); // 输出5```然后,让我们来看看乘法运算。
在JS中,乘法运算可以用于两个数值的相乘。
例如:```javascriptlet num1 = 10;let num2 = 3;let product = num1 * num2;console.log(product); // 输出30```让我们来看看除法运算。
在JS中,除法运算可以用于两个数值的相除。
js中简单计算器的代码

js中简单计算器的代码JS中实现一个简单的计算器并不困难,我们只需利用基本的数学运算符和JavaScript的语法即可。
下面是一个实现加、减、乘、除四则运算的计算器的JavaScript代码示例:```javascript// 获取计算器的元素var calculator = document.getElementById('calculator'); // 获取数字、运算符和结果显示区域的元素var num1 = document.getElementById('num1');var num2 = document.getElementById('num2');var operator = document.getElementById('operator');var result = document.getElementById('result');// 给运算符按钮添加事件处理函数var addButton = document.getElementById('add');addButton.addEventListener('click', function() {var n1 = parseFloat(num1.value);var n2 = parseFloat(num2.value);result.innerHTML = n1 + n2;});var subtractButton =document.getElementById('subtract');subtractButton.addEventListener('click', function() {var n1 = parseFloat(num1.value);var n2 = parseFloat(num2.value);result.innerHTML = n1 - n2;});var multiplyButton =document.getElementById('multiply');multiplyButton.addEventListener('click', function() { var n1 = parseFloat(num1.value);var n2 = parseFloat(num2.value);result.innerHTML = n1 * n2;});var divideButton = document.getElementById('divide'); divideButton.addEventListener('click', function() {var n1 = parseFloat(num1.value);var n2 = parseFloat(num2.value);result.innerHTML = n1 / n2;});```在以上代码中,我们定义了四个函数来处理加、减、乘、除四则运算。
四则运算代码(java版本)采用正则表达式

四则运算代码(java版本)采⽤正则表达式//加减乘除负数、括号这⼏种//具体看代码以及注释 (测试没发现bug,如发现有bug 请指正)package com.test;import java.util.ArrayList;import java.util.List;import java.util.regex.Matcher;import java.util.regex.Pattern;/*** 四则运算,可能不是最优的写法,⾃⼰测试没发现bug<br>* 前提:正确的算式,因为没做算式的验证<br>* 思路:<br>* 1、⽤正则拆分所有的元素例⼦:2-5+4*5/(2+2)-4<br>* 拆分为【2,-5,4,*,5,/,(,2,2,),-4】<br>* 2、先计算括号⾥,乘除,最后加减** 总结:总体花了17、8个⼩时,主要原因还是没有仔细总体分析流程导致。
<br>* 以⾄于想到点写点代码、修修改改,没有⼀个完整的把握的思路。
所以⼀个问题⼀定<br>* 先⾛通整个思路、流程其实编码是最容易的,重点是处理的过程思路** @author wangshiyan* @time 2014-12-4 下午12:50:01**/public class SizheTool {public static void main(String[] args) {try {System.out.println(jisuanStr("11.2+3.1*(423-(2+5.7*3.4)+5.6)-6.4/(15.5+24)"));} catch (Exception e) {System.out.println("请检查你的算式格式");e.printStackTrace();}}/*** 拆分算式⾥的各个元素并处理对应所在位置<br>** @param str* @return*/public static List<String> splitStr(String string) throws Exception {List<String> listSplit = new ArrayList<String>();Matcher matcher = pile("\\-?\\d+(\\.\\d+)?|[*/()]|\\-").matcher(string);// ⽤正则拆分成每个元素while (matcher.find()) {// System.out.println(matcher.group(0));listSplit.add(matcher.group(0));}return listSplit;}/*** 计算<br>* 步骤:1、如果有括号<br>* 然后取上⼀个最近的(坐标计算当前括号组合⾥的算式),在继续往下查找括号以此类推,直⾄循环使⽤到所有坐标元素* 计算完毕(运算顺序括号、乘除、加减)** @param str* @return*/public static double jisuanStr(String str) throws Exception {double returnDouble = 0;List<String> listSplit = splitStr(str); // 拆分好的元素List<Integer> zKuohaoIdxList = new ArrayList<Integer>();// 左括号,<所在坐标,>if (pile(".*\\(|\\).*").matcher(str).find()) {// 如果包含括号运算String value = "";// 单个字符值int zIdx = 0;// 上⼀个左括号在zKuoHaoIdxList的下标// 此层循环计算完所有括号⾥的算式List<String> tempList = new ArrayList<String>();// 前⾯没有计算的元素int removeL = 0;int tempListSize = 0;for (int i = 0; i < listSplit.size(); i++) {value = listSplit.get(i);tempList.add(value);tempListSize = tempList.size();if ("(".equals(value)) {// 左括号zKuohaoIdxList.add(tempListSize-1);} else if (")".equals(value)) {// 遇到右括号就计算与上⼀左括号间的算式zIdx = zKuohaoIdxList.size() - 1;// 离当前右括号最近的左括号配对int start = zKuohaoIdxList.get(zIdx);returnDouble = jisuan(tempList, start + 1, tempListSize-1); // 开始位置,就是上⼀个左括号 removeL = tempListSize - start;tempList = removeUseList(tempList, removeL);// 移除已使⽤的元素tempList.add(returnDouble + "");// 刚刚计算的值添加进来zKuohaoIdxList.remove(zIdx);// 计算完毕清除括号}}// 把所有计算完returnDouble = jisuan(tempList, 0, tempList.size());} else {// 没有括号运算returnDouble = jisuan(listSplit, 0, listSplit.size());}return returnDouble;}/*** 倒序删除已⽤过的元素** @param list* @param removeLength* 数量* @return*/public static List<String> removeUseList(List<String> list, int removeLength) {int le = list.size() - removeLength;for (int i = list.size() - 1; i >= le; i--) {list.remove(i);}return list;}/*** 计算算式** @param listSplit* @param start* 括号算式开始符位置* @param end* 括号结束符位置* @return*/public static double jisuan(List<String> listSplit, int start, int end)throws Exception {double returnValue = 0;String strValue = null;// 临时变量List<String> jjValueList = new ArrayList<String>();// 剩下的加减元素// 遍历计算乘除法for (int i = start; i < end; i++) {strValue = listSplit.get(i);if ("*".equals(strValue) || "/".equals(strValue)) {// 乘除strValue = jisuanValue("*".equals(strValue) ? "*" : "/", Double.parseDouble(jjValueList.get(jjValueList.size() - 1)),Double.parseDouble(listSplit.get(i + 1)))+ "";jjValueList.remove(jjValueList.size() - 1);i++;}jjValueList.add(strValue);}// 遍历计算加减for (int j = 0; j < jjValueList.size(); j++) {strValue = jjValueList.get(j);if ("-".equals(strValue) || "+".equals(strValue)) {returnValue = jisuanValue("-".equals(strValue) ? "-" : "+",returnValue, Double.parseDouble(jjValueList.get(j + 1)));j++;} else {returnValue += Double.parseDouble(jjValueList.get(j));}}return returnValue;}/*** 计算2个数间的加减乘除操作如:2*5 ,2/5** @param type* 运算符* @param start* 数相当于上⾯2* @param end* 被数相当于上⾯5* @return*/public static double jisuanValue(String type, double start, double end) throws Exception {double d = 0;if ("-".equals(type)) {d = start - end;} else if ("+".equals(type)) {d = start + end;} else if ("*".equals(type)) {d = start * end;} else if ("/".equals(type)) {if (0 == start || 0 == end)d = 0;elsed = start / end;}return d;}}。
javaweb编写四则运算

javaweb编写四则运算⾸先先画出⼀个表<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%><!DOCTYPE html><html><head><meta charset="UTF-8"><title>四则运算</title></head><form name="a1" action="chuti.jsp" onsubmit="return check(a1);"><h2 align="center">四则运算</h2><table align="center"border="2" width="400"><body><tr><td>你想要的题⽬数量</td><td><input type="text" name="number" value=""placeholder="题数"onchange="checkti(a1)"></td></tr><tr><td>你想要的题⽬的每⾏个数</td><td><input type="text" name="hang" value=""placeholder="⾏数"onchange="checkhang(a1)"></td></tr><tr><td colspan="2" align="center"><input type="submit" value="提交"></td></tr></table><script type="text/javascript">function check(a1){var num = a1.number.value;var hang= a1.hang.value;if(num.length==0){alert("题数不能为空");a1.number.value="";a1.number.focus();return false;}if(num.length==0){alert("⾏数不能为空");a1.number.value="";a1.number.focus();return false;}}function checkti(a1){var num = a1.number.value;if(num==""){alert("题数不能为空");a1.number.value="";a1.number.focus();return false;}}function checkhang(a1){var hang=a1.hang.value;if(hang.length==0){alert("⾏数不为空");a1.hang.value="";a1.hang.focus();return false;} 其中运⽤了script语句进⾏脚本判断,来初始判断你输进来的值然后我做了⼀个出题界⾯<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%><!DOCTYPE html><html><head><meta charset="UTF-8"><title>答题界⾯</title></head><%int h=Integer.parseInt(request.getParameter("hang"));int n=Integer.parseInt(request.getParameter("number"));String value[]=new String[n];String answer[]=new String[n];int i;for(i=0;i<n;){int num1=(int)(1+(Math.random()*100));int num2=(int)(1+(Math.random()*100));int f=(int)(1+(Math.random()*4));if(f==1){value[i]=num1+"+"+num2+"=";answer[i]=num1+num2+"";i++;}else if(f==2&&num1>=num2){value[i]=num1+"-"+num2+"=";answer[i]=num1-num2+"";i++;}else if(f==3&&num1*num2<100){value[i]=num1+"*"+num2+"=";answer[i]=num1*num2+"";i++;}else if(f==4&&num2!=0&&num1%num2==0){value[i]=num1+"/"+num2+"=";answer[i]=num1/num2+"";i++;}elsecontinue;}%><body><h2 align="center">练习题</h2><form action="panduan.jsp"><table align="center"border="2" width="400"><%int k;for(int j=0;j<n;){k=0;%><tr><%while(k<h){%><td><%=value[j] %></td><td><input type="hidden" name="value" value=<%=value[j] %>></td><td><input type="text" name="result" /></td><td><input type="hidden" name="answer" value=<%=answer[j] %>></td> <%j++;k++;if(j>=n)break;}%></tr><%}%><tr><td colspan="2" align="center"><input type="submit" value="提交"></td></tr></table></form></body></html> 然后设计⼀个后台来对程序进⾏判断<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%><!DOCTYPE html><html><head><meta charset="UTF-8"><title>答题界⾯</title></head><%int h=Integer.parseInt(request.getParameter("hang"));int n=Integer.parseInt(request.getParameter("number"));String value[]=new String[n];String answer[]=new String[n];int i;for(i=0;i<n;){int num1=(int)(1+(Math.random()*100));int num2=(int)(1+(Math.random()*100));int f=(int)(1+(Math.random()*4));if(f==1){value[i]=num1+"+"+num2+"=";answer[i]=num1+num2+"";i++;}else if(f==2&&num1>=num2){value[i]=num1+"-"+num2+"=";answer[i]=num1-num2+"";i++;}else if(f==3&&num1*num2<100){value[i]=num1+"*"+num2+"=";answer[i]=num1*num2+"";i++;}else if(f==4&&num2!=0&&num1%num2==0){value[i]=num1+"/"+num2+"=";answer[i]=num1/num2+"";i++;}elsecontinue;}%><body><h2 align="center">练习题</h2><form action="panduan.jsp"><table align="center"border="2" width="400"><%int k;for(int j=0;j<n;){k=0;%><tr><%while(k<h){%><td><%=value[j] %></td><td><input type="hidden" name="value" value=<%=value[j] %>></td><td><input type="text" name="result" /></td><td><input type="hidden" name="answer" value=<%=answer[j] %>></td> <%j++;k++;if(j>=n)break;}%></tr><%}%><tr><td colspan="2" align="center"><input type="submit" value="提交"></td></tr></table></form></body></html> 最后⼀个结束界⾯<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><!DOCTYPE html><html><head><meta charset="UTF-8"><title>结束界⾯</title></head><body>本次答题结束</body></html>。
js 正则表达式 四则运算

js 正则表达式四则运算摘要:1.正则表达式简介2.JavaScript 中正则表达式的应用3.JavaScript 中正则表达式的四则运算4.实例演示正文:正则表达式是一种强大的文本处理工具,它通过一系列特殊的字符和元字符来描述字符串的匹配模式。
在JavaScript 中,正则表达式被广泛应用于字符串操作,例如匹配、替换、分割等。
接下来我们将详细介绍JavaScript 中正则表达式的四则运算。
首先,我们需要了解正则表达式的基本语法和元字符。
在正则表达式中,一些特殊字符具有特殊含义,例如`.`表示任意字符、`*`表示零次或多次匹配、`+`表示一次或多次匹配、`?`表示零次或一次匹配等。
通过组合这些元字符,我们可以构建出复杂匹配模式的正则表达式。
在JavaScript 中,我们可以使用正则表达式对象的属性来表示四则运算。
例如,`regexp.source`属性返回正则表达式的原始字符串表示,`regexp.global`属性设置或返回全局匹配标志等。
此外,JavaScript 还提供了正则表达式的测试函数,例如`test()`函数用于测试一个字符串是否匹配某个正则表达式。
下面,我们通过一个实例来演示如何使用JavaScript 正则表达式进行四则运算。
假设我们有一个字符串`"1,2,3,4,5"`,我们希望将其分割成一个数组,数组中的每个元素都是一个整数。
我们可以使用以下正则表达式来实现这个功能:```javascriptconst regexp = /d+/g;const str = "1,2,3,4,5";const result = str.split(regexp);console.log(result); // ["1", "2", "3", "4", "5"]```在这个例子中,我们使用了正则表达式`/d+/g`,其中`d`表示匹配任意数字,`+`表示匹配一个或多个数字。
JSP。JavaBean实例---四则运算bean
JSP。
JavaBean实例---四则运算beanJSP JavaBean实例——四则运算BeanXXX是一种可重用的组件,我们可以在不同的程序中调用它来完成一定的逻辑功能。
现在,我们将创建一个案例,演示JavaBean的创建和调用。
该案例实现求取两个数的和、差、积和商等,包括一个JavaBean和两个JSP文件。
首先,我们需要创建JavaBean文件。
打开记事本,输入以下代码:package test;public class SiExample{private double num1;private double num2;public void setNum1(double num1){this.num1=num1;public void setNum2(double num2){ this.num2=num2;public double getNum1(){return num1;public double getNum2(){return num2;public double add(){double num3=num1+num2;return num3;public double jian(){double num3=num1-num2;return num3;public double cheng(){double num3=num1*num2;return num3;public double chu(){double num3=num1/num2;return num3;将上述代码保存,名称为SiExample.java。
在该文件中,我们创建了两个私有变量num1和num2,用来接收外部传递的参数。
接下来创建了set和get方法组,实现对变量num1和num2的赋值和获取。
最后创建了四个方法add()、jian()、cheng()和chu(),用来求取两个数的和、差、积和商。
接下来,我们需要创建客户输入页面。
简单js实现四则运算计算器
简单js实现计算器<html><head><script>function addnumber(){var num1=parseFloat(document.form1.num1.value); var num2=parseFloat(document.form1.num2.value); var num;num=(num1+num2);document.form1.num.value=num;}function jiannumber(){var num1=parseFloat(document.form1.num1.value); var num2=parseFloat(document.form1.num2.value); var num;num=(num1-num2);document.form1.num.value=num;}function chennumber(){var num1=parseFloat(document.form1.num1.value); var num2=parseFloat(document.form1.num2.value); var num;num=(num1*num2);document.form1.num.value=num;}function chunumber(){var num1=parseFloat(document.form1.num1.value); var num2=parseFloat(document.form1.num2.value); var num;num=(num1/num2);document.form1.num.value=num;}</script></head><body><form name="form1"><input type="text" name="num1" /><input type="button" name="addnum" value="+" onclick="addnumber()" /> <input type="button" name="jiannum" value="-" onclick="jiannumber()" /><input type="button" name="chennum" value="*"onclick="chennumber()" /> <input type="button" name="chunum" value="/" onclick="chunumber()" /> <input type="text" name="num2"> =<input type="text" name="num"></form></body></html>。
四则运算2的设计思想+源程序代码+运行结果截图+编程总结分析
四则运算2的设计思想+源程序代码+运⾏结果截图+编程总结分析(⼀)程序设计思想: 在第⼀个页⾯中输⼊要出的题数,然后接收这个数据去往数据库中写⼊相应数量的四则运算题⽬。
在第⼆个jsp页⾯中遍历输出数据库中的题⽬,并且加⼊⼀个⽂本框去让客户输⼊这个数据,当所有题都答完是下⾯有⼀个提交按钮,点击提交按钮,将⽤户输⼊的结果与数据库中结果进⾏⽐对,然后跳转到页⾯3去输出题⽬,⽤户结果,和判断结果,页⾯3有两个按钮分别是继续答题和推出,如果选择继续答题则跳转到页⾯1.(⼆)源程序代码:public class Dao {public void add(Connection conn ,users u) throws SQLException{try{Statement stmt = conn.createStatement();//浠庢暟鎹 簱閲岄潰鍙栦笢瑗垮 姣�PreparedStatement sql =conn.prepareStatement("insert into yunsuan(num1,num2,symbol,find)values(?,?,?,?)");sql.setInt(1,u.num1);sql.setInt(2,u.num2);sql.setString(3,u.symbol);sql.setInt(4,u.find);int rtn=sql.executeUpdate();}catch(Exception e){e.printStackTrace();}}}public class DBUtil {//eshop涓烘暟鎹 簱鍚嶇О锛宒b_user涓烘暟鎹 簱鐢ㄦ埛鍚峝b_password涓烘暟鎹 簱瀵嗙爜public static String db_url = "jdbc:sqlserver://localhost:1433; DatabaseName=users";public static String db_user = "sa";public static String db_password = "20163488";public static Connection getConn() {Connection conn = null;try {Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");conn = DriverManager.getConnection(db_url, db_user, db_password);} catch (Exception e) {e.printStackTrace();}return conn;}public static void close(Statement state, Connection conn) {if(state!=null) {try {state.close();} catch (SQLException e) {e.printStackTrace();}}if(conn!=null) {try {conn.close();} catch (SQLException e) {e.printStackTrace();}}}public static void close(ResultSet rs, Statement state, Connection conn) {if(rs!=null) {try {rs.close();} catch (SQLException e) {e.printStackTrace();}}if(state!=null) {try {state.close();} catch (SQLException e) {e.printStackTrace();}}if(conn!=null) {try {conn.close();} catch (SQLException e) {e.printStackTrace();}}}}public class YunServlet extends HttpServlet {/*** Constructor of the object.*/public YunServlet() {super();}/*** Destruction of the servlet. <br>*/public void destroy() {super.destroy(); // Just puts "destroy" string in log// Put your code here}/*** The doGet method of the servlet. <br>** This method is called when a form has its tag value method equals to get.** @param request the request send by the client to the server* @param response the response send by the server to the client* @throws ServletException if an error occurred* @throws IOException if an error occurred*/public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request,response);}/*** The doPost method of the servlet. <br>** This method is called when a form has its tag value method equals to post.** @param request the request send by the client to the server* @param response the response send by the server to the client* @throws ServletException if an error occurred* @throws IOException if an error occurred*/public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {dao.DBUtil db=new dao.DBUtil();Connection conn =db.getConn();Dao open=new Dao();int num=Integer.parseInt(request.getParameter("num")); int time=Integer.parseInt(request.getParameter("time")); HttpSession session = request.getSession(); session.setAttribute("time", time);//HttpSession session=request.getSession();//session.setAttribute("num", num);Random r=new Random();for(int i=0;i<num;i++){users u=new users();int r1=r.nextInt(100)+1;int r2=r.nextInt(100)+1;int r3=r.nextInt(4)+1;switch(r3){case 1:{int x=r1+r2;while(x>100||x<0){r1=r.nextInt(100)+1;r2=r.nextInt(100)+1;x=r1+r2;}u.num1=r1;u.num2=r2;u.symbol="+";u.find=x;try {open.add(conn, u);} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}break;}case 2:{int x=r1-r2;while(x>100||x<0){r1=r.nextInt(100)+1;r2=r.nextInt(100)+1;x=r1-r2;}u.num1=r1;u.num2=r2;u.symbol="-";u.find=x;try {open.add(conn, u);} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}break;}case 3:{int x=r1*r2;while(x>100||x<0){r1=r.nextInt(100)+1;r2=r.nextInt(100)+1;x=r1*r2;}u.num1=r1;u.num2=r2;u.symbol="*";u.find=x;try {open.add(conn, u);} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}break;}case 4:{int x=r1/r2;int root=r1%r2;while((x>100)||(root!=0)||(x<0)||(r1<r2)){r1=r.nextInt(100)+1;r2=r.nextInt(100)+1;x=r1/r2;root=r1%r2;}u.num1=r1;u.num2=r2;u.symbol="/";u.find=x;try {open.add(conn, u);} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}break;}}}request.getRequestDispatcher("../MyJsp.jsp").forward(request,response); }/*** Initialization of the servlet. <br>** @throws ServletException if an error occurs*/public void init() throws ServletException {// Put your code here}}public class users {public int num1;public int num2;public String symbol;public int find;public int getNum1() {return num1;}public void setNum1(int num1) {this.num1 = num1;}public int getNum2() {return num2;}public void setNum2(int num2) {this.num2 = num2;}public String getSymbol() {return symbol;}public void setSymbol(String symbol) {this.symbol = symbol;}public int getFind() {return find;}public void setFind(int find) {this.find = find;}}<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><base href="<%=basePath%>"><title>主页</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0"><meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">--><style type="text/css">#form2 p {text-align: center;}#form1 p label {text-align: center;}#form1 {text-align: center;}</style><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body background="timg.jpg"><form id="form2" name="form2" method="post" action=""><p> </p><p> </p><p> </p><p> </p><p> </p></form><form id="form1" name="form1" method="post" action="servlet/YunServlet?method=add"><p><span id="form1"><label for="name">要做的题数:</label><input type="text" name="num" id="num" /></span></p><p><label for="name2">限制的时间:</label><input type="text" name="time" id="time" /></p><p><input type="submit" name="tijiao" id="tijiao" align="center" value="提交" /></p></form></body></html><%@ page language="java" import="java.util.*,java.sql.*" pageEncoding="utf-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><base href="<%=basePath%>"><title>做题页</title><style type="text/css">.aaa {text-align: center;font-weight: bold;}.aa {text-align: center;}#tijiao {text-align: center;}#form p label {text-align: center;}#form1{text-align: center;}.qqq {text-align: center;font-size: 36px;font-family: "微软雅⿊";}</style><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0"><meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">--></head><body background="timg.jpg"><p> </p><p class="qqq">做题页</p><p> </p><p><form id="form1" name="form1" method="post" action="MyJsp1.jsp"><table width="386" border="1" align="center"><td width="79" height="23" class="aaa">题号</td><td width="116" class="aaa">题⽬</td><td width="169" class="aaa">答案</td></tr><%int time1=(int)session.getAttribute("time");dao.DBUtil db=new dao.DBUtil();Connection conn =db.getConn();Statement stmt = conn.createStatement();//从数据库⾥⾯取东西对⽐ResultSet rs=stmt.executeQuery("select * from yunsuan");int x=0;int y=1;while(rs.next()){x++;int num1=rs.getInt("num1");int num2=rs.getInt("num2");String symbol=rs.getString("symbol");%><table width="386" border="1" align="center"><tr><td width="79" height="23" class="aa"><%=y %>.</td><td width="116" class="aa"><%=num1+symbol+num2 %>=</td><td width="169" class="aa"><input type="text" name="<%=x %>" id="<%=x %>" /></td></tr></table><%y++;}%></table><p> </p><input type="submit" name="tijiao" id="tijiao" value="提交" /></form><p> </p> <head> <script type="text/javascript"> var time =<%=time1%>; //时间,秒 function Redirect() { window.location = "MyJsp1.jsp"; } var i = 0; function dis() { document.all.s.innerHTML = "还剩" + (time - i) + "秒"; i++; } timer = setInterval('dis()', 1000); //显⽰时间 timer = setTimeout('Redirect()', time * 1000); //跳转 </script> </head><table width="507" border="1" align="center"> <td width="229" class="a" style="text-align: center; color: #F00;"><span id="s"></span></td></table></body></html><%@ page language="java" import="java.util.*,java.sql.*" pageEncoding="UTF-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html><head><base href="<%=basePath%>"><title>答案页</title><style type="text/css">#form1 p label {text-align: center;}#form1 {text-align: center;}</style><style type="text/css">.a {color: #0F0;}.qqq {text-align: center;font-size: 36px;font-family: "微软雅⿊";}</style><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0"><meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">--></head><body background="timg.jpg"><p> </p><p class="qqq">结果页</p><p> </p><p><%dao.DBUtil db=new dao.DBUtil();Connection conn =db.getConn();Statement stmt = conn.createStatement();//从数据库⾥⾯取东西对⽐ResultSet rs=stmt.executeQuery("select * from yunsuan");int x=0;double q=0;//对题数double p=0;//错题数while(rs.next()){x++;String y=String.valueOf(x);int num1=rs.getInt("num1");int num2=rs.getInt("num2");String symbol=rs.getString("symbol");int find=rs.getInt("find");int a=0;if(request.getParameter(y)!=null&&(!"".equals(request.getParameter(y)))) {a=Integer.parseInt(request.getParameter(y));}%></p><table width="507" border="1" align="center"><td width="131" style="text-align: center"><%=num1+symbol+num2 %>=</td><td width="125" style="text-align: center"><%=a %></td><%if(find==a){q++;%><td width="229" class="a" style="text-align: center">回答正确</td><%}else{p++;%><td width="229" class="a" style="text-align: center; color: #F00;">回答错误,正确答案为<%=find %></td> <%}}%></tr></table><%stmt.executeUpdate("delete from yunsuan");%><br><br><br><table width="700" border="1" align="center"><tr><td width="200" height="23" class="aaa">总题数:<%=q+p%></td><td width="300" class="aaa">做对<%=q %>题正确率:<%=q/(q+p)*100 %>%</td><td width="200" class="aaa">做错<%=p %>题</td></tr></table><form id="form1" name="form1" method="post" action="index.jsp"><p> </p><input type="submit" name="tijiao" id="tijiao" value="继续答题" /></form></body></html><%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%String path = request.getContextPath();String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><base href="<%=basePath%>"><title>My JSP 'time.jsp' starting page</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0"><meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">--></head><html> <head> <meta charset="utf-8"> <script type="text/javascript"> var time = 8; //时间,秒 function Redirect() { window.location = "MyJsp.jsp"; } var i = 0; function dis() { document.all.s.innerHTML = "还剩" + (time - i) + "秒"; i++; } timer = setInterval('dis()', 1000); //显⽰时间 timer = setTimeout('Redirect()', time * 1000); //跳转 </script> </head> <body> <span id="s"></span> </body></html>(三)运⾏结果截图:(四)编程总结:组队过程中,别⼈⽐⾃⼰更容易去发现⾃⼰代码⾥的漏洞,在设计⽅⾯,两个⼈⼀起总结互相的意见中的有点,相对⼀个⼈的意见容易⾛歪路⽽⾔两个⼈更能想到的客户的需求,以及页⾯的布局与功能也随着两个⼈的讨论⽽越来越优化。
javabean实现四则运算
computer.jsp页面<%@ page language="java" import="java.util.*" pageEncoding="gbk"%><%@ page import="tom.jiafei.*"%><%String path = request.getContextPath();String basePath = request.getScheme() + "://"+ request.getServerName() + ":" + request.getServerPort()+ path + "/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><base href="<%=basePath%>"><title>My JSP 'computer.jsp' starting page</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0"><meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">--></head><jsp:useBean id="com" class="puterBean" scope="session"></jsp:useBean> <body bgcolor="yellow"><h2>四则运算</h2><jsp:setProperty name="com" property="*" /><form action="" method="post" name="form"><input type="text" name="number1"value=<jsp:getProperty name="com" property="numberOne"/> size=5> <select name="operator"><option value="+">+<option value="-">-<option value="*">*<option value="/">/</select><input type="text" name="number2"value=<jsp:getProperty name="com" property="numberTwo"/> size=5> =<jsp:getProperty name="com" property="result" /><P><input type="submit" name="sub" value="计算"></form></body></html>ComputerBean.java页面package tom.jiafei;public class ComputerBean {double numberOne, numberTwo, result;String operator = "";public void setNumberOne(double m) {numberOne = m;}public double getNumberOne() {return numberOne;}public void setNumberTwo(double n) {numberTwo = n;}public double getNumberTwo() {return numberTwo;}public void setOperator(String s) {operator = s;}public String getOperator() {return operator;}public double getResult() {if (operator.equals("+")) {result = numberOne + numberTwo;} else if (operator.equals("-")) {result = numberOne - numberTwo;} else if (operator.equals("*")) {result = numberOne * numberTwo;} else if (operator.equals("/")) {result = numberOne / numberTwo;}return result;}}。