这次的课堂小测是用以前生成的那些四则运算的代码,然后将这些题目写到一个文件中,再通过这个文件读取题目的信息,每读入一个答案的时候,遇到星号的时候,等待用户输入然后判断输入的答案是否正确,然后输出小一道题目让用户输入,直到文件读到结束的位置。

思路大概是如下:

一开始的思路是答案同样也在文本输入,例如1+1=2*;

每次读取到等号的时候,将文件中答案读取出来,并且将变量设置为此值。接下来就能用户输入的答案是否正确了。

//Sizezt.java的代码如下:

  1. package sz;
  2.  
  3. //import java.io.BufferedReader;
  4. import java.io.File;
  5. //import java.io.FileInputStream;
  6. import java.io.FileOutputStream;
  7. import java.io.IOException;
  8. //import java.io.InputStreamReader;
  9. import java.io.OutputStreamWriter;
  10. import java.util.Iterator;
  11. import java.util.Vector;
  12.  
  13. public class Sizezt {
  14.  
  15. //出现运算项的最高个数
  16. private static final int SXSIZE=4;
  17. //出多少道题目
  18. private static final int TIMES=100;
  19. private static Vector<Integer> ve=new Vector<Integer>();
  20. public static Vector<Integer> getVe() {
  21. return ve;
  22. }
  23. public static void setVe(Vector<Integer> ve) {
  24. Sizezt.ve = ve;
  25. }
  26. public static Vector<String> getSe() {
  27. return se;
  28. }
  29. public static void setSe(Vector<String> se) {
  30. Sizezt.se = se;
  31. }
  32. private static Vector<String> se=new Vector<String>();
  33. public static void run() {
  34. // TODO 自动生成的方法存根
  35. ve.clear();
  36. se.clear();
  37. int times=(int)(Math.random()*(SXSIZE-1)+1);
  38. String szM[]=new String[]{"+","-","X","/"};
  39. int numa=0,numb=0,numc=0,numg=0;
  40. String se_c="";
  41. int type=0;
  42. for(int j=0;j<TIMES;j++)
  43. {
  44. boolean g_ok=false;
  45. boolean g_run=false;
  46. boolean g_list=false;
  47. boolean g_xiuzheng=false;
  48. numg=0;
  49. se_c="";
  50. for(int i=0;i<times;i++)
  51. {
  52.  
  53. if(g_ok&&isOk(numa)||g_ok&&isOk(numc))
  54. {
  55. se_c+="=";
  56. System.out.print("="+"\n");
  57. ve.add(numc);
  58. numc=-2000;
  59. break;
  60. }
  61. if(!g_ok)
  62. {
  63. numa=(int)(Math.random()*600+1);
  64. numb=(int)(Math.random()*600+1);
  65. if(numg==0)
  66. numg=numa;
  67. }
  68. type=!g_list?(int)(Math.random()*4+1):(int)(Math.random()*2+1);
  69. //System.out.print("\t"+type+"\t");
  70. if(type==4)
  71. {
  72. if(!g_ok)
  73. {
  74. while(numa%numb!=0||numb==1)
  75. {
  76. numa=(int)(Math.random()*600+1);
  77. numb=(int)(Math.random()*600+1);
  78. }
  79. numc=numa/numb;
  80. }
  81. else
  82. {
  83. while(numg%numb!=0)
  84. {
  85. numb=(int)(Math.random()*600+1);
  86. }
  87. numc-=g_xiuzheng?-numg:numg;
  88. if(!g_xiuzheng)
  89. numc+=numg/numb;
  90. else numc-=numg/numb;
  91.  
  92. }
  93. g_list=true;
  94. g_run=!g_run;
  95. }
  96. else if(type==3)
  97. {
  98.  
  99. if(!g_ok)
  100. {
  101. while(numa*numb>1000||numa==1||numb==1)
  102. {
  103. numa=(int)(Math.random()*600+1);
  104. numb=(int)(Math.random()*600+1);
  105. }
  106. numc=numa*numb;
  107. }
  108. else
  109. {
  110. while(numg*numb>1000||numb!=1)
  111. {
  112. numb=(int)(Math.random()*600+1);
  113. }
  114. numc-=g_xiuzheng?numg:numg;
  115. numc+=numg*numb;
  116. }
  117. g_list=true;
  118. }
  119.  
  120. else if(type==2)
  121. {
  122. if(!g_ok)
  123. numc=numa-numb;
  124. else numc-=numb;
  125. g_list=false;
  126. }
  127. else if(type==1)
  128. {
  129. if(!g_ok)
  130. numc=numa+numb;
  131. else numc+=numb;
  132. g_list=false;
  133. }
  134.  
  135. if(!g_ok)
  136. {
  137. System.out.print(numa+szM[type-1]+numb);
  138. se_c+=numa+szM[type-1]+numb;
  139. }
  140. else
  141. {
  142. System.out.print(szM[(type==4&&!g_run)?(int)(Math.random()*2):type-1]+numb);
  143. se_c+=szM[(type==4&&!g_run)?(int)(Math.random()*2):type-1]+numb;
  144. }
  145. g_ok=true;
  146. if(type==2)
  147. g_xiuzheng=true;
  148. else g_xiuzheng=false;
  149. numg=numb;
  150. numb=(int)(Math.random()*600+1);
  151. }
  152. if(numc!=-2000)
  153. {
  154. ve.add(numc);
  155. System.out.print("="+"\r\n");
  156. se_c+="=";
  157. }
  158.  
  159. se.add(se_c);
  160. numc=0;
  161. times=(int)(Math.random()*(SXSIZE-1)+1);
  162. }
  163.  
  164. // Document document = null;
  165. // try {
  166. // document = XmlUtils.getDocument();
  167. // } catch (DocumentException e) {
  168. // // TODO 自动生成的 catch 块
  169. // e.printStackTrace();
  170. // }
  171. // Element root = document.getRootElement();
  172. // Element user_node = root.addElement("sz"); //创建user结点,并挂到root
  173. //
  174. // //user_node.setAttributeValue("ve", ve);
  175.  
  176. }
  177. public static boolean isOk(int num)
  178. {
  179. if(num==1||num<=0||num==2)
  180. return false;
  181. for(int i=2;i<=Math.sqrt(num);i++)
  182. {
  183. if(num%i==0)
  184. return false;
  185. }
  186. return true;
  187. }
  188. public void daochu() throws IOException
  189. {
  190. String st="";
  191. int an=0;
  192. Iterator<String> it;
  193. Iterator<Integer> it1;
  194. File a=new File("timu.txt");
  195. FileOutputStream b = new FileOutputStream(a);
  196. OutputStreamWriter c=new OutputStreamWriter(b,"UTF-8");
  197. it=se.iterator();
  198. it1=ve.iterator();
  199. int i=0;
  200. while(it.hasNext())
  201. {
  202. i++;
  203. if(i!=1)
  204. c.append("\r\n");
  205. st=it.next();
  206. an=it1.next();
  207. c.append(st+an+"*");
  208.  
  209. }
  210. c.close();
  211. b.close();
  212.  
  213. }
  214. public static void main(String args[])
  215. {
  216. Sizezt.run();
  217. Sizezt a=new Sizezt();
  218. try {
  219. a.daochu();
  220. } catch (IOException e) {
  221. // TODO 自动生成的 catch 块
  222. e.printStackTrace();
  223. }
  224. Daoru b=new Daoru();
  225. try {
  226. b.daoru();
  227. } catch (IOException e) {
  228. // TODO 自动生成的 catch 块
  229. e.printStackTrace();
  230. }
  231. }
  232.  
  233. }

