17-12-06——四則運算

1.設計思想:css

    主要是隨機生成兩個數(根據狀況限制一下),作它們加減乘除,將式子與結果存入數據庫,用戶輸入本身的答案,與數據庫答案比較,輸出作題狀況!!html

2.源程序代碼前端

/**鏈接數據庫*/java

package Util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class DBUtil {
    
    public  static  Connection getConnection() {
        try {
            
            Class.forName("com.mysql.jdbc.Driver").newInstance();
        } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        String user = "root";
        String password = "root";
        String url = "jdbc:mysql://localhost:3306/jaovo_msg";
        Connection connection = null;
        try {
        
             connection = DriverManager.getConnection(url,user,password);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return connection;
    }
    

    public static void close(Connection connection ) {
        try {
            if (connection != null) {
                connection.close();
            }
            
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    public static void close(PreparedStatement preparedStatement ) {
        try {
            if (preparedStatement != null) {
                preparedStatement.close();
            }
            
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    public static void close(ResultSet resultSet ) {
        try {
            if (resultSet != null) {
                resultSet.close();
            }
            
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    
}

/**操做*/mysql

/**11*/
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8" import="java.sql.Connection,java.sql.DriverManager,java.sql.ResultSet"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>整數式子</title>
<style type="text/css">
body,td,th {
    font-size: 24px;
}
body {
    background-color: #0CF;
    background-image: url(a.jpg);
}
</style>
</head>
<body>
<div align="center">
  <%!
    int i=0;
    String Array[]=new String[1000];//array 表達式
    class Zhengshu//整數類
    {
    String re1="";//初始化 「結果」 String類型的
    int re;
    String function(int r ,int q, int fun)// r最小值,q最大值,fun 選擇
    {
        //1有乘除法 2加減無負數 3除法無餘數(12,1,0)
        String fu ="";
        String shizi="";
        int a=(int)(Math.random()*(r)%(r-q+1)+q);
        int b=(int)(Math.random()*(r)%(r-q+1)+q);
        int c=(int)(Math.random()*4);
        if(fun==3)
        {
            if(c==0)
            {
                fu="+";
                re=a+b;
                re1=String.valueOf(re);
                shizi=a+" "+fu+" "+b;
            }
            if(c==1)
            {
                fu="-";
                //排除負數可能性
                while(a-b<0)
                {
                    a=(int)(Math.random()*(r)%(r-q+1)+q);
                    b=(int)(Math.random()*(r)%(r-q+1)+q);
                }
                re=a-b;
                re1=String.valueOf(re);
                shizi=a+" "+fu+" "+b;
            }
            if(c==2)
            {
                fu="+";
                re=a+b;
                re1=String.valueOf(re);
                shizi=a+" "+fu+" "+b;
            }
            if(c==3)
            {
                fu="-";
                //排除負數可能性
                while(a-b<0)
                {
                    a=(int)(Math.random()*(r)%(r-q+1)+q);
                    b=(int)(Math.random()*(r)%(r-q+1)+q);
                }
                re=a-b;
                re1=String.valueOf(re);
                shizi=a+" "+fu+" "+b;
            }
        }

         //2有乘除法餘數不定
        if(fun==2)
        {
            if(c==0)
            {
                fu="+";
                re=a+b;
                re1=String.valueOf(re);
                shizi=a+" "+fu+" "+b;
            }
            if(c==1)
            {
                fu="-";
                //排除負數可能性
                while(a-b<0)
                {
                    a=(int)(Math.random()*(r)%(r-q+1)+q);
                    b=(int)(Math.random()*(r)%(r-q+1)+q);
                }
                shizi=a+" "+fu+" "+b;
                re=a-b;
                re1=String.valueOf(re);
            }
            if(c==2)
            {
                fu="×";
                re=a*b;
                re1=String.valueOf(re);
                shizi=a+" "+fu+" "+b;
            }
            if(c==3)
            {
                fu="÷";
                //排除分母爲0的可能性
                while(b==0)
                {
                    b=(int)(Math.random()*(r)%(r-q+1)+q);
                }    
                re=a/b;
                if(a%b!=0)
                {
                    int flag1=a%b;
                    re1=String.valueOf(re)+"..."+String.valueOf(flag1);
                }
                else
                    re1=String.valueOf(re);
                shizi=a+" "+fu+" "+b;
            }
        }
        //1有乘除法
        if(fun==1)
        {
            if(c==0)
            {
                fu="+";
                shizi=a+" "+fu+" "+b;
                re=a+b;
                re1=String.valueOf(re);
            }
            if(c==1)
            {
                fu="-";
                //排除負數可能性
                while(a-b<0)
                {
                    a=(int)(Math.random()*(r)%(r-q+1)+q);
                    b=(int)(Math.random()*(r)%(r-q+1)+q);
                }
                shizi=a+" "+fu+" "+b;
                re=a-b;
                re1=String.valueOf(re);
            }
            if(c==2)
            {
                fu="×";
                shizi=a+" "+fu+" "+b;
                re=a*b;
                re1=String.valueOf(re);
            }
            if(c==3)
            {
                fu="÷";
                //排除分母爲0的可能性和餘數的可能性
                while(b==0||a%b!=0)
                {
                    a=(int)(Math.random()*(r)%(r-q+1)+q);
                    b=(int)(Math.random()*(r)%(r-q+1)+q);
                }    
                re=a/b;
                shizi=a+" "+fu+" "+b;
                re1=String.valueOf(re);
            }
        }
  
            return shizi;//返回字符串
    }
    }
%>
  <%
    
    Connection con = null; //定義一個MYSQL連接對象
    Class.forName("com.mysql.jdbc.Driver").newInstance(); //MYSQL驅動
    con = DriverManager.getConnection("jdbc:mysql://localhost:3306/jaovo_msg", "root", "root"); //連接本地MYSQL
    java.sql.Statement stmt; //建立聲明
    stmt = con.createStatement();//建立傳輸對象
    stmt.executeUpdate("delete from 四則運算題目2");
    int max=Integer.parseInt(String.valueOf(session.getAttribute("max")));//轉換max爲int
    int min=Integer.parseInt(String.valueOf(session.getAttribute("min")));//轉換min爲int
    int choose=Integer.parseInt(String.valueOf(session.getAttribute("choose")));//轉choose
    int geshu=Integer.parseInt(String.valueOf(session.getAttribute("geshu")));//轉換geshu
    session.setAttribute("geshu1", geshu);//得到session的值
    //String daan=request.getParameter("daan");
    int zhengque1=0;//對答案正確性做標記
    int cuowu1=0;//對答案錯誤做標記
    /**
    疑問:
    
    *表示正確的數組的長度是個數+10
    *表示錯誤的數組的長度是個數+10
     
    */
    int zhengque4[]=new int[geshu+10];//?
    int cuowu4[]=new int[geshu+10];
   Zhengshu a = new Zhengshu();//整數類製造對象,能夠返回表達試
    //for(int i=0;i<geshu;i++)
    %>
          
          
      <%
    //if(i<geshu)//i的初值爲零,知足條件
    for(int j=0;j<geshu;j++)
    {
 
        Array[i]=a.function(max,min,choose);//返回第一個字符串(字符串)
        
        if(i>0)//不知足條件,i的初值爲零
        {
            for(int z=0;z<i;z++)
            {
                if((Array[z].equals(Array[i])))
                {
                    Array[i]=a.function(max,min,choose);
                    z=-1;
                }    
            }
        }
  
        out.println(i+1+": "+Array[i]+" = "+'\n'); //輸出i+1表示第幾道題;輸出表達式加上「=」    1+1 =
         
        %>
          <% //將題目和答案寫入數據庫表
        String sql="INSERT INTO 四則運算題目2 (shizi,result) VALUES ('"+Array[i]+" = "+"','"+a.re1+"')";
        //out.println("<script language = 'javaScript'> alert('"+shi+"');</script>");
        stmt.executeUpdate(sql);//執行sql語句、
        
        i++;
        }
              
             
              response.setHeader("refresh","0;url =disanjiemian.jsp");
        %>
  
          <br>
          
</div>
        <form id="form1" name="form1" method="post" action="disanjiemian.jsp">
       
          <p>&nbsp;</p>
          <p align="center">答案:
          <input type="text" name="daan" id="daan" />
        <% 
        String shi3=Array[i]+" = ";
        session.setAttribute("shi",shi3);
        String rere3=a.re1;
        session.setAttribute("rere",rere3);
        //String daan3=request.getParameter("daan");
        //session.setAttribute("daan",daan3);
        session.setAttribute("i2",i);
        
        //i++;
      
        stmt.close();
        con.close();
         /**
        *把第一個式子放到數據庫,而後在zhengshusjk.jsp中比較結果
        *array不包括「=」
        *輸入答案
        *此時已將array存入數據庫
        *再把array+「=」存入shi字符串
        *把shi放到session裏
        *String型的結果放到rere3
        *把rere3放到session裏
        *把i放到session裏
        *i+1
        *提交
        */
        %>
        <input type="submit" value="下一個" >
          </p>
        </form>
        
        <div align="center">
          <%
        //String daan3=request.getParameter("daan");

        //daan3=request.getParameter("daan");
        //session.setAttribute("daan",daan3); 
        
        %>
          }
        <%/*
  }
    else
    {
        i=0;                                                                                                                                                                                                                                                                                                                                                        
        */%>
        
</div>
        <%/* <form action="chakan.jsp" method="post" >*/%>
          <div align="center">
            <% 
      //  String correct=String.valueOf(session.getAttribute("correct1"));
       // String wrong=String.valueOf(session.getAttribute("wrong1"));
        %>
          <%   /*<input type="submit" value="查當作績" name="button" align="middle"> */%>
          </div>
        </form>
       <%/* <div align="center"> */%>
          <%/*
    }
    
   
    */%>
        
          
</div>
</body>
</html>
/**chakan*/
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8" import="java.sql.Connection,java.sql.DriverManager,java.sql.ResultSet"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<style type="text/css">
body,td,th {
    font-size: 24px;
}
body {
    background-image:url("../images/5.jpg");
    background-repeat:no-repeat;
    background-size:130% auto;
}
</style>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>整數式子</title>

</head>
<body>
     <table align="center" border="1" width="600" style="width: 624px;">
         <tr>
         <td width="250"><%="式子"%></td>
         <td width="150"><%="答案"%></td>
         <td width="150"><%="正確答案"%></td>
         <td width="50"><%="結果"%></td>
         </tr>
         </table>
<%
 
    Connection con = null; //定義一個MYSQL連接對象
    Class.forName("com.mysql.jdbc.Driver").newInstance(); //MYSQL驅動
    con = DriverManager.getConnection("jdbc:mysql://localhost:3306/jaovo_msg", "root", "root"); //連接本地MYSQL
    java.sql.Statement stmt; //建立聲明
    stmt = con.createStatement();//建立傳輸對象 <br>
    String sql2 = "select result from 四則運算題目3";
    ResultSet resultSet = null;
    resultSet= stmt.executeQuery(sql2);
    String anw[]=new String[1000];
    int p=0;
    while(resultSet.next()){
      anw[p]=resultSet.getString("result");
      p++;
      }

String an[]=request.getParameterValues("anwser");
Connection conn = null; //定義一個MYSQL連接對象
    Class.forName("com.mysql.jdbc.Driver").newInstance(); //MYSQL驅動
    conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/jaovo_msg", "root", "root"); //連接本地MYSQL
    java.sql.Statement stmtn; //建立聲明
    stmtn = conn.createStatement();//建立傳輸對象 <br>
    String sql3 = "select shizi from 四則運算題目3";
    ResultSet resultSetn = null;
    resultSetn= stmtn.executeQuery(sql3);
     int i=0;
     int jud1=0;//判斷對的式子個數
     int jud2=0;//判斷錯的式子個數
while(resultSetn.next()){
      String anwser="";    
      String ar;
      String jud;
      ar=resultSetn.getString("shizi");
      if(an[i].equals(anw[i]))
      {
           jud="√";
           jud1++;
      }
      else{
         jud="×";
         jud2++;
      }
     %>
     <table align="center" border="1" width="600" style="width: 624px; ">
         <tr>
         <td width="250"><%=ar %></td>
         <td width="150"><%=an[i]%></td>
         <td width="150"><%=anw[i]%></td>
         <td width="50"><%=jud%></td>
         </tr>
         </table>
     <% 
         i++;
           }
    %>
    <font face="楷體_GB2312" color="blue" size="+3">你一共對了<a><%=jud1 %></a>道題!!!</font><br>

</body>
</html>
/**choose*/
<%@ page contentType = "text/html; charset=utf-8" import = "java.sql.*" errorPage = "error.jsp" %><%//java.sql %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>選擇</title>
<style>
body {
  background: #222;
  margin: auto;
  width: 960px;
}

.arc-text {
  font: 16px sans-serif;
}

.arc-center {
  fill: none;
}

#credit {
  position: absolute;
  font: 10px sans-serif;
  right: 10px;
  bottom: 10px;
  color: #ddd;
}

#credit a {
  color: inherit;
}

</style>
<div id="credit">Inspired by <a href="http://blog.pixelbreaker.com/polarclock/">pixelbreaker</a>.</div>
<script src="//d3js.org/d3.v3.min.js"></script>
<script>

var width = 960,
    height = 800,
    radius = Math.min(width, height) / 1.9,
    spacing = .09;

var formatSecond = d3.time.format("%-S seconds"),
    formatMinute = d3.time.format("%-M minutes"),
    formatHour = d3.time.format("%-H hours"),
    formatDay = d3.time.format("%A"),
    formatDate = function(d) { d = d.getDate(); 
    switch (10 <= d && d <= 19 ? 10 : d % 10) { case 1: d += "st"; break; case 2: d += "nd"; break; case 3: d += "rd"; break; default: d += "th"; break; } return d; },
    formatMonth = d3.time.format("%B");

var color = d3.scale.linear()
    .range(["hsl(-180,60%,50%)", "hsl(180,60%,50%)"])
    .interpolate(function(a, b) { var i = d3.interpolateString(a, b); return function(t) { return d3.hsl(i(t)); }; });

var arcBody = d3.svg.arc()
    .startAngle(0)
    .endAngle(function(d) { return d.value * 2 * Math.PI; })
    .innerRadius(function(d) { return d.index * radius; })
    .outerRadius(function(d) { return (d.index + spacing) * radius; })
    .cornerRadius(6);

var arcCenter = d3.svg.arc()
    .startAngle(0)
    .endAngle(function(d) { return d.value * 2 * Math.PI; })
    .innerRadius(function(d) { return (d.index + spacing / 2) * radius; })
    .outerRadius(function(d) { return (d.index + spacing / 2) * radius; });

var svg = d3.select("body").append("svg")
    .attr("width", width)
    .attr("height", height)
  .append("g")
    .attr("transform", "translate(" + width / 2 + "," + height / 2 + ")");

var field = svg.selectAll("g")
    .data(fields)
  .enter().append("g");

field.append("path")
    .attr("class", "arc-body");

field.append("path")
    .attr("id", function(d, i) { return "arc-center-" + i; })
    .attr("class", "arc-center");

field.append("text")
    .attr("dy", ".35em")
    .attr("dx", ".75em")
    .style("text-anchor", "start")
  .append("textPath")
    .attr("startOffset", "50%")
    .attr("class", "arc-text")
    .attr("xlink:href", function(d, i) { return "#arc-center-" + i; });

tick();

d3.select(self.frameElement).style("height", height + "px");

function tick() {
  if (!document.hidden) field
      .each(function(d) { this._value = d.value; })
      .data(fields)
      .each(function(d) { d.previousValue = this._value; })
    .transition()
      .ease("elastic")
      .duration(500)
      .each(fieldTransition);

  setTimeout(tick, 1000 - Date.now() % 1000);
}

function fieldTransition() {
  var field = d3.select(this).transition();

  field.select(".arc-body")
      .attrTween("d", arcTween(arcBody))
      .style("fill", function(d) { return color(d.value); });

  field.select(".arc-center")
      .attrTween("d", arcTween(arcCenter));

  field.select(".arc-text")
      .text(function(d) { return d.text; });
}

function arcTween(arc) {
  return function(d) {
    var i = d3.interpolateNumber(d.previousValue, d.value);
    return function(t) {
      d.value = i(t);
      return arc(d);
    };
  };
}

function fields() {
  var now = new Date;
  return [
    {index: .7, text: formatSecond(now), value: now.getSeconds() / 60},
    {index: .6, text: formatMinute(now), value: now.getMinutes() / 60},
    {index: .5, text: formatHour(now),   value: now.getHours() / 24},
    {index: .3, text: formatDay(now),    value: now.getDay() / 7},
    {index: .2, text: formatDate(now),   value: (now.getDate() - 1) / (32 - new Date(now.getYear(), now.getMonth(), 32).getDate())},
    {index: .1, text: formatMonth(now),  value: now.getMonth() / 12}
  ];
}

</script>
<style type="text/css">
body,td,th {
    font-size: 24px;
}
body {
    background-image:url("../images/0.gif");
    background-repeat:no-repeat;
    background-size:100% 100%;
}
<style>
<div {
    background-image:url("../images/0.gif");
    background-repeat:no-repeat;
    background-size:100% 100%;
    }
</style>
</style>
</head>
<body>
  <form id="form1" name="form1" method="post" action="zhengshu.jsp">
<% /*<form id="myForm" name="myForm"*>*/%>
<div align="center">
  <input type="submit" value="開始答題" onClick="zhengshu.jsp"></p><br><br>
 
</div>
</form>
<script>
function check(){
     var input = document.getElementsByName("RadioGroup1");
     for(var i=0; i< input.length; i++ ){
         if(input[i].checked == true ){
              //window.open(input[i].value,null); //新窗口打開
               window.location=input[i].value; //當前窗口打開,註釋一種方法
          }
     }
}
</script>
</body>
</html>
/**disanjiemian*/
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8" import="java.sql.Connection,java.sql.DriverManager,java.sql.ResultSet"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<style type="text/css">
body,td,th {
    font-size: 24px;
}
body {
    background-image:url("../images/4.jpg");
    background-repeat:no-repeat;
    background-size:130% auto;
}
</style>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>整數式子</title>
</head>
  
  <body>
  <form action="chakan.jsp" method="post">
      
  <% 
     Connection con = null; //定義一個MYSQL連接對象
    Class.forName("com.mysql.jdbc.Driver").newInstance(); //MYSQL驅動
    con = DriverManager.getConnection("jdbc:mysql://localhost:3306/jaovo_msg", "root", "root"); //連接本地MYSQL
    java.sql.Statement stmt; //建立聲明
    stmt = con.createStatement();//建立傳輸對象 <br>
    String sql2 = "select shizi from 四則運算題目3";
    ResultSet resultSet = null;
    resultSet= stmt.executeQuery(sql2);
    //String array[]=new String[1000];
    %>
         <table align="center" border="1"width="600" style="width: 624px;">
         <tr>
         <td width="250"><%="式子"%></td>
         <td width="150"><%="答案"%></td>
         <td width="150"><%="正確答案"%></td>
         <td width="50"><%="結果"%></td>
         </tr>
         </table>
    <% 
    while(resultSet.next()){
      String anwser="";
      int i=0;
      String ar;
      ar=resultSet.getString("shizi");
      i++;
      
     %>
     <table align="center" border="1" width="600" style="width: 624px;">
         <tr>
         <td width="250"><%=ar %></td>
         <td width="150"><input type="text" name="anwser" autocomplete="off" /></td>
         <td width="150"></td>
         <td width="50"></td>
         </tr>
         </table>
     <% 
         }
    %>
    <tr align="center">
                <td colspan="2">
                    
                </td>
    </tr>
    <div align="center">
        <input type="submit" value="提交" style="height: 41px; width: 81px; "></div>  
  </body>
</html>
/**zhengshu*/<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8" import="java.sql.Connection,java.sql.DriverManager"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<style type="text/css">
body,td,th {
    font-size: 24px;
}
body {
    background-image:url("../images/1.gif");
    background-repeat:no-repeat;
    background-size:auto auto;
}
</style>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>整數</title>

</head>
<body style="width: 1333px; ">
<form id="form1" name="form1" method="post" action="zhengshushizi.jsp" style="height: 469px; width: 1353px; ">
  <p>
    <label for="textfield"></label>
  </p>
  <p>&nbsp;</p>
  <p>&nbsp;</p>
  <p>&nbsp;</p>
  <p align="center" style="width: 1242px; height: 78px"><br>
    題目個數:<input type="text" name="geshu" autocomplete="off" id="geshu">
    <br>
  </p>
  <p>&nbsp;</p>
  
   <div align="center">
<input type="submit" value="肯定" value="zhengshushizi.jsp">
</div>
</form>
</body>
</html>
/**zhengshushizi*/
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8" import="java.sql.Connection,java.sql.DriverManager,java.sql.ResultSet"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>整數式子</title>
<style type="text/css">
body,td,th {
    font-size: 24px;
}
body {
    background-color: #0CF;
    background-image: url(a.jpg);
}
</style>
</head>
<body>
<div align="center">
  <%!
    int i=0;
    String Array[]=new String[1000];//array 表達式
    class Zhengshu//整數類
    {
    String re1="";//初始化 「結果」 String類型的
    int re;
    String function(int r ,int q, int fun)// r最小值,q最大值,fun 選擇
    {
        //1有乘除法 2加減無負數 3除法無餘數(12,1,0)
        String fu ="";
        String shizi="";
        int a=(int)(Math.random()*(r)%(r-q+1)+q);
        int b=(int)(Math.random()*(r)%(r-q+1)+q);
        int c=(int)(Math.random()*4);
        if(fun==1)
        {
            if(c==0)
            {
             while(a+b>100){
                    a=(int)(Math.random()*(r)%(r-q+1)+q);
                    b=(int)(Math.random()*(r)%(r-q+1)+q);
                }
                fu="+";
                shizi=a+" "+fu+" "+b;
                re=a+b;
                re1=String.valueOf(re);
            }
            if(c==1)
            {     
                fu="-";
                //排除負數可能性
                while(a-b<0)
                {
                    a=(int)(Math.random()*(r)%(r-q+1)+q);
                    b=(int)(Math.random()*(r)%(r-q+1)+q);
                }
                shizi=a+" "+fu+" "+b;
                re=a-b;
                re1=String.valueOf(re);
            }
            if(c==2)
            {
            while(a>10||b>10){
                    a=(int)(Math.random()*(r)%(r-q+1)+q);
                    b=(int)(Math.random()*(r)%(r-q+1)+q);
                }
                fu="×";
                shizi=a+" "+fu+" "+b;
                re=a*b;
                re1=String.valueOf(re);
            }
            if(c==3)
            {
            while(a>10||b>10){
                    a=(int)(Math.random()*(r)%(r-q+1)+q);
                    b=(int)(Math.random()*(r)%(r-q+1)+q);
                }
                fu="÷";
                //排除分母爲0的可能性和餘數的可能性
                while(b==0||a==0)
                {
                    a=(int)(Math.random()*(r)%(r-q+1)+q);
                    b=(int)(Math.random()*(r)%(r-q+1)+q);
                }    
                re=a;
                shizi=a*b+" "+fu+" "+b;
                re1=String.valueOf(re);
            }
        }
            return shizi;//返回字符串
    }
    }
%>
  <%
    
    Connection con = null; //定義一個MYSQL連接對象
    Class.forName("com.mysql.jdbc.Driver").newInstance(); //MYSQL驅動
    con = DriverManager.getConnection("jdbc:mysql://localhost:3306/jaovo_msg", "root", "root"); //連接本地MYSQL
    java.sql.Statement stmt; //建立聲明
    stmt = con.createStatement();//建立傳輸對象
    stmt.executeUpdate("delete from 四則運算題目3");
   // stmt.executeUpdate("delete from 四則運算題目2");
   /* int max=Integer.parseInt(String.valueOf(session.getAttribute("max")));//轉換max爲int
    int min=Integer.parseInt(String.valueOf(session.getAttribute("min")));//轉換min爲int
    int choose=Integer.parseInt(String.valueOf(session.getAttribute("choose")));//轉choose*/
    /*int geshu=Integer.parseInt(String.valueOf(session.getAttribute("geshu")));//轉換geshu
    session.setAttribute("geshu1", geshu);//得到session的值*/
    //String daan=request.getParameter("daan");
   /* int zhengque1=0;//對答案正確性做標記
    int cuowu1=0;//對答案錯誤做標記*/
    /**
    疑問:
    
    *表示正確的數組的長度是個數+10
    *表示錯誤的數組的長度是個數+10
     
    */
    int geshu=Integer.parseInt(request.getParameter("geshu"));
    int zhengque4[]=new int[geshu+10];//?
    int cuowu4[]=new int[geshu+10];
    Zhengshu a = new Zhengshu();//整數類製造對象,能夠返回表達試
    //for(int i=0;i<geshu;i++)
    %>
          
          
      <%
    //if(i<geshu)//i的初值爲零,知足條件
    for(int j=0;j<geshu;j++)
    {
 
        Array[i]=a.function(1,100,1);//返回第一個字符串(字符串)
        
        if(i>0)//不知足條件,i的初值爲零
        {
            for(int z=0;z<i;z++)
            {
                if((Array[z].equals(Array[i])))
                {
                    Array[i]=a.function(100,1,1);
                    z=-1;
                }    
            }
        }
  
        //out.println(i+1+": "+Array[i]+" = "+'\n'); //輸出i+1表示第幾道題;輸出表達式加上「=」    1+1 =
         
        %>
          <% //將題目和答案寫入數據庫表
        String sql="INSERT INTO 四則運算題目3 (shizi,result) VALUES ('"+Array[i]+" = "+"','"+a.re1+"')";
        //out.println("<script language = 'javaScript'> alert('"+shi+"');</script>");
        stmt.executeUpdate(sql);//執行sql語句、
        
        i++;
        }
              
             
              response.setHeader("refresh","0;url =disanjiemian.jsp");
        %>
  
          <br>
          
</div>
        
        stmt.close();
        con.close();
       
          
       
        
</body>
</html>

 

3.運行結果截圖sql

 

4.編程總結分析數據庫

學會了前端設計  前端有點難啊編程

相關文章
相關標籤/搜索