03四则运算

Posted 肥鹅PU火

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了03四则运算相关的知识,希望对你有一定的参考价值。

 

package pers.sun.operateion;

public class Operated {

   private int resultx;
   private String formulax;
 
 public int getResultx() {
    return resultx;
 }
 
 public void setResultx(int resultx) {
    this.resultx = resultx;
 }
 public String getFormulax() {
    return formulax;
 }
 public void setFormulax(String formula) {
    this.formulax=formula;
 }
 public String calculation() {
  
    int first=(int) (Math.random()*10+1);
    int second=(int) (Math.random()*10+1);
    int op=(int) (Math.random()*4+1);
    char operator = 0;
    switch(op) {
    case 1:operator=\'+\';resultx=first+second;break;
    case 2:operator=\'-\';resultx=first-second;break;
    case 3:operator=\'*\';resultx=first*second;break;
    case 4:operator=\'/\';break;
    }
  //是除
  if(op==4) {
   //分母不为0 且能除尽
   if(second!=0) {
    int res=first%second;
    if(res==0) {
       formulax=first+" "+operator+" "+second+" =";
       resultx=first/second;
    }
    else
       formulax=null;
   }
   else
      formulax=null;
  }
  //不是除
  else {
     formulax=first+" "+operator+" "+second+" =";
  }
    return formulax;
 }
}

package pers.sun.operateion;

import pers.sun.operateion.Operated;
//产生N个算式,及结果
public class ApplyIt {

 private int[] result;
 
 public String[] make(int n) {
  //接收的容器
           
  String formulas[]=new String[n];
  result=new int[n];
  
  //产生算式
  Operated opera=new Operated();
  for(int i=0;i<n;) {
   String temp=opera.calculation();
   //判断算式是否为null
   if(temp!=null) {
          
    formulas[i]=temp;
    result[i]=opera.getResultx();
    i++;
   }
  }
  return formulas;
 }
 
 
 public int[] getResult() {
  return result;
 }
 public void setResult(int[] result) {
  this.result = result;
 }
}

package pers.sun.sql;

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

public class DB {

