今天开始写个Javaweb吧
我把四则运算的出题界面写成了web,运行一下发现真的行哦
虽然还没做出答题的审核页面,但毕竟是网站嘛,这就是个重大突破啦
这时第一个页面(设定相关参数页面)
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <html> <head> <title>Four operations</title> </head> <body style="text-align:center;"> <br> <form name="form1" action="Question.jsp" method="post"> <br><br> <input type="radio" name="degree" value="0">自己设置 <input type="radio" name="degree" value="1" checked>2年级版本 <input type="radio" name="degree" value="2">3年级版本 <input type="radio" name="degree" value="3">4年级版本 如果您没有选择"自己设置"选项,请跳过下面的设置选项 <br><br> 问题数 <input type="text" name="selfn"> <br><br> 最大值 <input type="text" name="selfma"> <br><br> 最小值 <input type="text" name="selfmi"> <br><br> 如果您的输入不合规则,我们会采用默认参数啦 <br><br> <input type="submit" value="Let us go!"> </form> <form name="form2" action="Damit.jsp" method="post"> <input type="submit" value="错题本"> </form> <br><br> </body> </html>
然后第二个页面:出题页面
<%@ page language="java" import="java.util.*" import="java.text.*" pageEncoding="utf-8"%> <!DOCTYPE html> <html> <head> <title>Go!Go!Go!</title> </head> <body style="text-align:center;" > <% String chooce=request.getParameter("degree"); String tmax=request.getParameter("selfma") ; String tmin=request.getParameter("selfmi") ; String tnum=request.getParameter("selfn") ; Random rand = new Random(); int numT=30,numU=2,max=10,min=99; String dop=null; if(chooce.equals("1")) { numU=2; max=100;min=0; } else if(chooce.equals("2")) { numU=4; max=100;min=0; } else if(chooce.equals("3")) { numU=5; max=100;min=0; } if(chooce.equals("0")) { max= Integer.parseInt(tmax); min= Integer.parseInt(tmin); numT= Integer.parseInt(tnum); if((max-min)<=(2*numT)) { max=100;min=0;numT=30; } } int c=0,p=0,rio=0,sop=0; String[] suanshi=new String[1000]; for(int i=0;i<numT;i++) { p=rand.nextInt(max)-min; suanshi[i]=""+p; int sc=0,sd=0; int error=1; for(;error==1;) { error=0; for(int n=1;n<numU;n++) { c=rand.nextInt(3); switch(c) { case 0: rio=rand.nextInt(max-min)+min+2; if(sc==1) {suanshi[i]=suanshi[i]+("+"+rio+")");sc=0;} else { int f=rand.nextInt(2); if((n<=numU-2)&&(f==1)&&(sd==0)) {suanshi[i]=suanshi[i]+("+("+rio+"");sc=1;sd=1;} else {suanshi[i]=suanshi[i]+("+"+rio);} } sop=rio; break; case 1: rio=rand.nextInt(max-min)+min+2; if(sc==1) {suanshi[i]=suanshi[i]+("-"+rio+")");sc=0;} else { int f=rand.nextInt(2); if((n<=numU-2)&&(f==1)&&(sd==0)) {suanshi[i]=suanshi[i]+("-("+rio+"");sc=1;sd=1;} else {suanshi[i]=suanshi[i]+("-"+rio);} } sop=rio; break; case 2: rio=rand.nextInt(max-min)+min+2; if(sc==1) {suanshi[i]=suanshi[i]+("*"+rio+")");sc=0;} else { int f=rand.nextInt(2); if((n<=numU-2)&&(f==1)&&(sd==0)) {suanshi[i]=suanshi[i]+("*("+rio+"");sc=1;sd=1;} else {suanshi[i]=suanshi[i]+("*"+rio);} } sop=rio; break; case 3: rio=rand.nextInt(max-min)+min+2; try { for(;(sop<rio)||(sop%rio!=0);) {rio=rand.nextInt(max-min)+min;} }catch(ArithmeticException e) { rio=1; } if(sc==1) {suanshi[i]=suanshi[i]+("/"+rio+")");sc=0;} else { int f=rand.nextInt(2); if((n<=numU-2)&&(f==1)&&(sd==0)) {suanshi[i]=suanshi[i]+("/("+rio+"");sc=1;sd=1;} else {suanshi[i]=suanshi[i]+("/"+rio);} } sop=rio; break; } } try { dop=getzongde(suanshi[i]); if(dop.contains(".")) {suanshi[i]=""+p;error=1;} } catch (Exception e) {suanshi[i]=""+p;error=1;} } } %> <%! 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; } %> <form name="form1" action="Rider.jsp" method="post"> 共<input type="text" name="size" size="1" value="<%=numT%>" readonly>题 <br><br> <%for(int i=0;i<numT;i++){ %> <tr> <% out.print("第"+(i+1)+"题:");out.print(suanshi[i]); %> <br> </tr> <%session.setAttribute(("suanshi"+i),suanshi[i]);%> <input type="text" name=<%=i%>><br><br> <%} %> <input type="submit" value="我做完啦!"> </form> <br><br><br> </body> </html>
运行效果: