• 四则运算2


    程序设计思想:利用随机数产生随机四则运算式子,然后检验正误

    程序源代码:

    chutishumu.jsp

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!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>
    </head>
    
    <h1 style="font-family:新蒂黑板报底字 ; font-size:5em">选择题目吧</h1>
    
        <form action="Chuti.jsp"  method="post">
        <table align="center" border="0" width="500" style="margin:00px 200px 00px 5px">
        <tr>
            <td style="font-size:2em;500px"  align="right">你想做几道题? </td>
            <td>
                <input style="100px;height:30px;" type="text" name="username" />
            </td>
        </tr>
        
        <tr>
            <td style="font-size:2em;500px"  align="right">设置时间: </td>
            <td>
                <input style="100px;height:30px;" type="text" name="usertime" />
                </td>
                <td style="font-size:2em;500px"  align="right">秒 </td>
        </tr>
        
        <tr><td style="150px;height:40px;"></td></tr> <!-- 加了一个自己设置的高度的空行 -->
        <tr align="center">
        <td colspan="2">
            <input  style="100px;height:30px;  margin:00px 20px 00px 150px" type="submit" value="我选好了" />
    </body>
    </html>
    <%@page import="com.jaovo.msg.model.arithmetic"%>
    <%@ page import="javax.swing.*" %>
    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head><title>出题页</title></head>
    <body background="./images/tu2.jpg">
    <%
      //接收客户端传递过来的参数
        request.setCharacterEncoding("UTF-8");
        String time = request.getParameter("usertime");//接收时间
        int time1=0;
        int x=1;
        for(int m=0;m<time.length();m++)
        {
            time1+=(time.charAt(time.length()-m-1)-'0')*x;
            x*=10;
        }//字符串类型的数字转换为整型  成为参数
    %>
    <script>
    var c=1;
    var t;
    var num1=<%=time1%>
    function timeCount()
    {
        document.getElementById("txt").innerHTML=num1-c;
        c=c+1;
        t=setTimeout("timeCount()",1000);
        if(num1==c-1)
        {
            clearTimeout(t);
            alert("时间到了!");
            load();
        }
    }
    function load(){  
            document.getElementById("anniu").click();  
    }
    window.onload =function(){
        timeCount();//onload 事件会在页面或图像加载完成后立即发生。
    }
    
    </script>
    <h2 align="center" style="font-family:新蒂黑板报底字 ; font-size:4em">开始答题</h2>
    <td style="font-size:1em;500px"  align="right">倒计时:</td>
    <p id = "txt"></p>
    <form action="Result.jsp" onsubmit="return validate()==1" method="get">
    
    <%
      //接收客户端传递过来的参数
        request.setCharacterEncoding("UTF-8");
        String num = request.getParameter("username");//接收出题的数目
        int num1=0;
          x=1;
        for(int m=0;m<num.length();m++)
        {
            num1+=(num.charAt(num.length()-m-1)-'0')*x;
            x*=10;
        }//字符串类型的数字转换为整型  成为参数
        
        arithmetic demo=new arithmetic();//定义对象
        String []biaodashi1=new String[num1];
        biaodashi1=demo.biaodashi(num1);//接收算式
        demo.setsuanshi(biaodashi1);//调用函数 给数据成员算式赋值 以便用于传递
        
        for(int i=0;i<num1;i++)
        {
    %>
    <table align="center" border="0" width="600">
        <tr>
            <td>
            <% 
                out.println(biaodashi1[i]);//输出表达式
            %>
            </td>
            <td>
                 <input  type="text" name="result[<%=i%>]"/>  
             </td> 
         </tr>                     
     <% 
        }
        session.setAttribute("jieshou",demo);
    %>
    </table>
     <input type="submit" value="交卷" /> 
    
    
    
    </body>
    </html>

    result.jsp

    <%@page import="com.jaovo.msg.model.arithmetic"%>
    <%@ page import="javax.swing.*" %>
    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head><title>出题</title></head>
    
    <body background="./images/tu3.jpg">
    <h1 align="center"  style="font-family:新蒂黑板报底字 ; font-size:5em">答题结果</h1>
    <table align="center" border="0" width="600">
    <%
      //接收客户端传递过来的参数
        arithmetic newdemo=new arithmetic();
        newdemo=(arithmetic)session.getAttribute("jieshou");//用于接收CHUti界面传过来的数 (对象)
        String []yoursolution=new String[newdemo.shumu];//接收传过来的文本框的答案
        int sumright=0,sumerror=0,empty=0;
        for(int i=0;i<newdemo.shumu;i++)
        {
            request.setCharacterEncoding("UTF-8");
            %>
            <tr>
            
            <% 
                out.print(newdemo.suanshi[i]);//算式
                yoursolution[i] = request.getParameter("result["+i+"]");//你的答案
                out.println(yoursolution[i]);
            %>
           
            
            
            <% 
                out.println("正确答案是:             ");
                out.println(newdemo.answer[i]);//正确的答案
            %>
            
            <% 
            int num1=0;
            int x=1;
            for(int m=0;m<yoursolution[i].length();m++)
            {
                num1+=(yoursolution[i].charAt(yoursolution[i].length()-m-1)-'0')*x;
                x*=10;
            }//字符串类型的数字转换为整型  用于和正确答案比较 因为从出题界面接受的答案是字符串类型
            if(yoursolution[i].equals(""))
            {
                out.println("你没有回答哦!");
                empty++;
            }
            else if(num1==newdemo.answer[i])
            {
                sumright++;
                out.println("恭喜你!回答正确!");
            }
            else
            {
                sumerror++;
                out.println("回答错误,再接再厉!");
            }
    %>        
    </tr><br>
        <% 
        }
        %>
        <tr>
        <td>
        <% 
            out.println("回答正确了"+sumright+"道题!");
        %>
        </td>
        </tr>
        <tr>
            <td>
            <% 
                out.println("回答错误了"+sumerror+"道题!");
            %>
            </td>
        </tr>
        <tr>
            <td>
            <% 
                out.println("有"+empty+"道题你没有回答哟!");
            %>
            </td>
        </tr>
    <a href="chutishumu.jsp">退出</a>
    </body>
    </html>

    arithmetic.java

    package com.jaovo.msg.model;
    
    public class arithmetic {
        public int []answer;//��
        public int shumu;//������Ŀ
        public String []suanshi;//��ʽ
        public void setsuanshi(String []suanshi)
        {
            this.suanshi=suanshi;
        }
                
        public String [] biaodashi(int n)
        {
            shumu=n;
            answer=new int[n];
            int a,b,c,d1 = 0,d,d2=0;
            int []mixture=new int[2];
            String []biaodashi=new String[n];
            
            for(int i=0;i<n;i++)
            {
                 a=(int)(Math.random()*100)+1;//1-100
                 b=(int)(Math.random()*100)+1;
                 c=(int)(Math.random()*5)+1;//�������һ��1-5��������4��ʾ�ӷ���1��ʾ������2��ʾ�˷���3��ʾ����,5��ʾ���
                 if(c==5)//�������
                 {
                     do
                     {     
                         for(int m=0;m<2;m++)
                         {
                             mixture[m]=(int)(Math.random()*2);//0-1
                         }//���������
    
                         a=(int)(Math.random()*100)+1;
                         b=(int)(Math.random()*100)+1;
                         d=(int)(Math.random()*100)+1;//����������
                         if(mixture[0]==0&&mixture[1]==0)
                         {
                             biaodashi[i]=a+"+"+b+"+"+d+"  = ";
                             d1=a+b+d;
                         }
                         if(mixture[0]==1&&mixture[1]==1)
                         {
                             biaodashi[i]=a+"-"+b+"-"+d+"  = ";
                             d2=a-b;
                             d1=a-b-d;
                         }
                         if(mixture[0]==0&&mixture[1]==1)
                         {
                             biaodashi[i]=a+"+"+b+"-"+d+"  = ";
                             d1=a+b-d;
                         }
                         if(mixture[0]==1&&mixture[1]==0)
                         {
                             biaodashi[i]=a+"-"+b+"+"+d+"  = ";
                             d2=a-b;
                             d1=a-b+d;
                         }
                     }    while(d2<0||d1<0); 
                     answer[i]=d1;
                 }
                  if(c==4)//���ӷ�
                  {
                      d1=a+b;
                      biaodashi[i]=a+"+"+b+"  = ";
                      while(d1>100)
                      {
                             a=(int)(Math.random()*100)+1;
                             b=(int)(Math.random()*100)+1;//1-100 ����1��100  ����1 ��ʾ0-99
                             d1=a+b;
                      }
                     biaodashi[i]=a+"+"+b+"  = ";
                     answer[i]=d1;
                     System.out.print(a+"+"+b+"=       ");
                  }
                  if(c==1)//������
                  {
                      d1=a-b;
                      while(d1<0)
                      {
                          a=(int)(Math.random()*100)+1;
                          b=(int)(Math.random()*100)+1;
                          d1=a-b;
                      }
                      biaodashi[i]=a+"-"+b+"  = ";
                      answer[i]=d1;
                      System.out.print(a+"-"+b+"=       ");
                  }
                  if(c==2)//�˷�
                  {
                      a=(int)(Math.random()*10);//0-9
                      b=(int)(Math.random()*10);//1-100 ����1��100  ����1 ��ʾ0-99
                      d1=a*b;
                      while(a<1||b<1||d1>81)
                      {
                          a=(int)(Math.random()*10);//0-9
                          b=(int)(Math.random()*10);//1-100 ����1��100  ����1 ��ʾ0-99
                      }
                      d1=a*b;
                      biaodashi[i]=a+"*"+b+"  = ";
                      answer[i]=d1;
                      System.out.print(a+"*"+b+"=       ");
                  }
                  if(c==3)//����
                  {
                      d1=a/b;
                      while(a%b!=0||a/b>9||(a<=81&&b>=10)||(a>9&&a==b)||(a>81))
                      {
                          a=(int)(Math.random()*100)+1;
                          b=(int)(Math.random()*100)+1;//1-100 ����1��100  ����1 ��ʾ0-99
                      }
                        d1=a/b;
                         biaodashi[i]=a+"÷"+b+"  = ";
                         answer[i]=d1;
                         System.out.print(a+"÷"+b+"=       ");
                  }
                  
                  //����
                  for(int k=i-1;k>=0;k--)
                  {
                      while(biaodashi[i].equals(biaodashi[k]))
                      {
                          a=(int)(Math.random()*100)+1;//1-100
                          b=(int)(Math.random()*100)+1;
                          c=(int)(Math.random()*5)+1;//�������һ��1-5��������4��ʾ�ӷ���1��ʾ������2��ʾ�˷���3��ʾ����,5��ʾ���
                          if(c==5)
                          {
                            do//�������
                            {     
                                for(int m=0;m<2;m++)
                                {
                                     mixture[m]=(int)(Math.random()*2);//0-1
                                 }//���������
    
                                 a=(int)(Math.random()*100)+1;
                                 b=(int)(Math.random()*100)+1;
                                 d=(int)(Math.random()*100)+1;//����������
                                 if(mixture[0]==0&&mixture[1]==0)
                                 {
                                    biaodashi[i]=a+"+"+b+"+"+d+"  = ";
                                    d1=a+b+d;
                                 }
                                 if(mixture[0]==1&&mixture[1]==1)
                                 {
                                     biaodashi[i]=a+"-"+b+"-"+d+"  = ";
                                     d2=a-b;
                                     d1=a-b-d;
                                 }
                                 if(mixture[0]==0&&mixture[1]==1)
                                 {
                                     biaodashi[i]=a+"+"+b+"-"+d+"  = ";
                                     d1=a+b-d;
                                 }
                                 if(mixture[0]==1&&mixture[1]==0)
                                 {
                                     biaodashi[i]=a+"-"+b+"+"+d+"  = ";
                                     d2=a-b;
                                     d1=a-b+d;
                                 }
                             }while(d2<0||d1<0); 
                             answer[i]=d1;
                         }
                         if(c==4)
                         {
                             d1=a+b;
                             biaodashi[i]=a+"+"+b+"  = ";
                             while(d1>100)
                             {
                                 a=(int)(Math.random()*100)+1;
                                 b=(int)(Math.random()*100)+1;//1-100 ����1��100  ����1 ��ʾ0-99
                                 d1=a+b;
                            }
                             biaodashi[i]=a+"+"+b+"  = ";
                             answer[i]=d1;
                             System.out.print(a+"+"+b+"=       ");
                         }
                         if(c==1)
                         {
                              d1=a-b;
                              while(d1<0)
                              {
                                 a=(int)(Math.random()*100)+1;
                                 b=(int)(Math.random()*100)+1;
                                 d1=a-b;
                              }
                             biaodashi[i]=a+"-"+b+"  = ";
                             answer[i]=d1;
                             System.out.print(a+"-"+b+"=       ");
                         }
                         if(c==2)
                         {
                           a=(int)(Math.random()*10);//0-9
                           b=(int)(Math.random()*10);//1-100 ����1��100  ����1 ��ʾ0-99
                           d1=a*b;
                           while(a<1||b<1||d1>81)
                           {
                             a=(int)(Math.random()*10);//0-9
                             b=(int)(Math.random()*10);//1-100 ����1��100  ����1 ��ʾ0-99
                           }
                           d1=a*b;
                           biaodashi[i]=a+"*"+b+"  = ";
                           answer[i]=d1;
                           System.out.print(a+"*"+b+"=       ");
                         }
                        if(c==3)
                        {
                          while(a%b!=0)
                          {
                             a=(int)(Math.random()*100)+1;
                             b=(int)(Math.random()*100)+1;//1-100 ����1��100  ����1 ��ʾ0-99
                          }
                          d1=a/b;
                          biaodashi[i]=a+"÷"+b+"  = ";
                          answer[i]=d1;
                          System.out.print(a+"÷"+b+"=       ");
                        }
                     }
                 }
             }
            return biaodashi;
        }
    
    }

    运行结果截图:

  • 相关阅读:
    查询长事务和SQL执行等待间隔时间
    一条诡异的SQL
    查询表的使用空间和可用空间
    查询SQL Server存储过程的执行信息
    清理大批量数据例子
    sql server 清理日志存储过程
    BCP 实例
    SQL Server 查询Job中的存储过程
    下车扫描五次优化全过程
    empty、isset和is_null的比较
  • 原文地址:https://www.cnblogs.com/tianzeyangblog/p/7995027.html
Copyright © 2020-2023  润新知