软件工程个人作业01

 

 

|=小学生四则运算=|

设计思想:

  1 程序生成数据,并存到数据库Mysql;

  2用户输入数据

  3程序接收数据,并判断数据

  4返回信息。

源程序代码:

java文件

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

 

运行结果截图:

 

 

此处不正确:“你的答案” 出问题。

 

 

 

学习进度:loading............

转载于:https://www.cnblogs.com/floakss/p/7956173.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/267991.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

db设计专用excel_工程师必备:硬件EMC设计规范

一引言广义的电磁兼容控制技术包括抑制干扰源的发射和提高干扰接收器的敏感度&#xff0c;我们都知道干扰源、干扰传输途径和干扰接收器是电磁干扰的三要素&#xff0c;同时EMC也是围绕这些问题进行研究&#xff0c;而运用最为广泛的抑制方法是屏蔽、滤波和接地&#xff0c;用它…

快速手动替换 Windows 7 系统字体

自从发现 GDI 这个能把 Windows 字体显示效果变得更平滑的软件之后&#xff0c;就欲罢不能了。借用网上的一张图片&#xff08;来源未知&#xff09;&#xff0c;对比了 Windows 标准、Windows Cleartype、GDI 和 Mac OS 四种情况下的字体效果&#xff0c;很明显 GDI 的效果非常…

使用说明 思迅收银系统_便利店收银使用的收银系统应该取决于什么?

生活中&#xff0c;我们常见的便利店规模可大可小&#xff0c;比如最小就十平米&#xff0c;大的话也有可能会好几百平米&#xff0c;有些人疑惑&#xff0c;到底便利店该不该使用收银系统&#xff0c;又或者该如何选择合适的收银系统。其实&#xff0c;不管如何&#xff0c;只…

MFC+GDI+绘制出雷达余晖效果

MFCGDI绘制出雷达余晖效果 1.首先要画出静态的坐标轴&#xff0c;用双缓冲方法在onpain消息中绘制。绘制方法都比较简单。声明一个内存DC&#xff0c;绘制一个圆形&#xff0c;再把坐标轴画上去。 void CDlg_RadarScanning::OnPaint() {CPaintDC dc(this); // device context …

如果你们都忘记了鼓励原创,那,我们来 - 2017年度原创IT博主全网评选

2017最后一个月&#xff0c;当全世界都是各种年度总结&#xff0c;奖励的时候&#xff0c;IT博客圈似乎已经被人遗忘。而&#xff0c;那些还在半夜&#xff0c;加班写博客&#xff0c;分享自己经验的热心程序猿们&#xff0c;依然&#xff0c;吭哧吭哧的写着&#xff0c;为了几…

BZOJ 2599

http://www.lydsy.com/JudgeOnline/problem.php?id2599 就是开一个数组t,t[i]表示权值为i的路径最少边数 点分治&#xff0c;找到树的重心分成若干子树后&#xff0c; 得出一棵子树的所有点到根的路径长度x&#xff0c;到根有a条边&#xff0c;用t[k-x]a更新答案&#xff0c; …

android用户界面-菜单

创建菜单 Creating Menus Android提供了三种基础菜单类型&#xff1a; 选项菜单Options Menu 这是一个活动的主菜单。通过按下设备菜单键来显示它。选项菜单包含两组菜单项&#xff1a; 图标菜单Icon Menu 这个是当用户按下菜单键时最初出现屏幕下方的item集合。它支持最多…

QT打开文件

https://www.cnblogs.com/ZY-Dream/p/10571490.html

12.5

持续关注一下足球经理游戏 转载于:https://www.cnblogs.com/dandansang/p/7980132.html

流浪宠物救助网站前端页面_全国爱心人士齐聚鞍山 救助流浪猫狗