 public static Connection getConnection() {
  try {
   Class.forName("com.mysql.jdbc.Driver");
  } catch (ClassNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  String root="root";
  String password="sunyu";
  String url="jdbc:mysql://localhost:3306/user_message";
  
  Connection con=null;
  try {
   con=DriverManager.getConnection(url,root,password);
  } catch (SQLException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  
  return con;
 }
 
 public static void close(Connection con) {
  try {
   if(con!=null)
    con.close();
  } catch (SQLException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  
 }
 public static void close(PreparedStatement pre) {
  try {
   if(pre!=null)
    pre.close();
  } catch (SQLException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
 public static void close(ResultSet result) {
  try {
   if(result!=null)
    result.close();
  } catch (SQLException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
}

package pers.sun.sql;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import pers.sun.operateion.Operated;

public class SqlTool {
 
 public static void add(String formula,int result) {
  if(formula!=null) {
   String sql="insert into math(formula,result) value(?,?)";
   Connection connection=DB.getConnection();
   PreparedStatement preparedstatement=null;
   try {
    preparedstatement=connection.prepareStatement(sql);
    preparedstatement.setString(1,formula);
    preparedstatement.setInt(2,result);
    preparedstatement.executeUpdate();
    
   } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }finally {
    DB.close(preparedstatement);
    DB.close(connection);
   }
   
  }
 }
 public static void deleted() {
  
 }
 public static List<Operated> search() {
  
  Connection connection=DB.getConnection();
  String sql="select * from math";
  PreparedStatement pre=null;
  ResultSet result=null;
  
  List<Operated> op= new ArrayList<Operated>();
  
  try {
   pre = connection.prepareStatement(sql);
   result=pre.executeQuery();
   while(result.next()) {
    Operated temp=new Operated();
    temp.setResultx(result.getInt("result"));
    temp.setFormulax(result.getString("formula"));
    op.add(temp);
   }
   
  } catch (SQLException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  return op;
  
 }
 public static void updata() {
  
 }
}

JSP文件

<%@ page language="java" contentType="text/html; 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>
<body>
 <form action="outputbegin.jsp" method="post">
  <table align="center" border="1" width="100">
   <tr>
   <!-- 没有判断输入的是否为整数 或 不为空 -->
    <td>输入你要做的题</td>
   </tr>
   <tr>
    <td>
     <input type="text" name="number" />
    </td>
   </tr>
   <tr>
    <td>
     <input type="submit" value="确定" name="submit" />
     <input type="reset" value="重置" name="reset" />
    </td>
   </tr>
  </table>
  
 </form>
</body>
</html>

<%@page import="pers.sun.sql.SqlTool"%>
<%@ page language="java" contentType="text/html; UTF-8"
    pageEncoding="UTF-8"%>
<%@page import="pers.sun.util.*" %>
<%@page import="pers.sun.operateion.*" %>
<%@page import="java.util.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<!-- 接收用户数字-->
<html>
<head>
 <title>算式表格</title>
</head>
<body>
<%
 //接收信息
 int num=Integer.parseInt(request.getParameter("number"));
 ApplyIt apply =new ApplyIt();
 //生成算式+结果
 String[] suanshi=apply.make(num);
 int rightresults[]=apply.getResult();
 
 //写入数据库
 for(int i=0;i<num;i++){
  SqlTool.add(suanshi[i], rightresults[i]);
 }
%>
 <form action="handle.jsp" method="post">
 <table align="center" border="1" width="500">
  <tr>
   <td>算式</td>
   <td>结果</td>
  </tr>
  <%
   for(String stemp:suanshi){
  %> 
  <tr>
   <td><%=stemp %></td>
   <td><input type="text" value="" name="peoresult" /></td>
  </tr>
  <%
   }
  %>
  <tr>
    <td><input type="submit" value="提交" name="submit" /></td>
  </tr>
  </table>
 </form>

</body>

<%@page import="pers.sun.sql.SqlTool"%>
<%@page import="pers.sun.operateion.Operated"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@page import="java.util.*"%>
<!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>
<body>
  
  <ul>
   <li> <a href="begin.jsp" >重来</a>&nbsp;&nbsp;</li>
   <li> <a href="ending.jsp">结束</a></li>
  </ul>
  <hr>
  <table align="center" border="1" width="500">
   <tr>
    <td>算式</td>
    <td>你的答案</td>
    <td>对错</td>
    <td>正确答案</td>
   </tr>
<%
  List<Operated> operation=new ArrayList<Operated>();
  int length=operation.size();
  //String[] peoresult=new String[length];
  //peoresult=request.getParameterValues("peoresult");
  operation=SqlTool.search();
  int count=0;
  System.out.print(request.getParameter("peoresult"));
  for(Operated temp:operation){
   int i=0;
   String peoresultx=request.getParameter("peoresult");
%>
  <tr>
   <td><%=temp.getFormulax() %></td>
   <td><%=peoresultx %></td>
   <%
   String correct=null;
   if(peoresultx!=null&&!"".equals(peoresultx.trim())){
    if(Integer.parseInt(peoresultx)==temp.getResultx()){
     correct="对";
     count++;
    }
    else
     correct="错";
   }
   %>
   <td><%=correct %></td>
   <td><%=temp.getResultx() %></td>
  </tr>
<%  
  }
%> 
   <tr><td>你的得分:<%=count %></td></tr>
  </table>

</body>
</ht

运行结果截图:

 

 

以上是关于03四则运算的主要内容,如果未能解决你的问题,请参考以下文章

四则运算设计03

03四则运算

Python-03-Python运算符

03.运算符与表达式

03.JS运算符

软件工程个人作业03-网页版四则运算