导入类我单独写了一个,方便文件的导入。

//Daoru.java的代码如下所示:

  1. package sz;
  2.  
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.io.IOException;
  6. import java.io.InputStreamReader;
  7. import java.util.Scanner;
  8. import java.util.Vector;
  9.  
  10. public class Daoru {
  11.  
  12. Scanner can=new Scanner(System.in);
  13. public boolean isNum(char a)
  14. {
  15. if(a-'0'>=0&&a-'0'<=9)
  16. return true;
  17. return false;
  18. }
  19. public void daoru() throws IOException
  20. {
  21.  
  22. File a=new File("timu.txt");
  23. FileInputStream b = new FileInputStream(a);
  24. InputStreamReader c=new InputStreamReader(b,"UTF-8");
  25. // {
  26. // BufferedReader bufr =new BufferedReader(c);
  27. // String line = null;
  28. // while((line = bufr.readLine())!=null){
  29. // Student st=new Student();
  30. // String ook[]=line.toUpperCase().split(" ");
  31. // st.set(ook[0],ook[1],ook[2]);
  32. // map.add(st);
  33. // }
  34. // bufr.close();
  35. // //System.out.println(sb.toString());
  36. //
  37. // }
  38. int num=0,count=0,account=0;
  39. Vector<Integer> num1=new Vector<Integer>();
  40. int g_score=0;
  41. boolean g_deng=false,g_xin=true,g_jian=false;
  42. while(c.ready())
  43. {
  44. char sz=(char)c.read();
  45. if(g_xin)
  46. {
  47. ++account;
  48. System.out.print("\r\n第"+account+"题目:");
  49. if(account==1)
  50. {
  51. System.out.print("\r\n");
  52. }
  53. g_xin=false;
  54. }
  55. if(isNum(sz)&&g_deng)
  56. {
  57.  
  58. num1.add((int)(sz-'0'));
  59. count++;
  60. }
  61. else if(sz=='=')
  62. {
  63. num1.clear();
  64. num=0;
  65. count=0;
  66. g_deng=true;
  67. System.out.print(sz);
  68. }
  69. else if(sz=='*')
  70. {
  71. for(int i=num1.size()-1;i>=0;i--)
  72. {
  73. num+=num1.get(num1.size()-1-i)*Math.pow(10, i);
  74. }
  75.  
  76. if(g_jian)
  77. num=-num;
  78. //System.out.print(num);
  79. num1.clear();
  80. g_deng=false;
  81. g_xin=true;
  82. g_jian=false;
  83.  
  84. boolean g_down=true;
  85. while(g_down)
  86. {
  87. if(can.hasNextInt())
  88. {
  89.  
  90. int daan=can.nextInt();
  91.  
  92. if(daan==num)
  93. {
  94. g_score++;
  95. }
  96. g_down=false;
  97. }
  98. else
  99. {
  100. can.next();
  101. System.out.println("\r\n格式错误,请重新输入");
  102. }
  103. }
  104. num=0;
  105. count=0;
  106. }
  107. else if(sz=='-'&&g_deng)
  108. {
  109. g_jian=true;
  110.  
  111. }
  112. else
  113. {
  114. System.out.print(sz);
  115. }
  116. }
  117. c.close();
  118. b.close();
  119. System.out.println("\r\n你最终的得分是:"+g_score);
  120. }
  121.  
  122. }

