A arithmetic expression is generated on the Web page, and the user can enter the value of the expression, and then the system prompts for the correct result. This web-based program is a bit easier than the native program, because it is easy to get an expression because the number of operators in the expression is no more than three. Also, JavaScript comes with a function that evaluates the value of an expression, and we can call this function to do a calculation of the value of an expression.
<%@ 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" >Please enter an answer<input type= "text" id= "answer" value= "" maxlength= "8" style= "width:50px;" > </p> <p> <input type= "button" name= "button" id= "Btnans" value= "Answer" style= "wid th:50px;height:30px; " > <input type= "button" name= "button" id= "Btnnext" value= "Continue" style= "dispaly:none;width:50px;height:30px;" > </p> <p id= "sans" > <span></span> </p> </div> <script type= "Text/javascript" >function Getelebyid (ID) {returndocument.getElementById (ID); } function Randomnumber (limit) {returnMath.floor (Math.random () *limit); } var sign=NewArray ("+", "-", "*", "/"); var numa,numb,numx,num; var countSign1= "+", countSign2 = "+"; Getelebyid ("Btnnext"). onclick =function () { This. style.display = "None"; Getelebyid ("Btnans"). Style.display = "inline"; Getelebyid ("Answer"). Disabled = ""; Getelebyid ("Answer"). Value = ""; Getelebyid ("Sans"). InnerHTML = ""; Do{numa= Randomnumber (10); Numb= Randomnumber (10); Numx= Randomnumber (10); CoutSign1= Sign[randomnumber (100)%4]; CoutSign2= Sign[randomnumber (100)%4]; } while(Numb = = 0 && coutSign1 = = "/") | | (Numx = = 0 && coutSign2 = = "/")) Numa= Numa.tostring (10); Numb= Numb.tostring (10); Numx= Numx.tostring (10); Getelebyid ("Num1"). InnerHTML =Numa; Getelebyid ("Sign1"). InnerHTML =coutSign1; Getelebyid ("Num2"). InnerHTML =numb; Getelebyid ("Sign2"). InnerHTML =coutSign2; Getelebyid ("Num3"). InnerHTML =numx; var num= Math.floor (Math.random ()); Num= Num.tostring (10); if(num = = 0) { } if(num = = 1) {Getelebyid ("Bracket1left"). InnerHTML = "("; Getelebyid ("Bracket1right"). InnerHTML = ")"; if((Getelebyid ("Bracket2left"). InnerHTML = = "(" && Getelebyid ("Bracket1right"). InnerHTML = = ")") || (Getelebyid ("Bracket2right"). InnerHTML = = ")" && Getelebyid ("Bracket1right"). InnerHTML = = ")") {Getelebyid ("Bracket1left"). InnerHTML = ""; Getelebyid ("Bracket1right"). InnerHTML = ""; Getelebyid ("Bracket2left"). InnerHTML = ""; Getelebyid ("Bracket2right"). InnerHTML = ""; } } if(num = = 2) {Getelebyid ("Bracket2left"). InnerHTML = "("; Getelebyid ("Bracket2right"). InnerHTML = ")"; if(Getelebyid ("Bracket2left"). InnerHTML = = "(" && Getelebyid ("Bracket1right"). InnerHTML = = ")" | | (Getelebyid ("Bracket2right"). InnerHTML = = ")" && Getelebyid ("Bracket1right"). InnerHTML = = ")") {Getelebyid ("Bracket1left"). InnerHTML = ""; Getelebyid ("Bracket1right"). InnerHTML = ""; Getelebyid ("Bracket2left"). InnerHTML = ""; Getelebyid ("Bracketright"). InnerHTML = ""; }} Getelebyid ("Answer"). Foucs (); } Getelebyid ("Btnans"). onclick =function () {if(Getelebyid ("answer"). value== "" | | IsNaN (Getelebyid ("answer")) {alert ("Please enter a number"); Getelebyid ("Answer"). Value = ""; Getelebyid ("Answer"). Foucs (); return; } This. style.display = "None"; Getelebyid ("Btnnext"). Style.display = "inline"; if(Getelebyid ("Bracket1left"). InnerHTML = = "" && Getelebyid ("Bracket2left"). InnerHTML = = "") {var result= eval (numa+coutsign1+numb+coutsign2+numx); } if(Getelebyid ("Bracket1left"). InnerHTML = = "(") {var result= eval (' (' +numa+coutsign1+numb+ ') ' +coutsign2+numx); } if(Getelebyid ("Bracket2left"). InnerHTML = = "(") {var result= eval (numa+coutsign1+ ' (' +numb+coutsign2+numx+ ') '); } var isright=false; if(/^[-]{0,1}[0-9]{1,}$/. Test (Result)) { if(Result = = parseint (Getelebyid ("answer")) (value)) Isright=true; } Else{result= result.tofixed (1); var ans= Parsefloat (Getelebyid ("Answer"). Value); Getelebyid ("Answer"). Value =ans; if(Result-getelebyid ("answer") < 0.01) {Isright=true; } } if(Isright = =true) {Getelebyid ("Sans"). InnerHTML = "Correct it!"; } Else{Getelebyid ("Sans"). InnerHTML = "answer wrong!" The correct answer is: "+result.tostring () +". "; }} document.body.onload=function () {Getelebyid ("Btnnext"). onclick (); } document.body.onkeyup=function () {if(13 = =event.keycode) {if(Getelebyid ("Btnans"). Style.display = = "None") {Getelebyid ("Btnnext"). onclick (); } Else{Getelebyid ("Btnans"). onclick (); } } } </script> </body>
Answer
No input operation results
Enter non-numeric
Answer right
Answer the wrong
Arithmetic Expression Web version