入冬之后&#xff0c;流浪猫狗的“吃饭问题”成了众多爱心人士的牵挂。12日上午&#xff0c;来自全国各地的20余位爱心人士&#xff0c;来到宁远镇张忠堡村一流浪猫狗救助站&#xff0c;将价值7000余元的宠物用品捐赠给救助站。当日早上8时许&#xff0c;爱心人士们特意到铁西八…

问题解决

问题解决了&#xff0c;把 #include <string.h> 改写成 #include <string> 就行了 #include <iostream> #include <string> #include "tinystr.h" #include "tinyxml.h" #include <vector> #include "Text.h" #in…

QT 线程之间通信

1.先说QT线程。都知道QT线程有两种使用方式&#xff0c;一种是继承QThread重写run()&#xff0c;一种是继承QObject然后再MovetoThread()到线程下。我也是刚学QT&#xff0c;之前一直做MFC。现在有个问题&#xff0c;我想要创建的是单次运行的线程&#xff0c;就是处理完后就自…

微信小程序 开发过程中遇到的坑(一)

21241.我们使用app.json文件来对微信小程序进行全局配置&#xff0c;决定页面文件的路径、窗口表现、设置网络超时时间、设置多 tab 的时候在pages中写注释的时候回报错。 例如&#xff1a; {"pages":[//这是首页面"pages/welcome/welcome"] } 此时就会报错…

java 创建日程到期提醒_日程管理工具—Things 3

Mac上最好用的日程管理工具是哪个&#xff1f;Things 3 for Mac是首选&#xff0c;Things3 Mac全新的设计&#xff0c;外观简洁、大气&#xff0c;功能也是非常强大的&#xff0c;让你用一种优雅而直观的方式来管理个人事务&#xff0c;无论在生活还是工作中&#xff0c;都能取…

数据库优化小结

WHERE条件的优化&#xff1a;1、IN&#xff1a;至少多一个转换的过程&#xff08;先试图转成多个表的连接&#xff0c;如果转换不成功先执行IN里面的子查询&#xff0c;再查询外层记录&#xff09;2、NOTIN&#xff1a;不能应用表索引&#xff0c;可用NOT EXISTS替换的3、<&…

使用VS制作MFC、libmysql.dll出现计算机缺少msvcr120.dll和msvcp120.dll问题

自己应客户要求做了一个数据库的查询工具&#xff0c;结果在客户电脑那边运行出现了计算机缺少msvcr120.dll和msvcp120.dll问题&#xff0c;一开始以为是MFC程序自身有动态链接&#xff0c;结果才发现是libmysql.dll本身依赖于msvcr120.dll和msvcp120.dll&#xff0c;这两个库对…

python 流程控制if判断

简单看下if判断在python的基本用法 # gyf28 定义变量gyf # if gyf > 20: 判断 gyf 是不是〉20# print(你真年轻) 简单实现了 if判断的用法 下面我们再来做一个例子 if判断 gyf是不是〉18 并且〈 22 如果是 则执行打印的任务 输出 表白 如果条件不成立输入打印 阿姨…

python同步oracle_Python cx_Oracle 7引入苏打文档存储

在线QQ客服&#xff1a;1922638专业的SQL Server、MySQL数据库同步软件cx _甲骨文7 .0是非常受欢迎的蟒蛇甲骨文数据库接口,现在是PyPI上的产品。cx _甲骨文是一个开源包,它涵盖了大蟒数据库应用编程接口规范,并添加了许多支持神谕高级功能的内容。安东尼 图宁加刚刚发布了cx _…

总结2010展望2011

总结2010 展望2011转载于:https://www.cnblogs.com/zhouxiuquan/archive/2011/01/28/1947269.html

XP下使用FFMPEG(API和exe)遇到的问题和解决方法。

XP下使用FFMPEG&#xff08;API和exe&#xff09;遇到的问题和解决方法。1、需求背景2、遇到的问题3、解决方法下载链接1、需求背景 因为最近接到项目上的一个需求&#xff0c;就是在MP4&#xff08;H264编码&#xff09;文件里叠加指定字符&#xff0c;于是就想到了使用FFMPE…