生成的size.txt文件如下所示

实验的截图如下:

此次的实验大概就是如此

java实验(三)——课堂小测的更多相关文章

  1. 20165324 Java实验三 敏捷开发与XP实验

    20165324 Java实验三 敏捷开发与XP实验 一.实验报告封面 课程:Java程序设计 班级:1653班 姓名:何春江 学号:20165324 指导教师:娄嘉鹏 实验日期:2018年4月16日 ...

  2. 20145203盖泽双java实验三 敏捷开发与XP实践

    java实验三 敏捷开发与XP实践 实验内容 XP基础 XP核心实践 相关工具 实验要求 1.没有Linux基础的同学建议先学习<Linux基础入门(新版)><Vim编辑器> ...

  3. MySQL课堂小测

    目录 一.基本知识与操作方法 二.小测具体内容 (一)向数据库表中添加记录 (二)下载并导入world.sql (三)数据库查询与输出 (四)查询数据库并求某字段和 (五)查询数据库并取最大& ...

  4. java实验三 敏捷开发与XP实践

    一.实验内容 (一)敏捷开发与XP 软件开发流程的目的是为了提高软件开发.运营.维护的效率,并提高软件的质量.用户满意度.可靠性和软件的可维护性. 光有各种流程的思想是不够的,我们还要有一系列的工具来 ...

  5. Java实验三

    20145113 20145102实验三 实验步骤 编码标准 编程标准包含:具有说明性的名字.清晰的表达式.直截了当的控制流.可读的代码和注释,以及在追求这些内容时一致地使用某些规则和惯用法的重要性 ...

  6. java实验三——求平均数,数组排序(有关java保留小数位数,由于编译器版本未到1.5导致的报错format函数第二个参数不对,要求是Object[])

    package hello; import java.util.Arrays; public class 实验三更正版 { public static void main(String[] args) ...

  7. JAVA实验三及总结

    JAVA第五周作业 Java实验报告三 第一题 1.已知字符串:"this is a test of java".按要求执行以下操作:(要求源代码.结果截图.) (1).统计该字符 ...

  8. Java实验三报告

    一.  实验内容 (一)敏捷开发与XP 摘要:一项实践在XP环境中成功使用的依据通过XP的法则呈现,包括:快速反馈.假设简单性.递增更改.提倡更改.优质工作.XP软件开发的基石是XP的活动,包括:编码 ...

  9. java实验三20135104

    北京电子科技学院(BESTI) 实     验    报     告 课程:Java程序设计                         班级:1351            姓名:刘帅      ...

随机推荐

  1. java nio 读取大文件

    package com.yao.bigfile; import java.io.File; import java.io.IOException; import java.io.RandomAcces ...

  2. SQL Server中的事务日志管理的阶梯,级别1:事务日志概述

    SQL Server中的事务日志管理的阶梯,级别1:事务日志概述 翻译:刘琼滨 谢雪妮 许雅莉 赖慧芳 级别1:事务日志概述 事务日志是一个文件,其中SQL服务器存储了所有与日志文件关联的数据库执行的 ...

  3. maven打包的含义

    我们在用maven构建java项目时,最常用的打包命令有mvn package.mvn install.deploy,这三个命令都可完成打jar包或war(当然也可以是其它形式的包)的功能,但这三个命 ...

  4. core 中使用 nlog

    引包 代码 public void Configure(IApplicationBuilder app, IHostingEnvironment env,ILoggerFactory logFac) ...

  5. python 人脸识别试水(一)

    1.安装python,在这里我的版本是python 3.6 2.安装pycharm,我的版本是pycharm 2017 3.安装pip  pip 版本10 4.安装 numpy    :pip ins ...

  6. vux安装

    1. 在项目里安装vux cnpm install vux --save 2. 安装vux-loader cnpm install vux-loader --save-dev 3. 安装less-lo ...

  7. 失误1: 把i放到循环体内部,i++失效

    54             while($lines_num_of_whole_table>=1){ 55                 my $i = 1;                 ...

  8. 洛谷 P1518 两只塔姆沃斯牛

    P1518 两只塔姆沃斯牛 The Tamworth Two 简单的模拟题,代码量不大. 他们走的路线取决于障碍物,可以把边界也看成障碍物,遇到就转,枚举次,因为100 * 100 * 4,只有4个可 ...

  9. go语言的碎片整理:time

    时间和日期是我们编程中经常用到的,本文主要介绍了Go语言内置的time包的基本用法. Go语言中导入包 单行导入 import "time" import "fmt&qu ...

  10. BZOJ2726【SDOI2012】任务安排(斜率优化Dp+二分查找)

    由题目条件显然可以得到状态 f[i][j] 表示以 i 为结尾且 i 后作为断点,共做了 j 次分组的最小代价. 因此转移变得很显然:f[i][j]=min{f[k][j-1]+(s×j+sumT[i ...