四则运算(三)

1.设计思路

  接着上次的程序写,这周出题可以加入括号,但是带括号以及分数答案的计算还没有完成,还在思考,另外这次加入了数据库,可以将题目及答案存入数据表中,没有答案的题存为null值,连上数据库,用SQL语句就能存入。

2.源代码

  

  1 <%@ page language="java" contentType="text/html; charset=UTF-8"
  2     pageEncoding="UTF-8" %>
  3     <%@ page import="jisuan.*" import="java.sql.*" import="chuti.*" import="dbcoon.*" %>
  4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  5 <html>
  6 <head>
  7 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
  8 <title>自动出题</title>
  9 </head>
 10     
 11 <body>
 12     <%
 13     String type = request.getParameter("type");
 14     int cc=Integer.parseInt(request.getParameter("chengchu"));
 15     int kuohao=Integer.parseInt(request.getParameter("kuohao"));
 16     int fw=Integer.parseInt(request.getParameter("fanwei"));
 17     int num=Integer.parseInt(request.getParameter("num"));
 18     int chutishu=Integer.parseInt(request.getParameter("chutishu"));
 19     int da=Integer.parseInt(request.getParameter("daan"));
 20     int yushu=Integer.parseInt(request.getParameter("yushu"));
 21     int fushu=Integer.parseInt(request.getParameter("fushu"));
 22     
 23     JspWriter mout=pageContext.getOut();
 24      String []a=new String [chutishu];
 25     String []daan=new String[chutishu];
 26     
 27     //连数据库
 28     String driverName="com.microsoft.sqlserver.jdbc.SQLServerDriver";
 29     String dbURL="jdbc:sqlserver://localhost:1433;DatabaseName=Web";
 30     String userName="sa";
 31     String userPwd="ghs123456";
 32     Class.forName(driverName);
 33     Connection dbConn=DriverManager.getConnection(dbURL,userName,userPwd);
 34     
 35     Statement st = dbConn.createStatement();
 36     
 37     //出题
 38     for(int j=0;j<chutishu;j++){
 39         if(type.equals("zhengshu"))
 40             a[j]=Chuti.putong(num,cc,fw);
 41         else if(type.equals("zhenfenshu"))
 42             a[j]=Chuti.zhenfenshu(num,cc);
 43         else if(type.equals("douyou")){
 44             int m=(int)(Math.random()*2+1);
 45             if(m==1)
 46                 a[j]=Chuti.putong(num,cc,fw);
 47             else if(m==2)
 48                 a[j]=Chuti.zhenfenshu(num,cc);
 49         }
 50         //查重
 51         for(int k=0;k<j;k++){
 52             if(a[k].equals(a[j]))
 53                 {j--;break;}
 54             else
 55                 continue;
 56              }
 57         //加括号
 58         if(kuohao==1)
 59             a[j]=Chuti.kuohao(a[j],num);
 60         //计算结果(不带括号)
 61         if(kuohao==0)
 62         daan[j]=String.valueOf(Zhengshujisuan.fun1(a[j]));
 63         //多元运算结果无限循环处理
 64         if(type.equals("zhengshu")&&num>2&&kuohao==0){
 65              if (daan[j].length()-daan[j].indexOf(".")>5)
 66                  j--;
 67              else
 68                  continue;
 69     }
 70         //二元运算有无余数处理
 71         if(type.equals("zhengshu")&&yushu==0&&num==2){
 72                  if (daan[j].length()-daan[j].indexOf(".")>2)
 73                      j--;
 74                  else
 75                      continue;
 76         }
 77         //有无负数处理
 78         if(type.equals("zhengshu")&&fushu==0&&kuohao==0){
 79             if (daan[j].charAt(0)=='-')
 80                  j--;
 81         }
 82     }
 83     //输出题目并存入数据库
 84     for(int i=0;i<chutishu;i++){
 85         mout.print((i+1)+". "+a[i]+"<br>");
 86         
 87     }
 88     //如果是整数输出答案
 89     mout.print("答案:<br>");
 90     //有余数输出带余数的答案
 91     if(type.equals("zhengshu")&&yushu==1&&num==2){
 92         for(int i=0;i<chutishu;i++){
 93             if(daan[i].length()-daan[i].indexOf(".")>2){
 94                 int first=Integer.parseInt(a[i].substring(0,a[i].indexOf("÷")));
 95                 int second=Integer.parseInt(a[i].substring(a[i].indexOf("÷")+1,a[i].length()));
 96                 daan[i]=first/second+"········"+first%second;
 97             }
 98         }
 99     }
100     if(type.equals("zhengshu")&&da==1){
101         for(int i=0;i<chutishu;i++){
102             mout.print(i+1+".    "+daan[i]+"<br>");
103         }
104     }
105     for(int i=0;i<chutishu;i++){
106     String sqlStr="INSERT INTO  出题 (No,题目,答案)VALUES('"+(i+1)+"' , '" + a[i] + "' ,'" + daan[i] + "'  )";
107     st.executeUpdate(sqlStr);
108     }
109     st.close();
110     dbConn.close();
111     
112     %>
113 </body>
114 </html>

3.截图

 

 

转载于:https://www.cnblogs.com/ghs1065248758/p/6566920.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值