今天写了题目正误判断的页面:
<%@ page language="java" import="java.util.*" import="java.text.*" pageEncoding="utf-8"%> <!DOCTYPE html> <html> <head> <title>Now here is the result</title> </head> <body style="text-align:center;"> <%! public static String getzongde(String str){ String kuohao=getkuohao(str); String chengchu=getchengchu(kuohao); String jiajian=getjiajian(chengchu); return jiajian; } public static String getkuohao(String str){ String newstr=str; if(newstr.contains("(")){ int a=str.lastIndexOf("("); String str1=str.substring(a,str.length()); int b =str1.indexOf(")"); String str2=str1.substring(1,b); String chengchu=getchengchu(str2); String jiajian=getjiajian(chengchu); StringBuffer s=new StringBuffer(str); newstr=s.replace(a, a+b+1, jiajian).toString(); return getkuohao(newstr); } return newstr; } public static String getchengchu(String str){ String newstr=str; if(str.contains("*")||str.contains("/")){ String str2[]=str.split("[*/+-]");// String str3=str.replaceAll("\d+","").replaceAll("\.",""); int a=0,b=0,c=0,star=0; if(str3.contains("*")&&str3.contains("/")){ a=str3.indexOf("*"); b=str3.indexOf("/"); if(a>b){ c=b; star=str.indexOf("/"); }else{ c=a; star=str.indexOf("*"); } }else if(str3.contains("*")){ c=str3.indexOf("*"); star=str.indexOf("*"); }else{ c=str3.indexOf("/"); star=str.indexOf("/"); } String num=getsuanshi(str2[c]+str3.charAt(c)+str2[c+1]); StringBuffer s=new StringBuffer(str); int kaishi=star-str2[c].length(),jieshu=star+str2[c+1].length()+1; StringBuffer strs=s.replace(kaishi,jieshu ,num); newstr=strs.toString(); return getchengchu(newstr); } return newstr; } public static String getjiajian(String str){ String newstr=str; if(str.contains("+")||str.contains("-")){ String str2[]=str.split("[*/+-]"); String str3=str.replaceAll("\d+","").replaceAll("\.",""); int a=0,b=0,c=0,star=0; if(str3.contains("+")&&str3.contains("-")){ a=str3.indexOf("+"); b=str3.indexOf("-"); if(a>b){ c=b; star=str.indexOf("-"); }else{ c=a; star=str.indexOf("+"); } }else if(str3.contains("+")){ c=str3.indexOf("+"); star=str.indexOf("+"); }else{ c=str3.indexOf("-"); star=str.indexOf("-"); } String num=getsuanshi(str2[c]+str3.charAt(c)+str2[c+1]); StringBuffer s=new StringBuffer(str); int kaishi=star-str2[c].length(),jieshu=star+str2[c+1].length()+1; StringBuffer strs=s.replace(kaishi,jieshu ,num); newstr=strs.toString(); return getjiajian(newstr); } return newstr; } public static String getsuanshi(String str){ double c=0.0; String str2[]; if(str.contains("+")){ str2=str.split("[+]"); c=getjia(Double.parseDouble(str2[0]),Double.parseDouble(str2[1])); }else if(str.contains("-")){ str2=str.split("[-]"); c=getjian(Double.parseDouble(str2[0]),Double.parseDouble(str2[1])); }else if(str.contains("*")){ str2=str.split("[*]"); c=getcheng(Double.parseDouble(str2[0]),Double.parseDouble(str2[1])); }else if(str.contains("/")){ str2=str.split("[/]"); c=getchu(Double.parseDouble(str2[0]),Double.parseDouble(str2[1])); } NumberFormat format=NumberFormat.getInstance(); format.setMaximumFractionDigits(30); format.setMinimumFractionDigits(0); format.setGroupingUsed(false); return format.format(c); } public static Double getjia(double a,double b){ double c=a+b; return c; } public static Double getjian(double a,double b){ double c=a-b; return c; } public static Double getcheng(double a,double b){ double c=a*b; return c; } public static Double getchu(double a,double b){ double c=a/b; return c; } %> <br><br><br>答题结果<br><br><br> <% int numT=Integer.parseInt(request.getParameter("size"));int s=0,i=0;%> <% String xor=(String)session.getAttribute("numR"); if(xor!=null) {s=Integer.parseInt(xor);} for(i=0;i<numT;i++){ String str=(String)session.getAttribute("suanshi"+i); String answer=request.getParameter(""+i); String keka=getzongde(str); out.print("第"+(i+1)+"题:"+str+"="+keka); %> <% if(answer.equals(keka)){ %> ————————正确(√) <%} else{%> ————————错误(×) <%session.setAttribute(("cuoti"+s),str);s++;%> <%} %> <br> <%}%> <br><br> 本次做题的得分(百分制)为:<%out.print(String.format("%.2f",(double)(i-s)/((double)i)*100)); %> <%session.setAttribute("numR",""+s);%> <br><br> <form name="form1" action="indes.jsp" method="post"> <input type="submit" value="回到主页面"> </form> <br> <form name="form2" action="Damit.jsp" method="post"> <input type="submit" value="错题本"> </form> <br><br><br> </body> </html>
运行结果: