java实验(三)——课堂小测
这次的课堂小测是用以前生成的那些四则运算的代码,然后将这些题目写到一个文件中,再通过这个文件读取题目的信息,每读入一个答案的时候,遇到星号的时候,等待用户输入然后判断输入的答案是否正确,然后输出小一道题目让用户输入,直到文件读到结束的位置。
思路大概是如下:
一开始的思路是答案同样也在文本输入,例如1+1=2*;
每次读取到等号的时候,将文件中答案读取出来,并且将变量设置为此值。接下来就能用户输入的答案是否正确了。
//Sizezt.java的代码如下:
- package sz;
- //import java.io.BufferedReader;
- import java.io.File;
- //import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.IOException;
- //import java.io.InputStreamReader;
- import java.io.OutputStreamWriter;
- import java.util.Iterator;
- import java.util.Vector;
- public class Sizezt {
- //出现运算项的最高个数
- private static final int SXSIZE=4;
- //出多少道题目
- private static final int TIMES=100;
- private static Vector<Integer> ve=new Vector<Integer>();
- public static Vector<Integer> getVe() {
- return ve;
- }
- public static void setVe(Vector<Integer> ve) {
- Sizezt.ve = ve;
- }
- public static Vector<String> getSe() {
- return se;
- }
- public static void setSe(Vector<String> se) {
- Sizezt.se = se;
- }
- private static Vector<String> se=new Vector<String>();
- public static void run() {
- // TODO 自动生成的方法存根
- ve.clear();
- se.clear();
- int times=(int)(Math.random()*(SXSIZE-1)+1);
- String szM[]=new String[]{"+","-","X","/"};
- int numa=0,numb=0,numc=0,numg=0;
- String se_c="";
- int type=0;
- for(int j=0;j<TIMES;j++)
- {
- boolean g_ok=false;
- boolean g_run=false;
- boolean g_list=false;
- boolean g_xiuzheng=false;
- numg=0;
- se_c="";
- for(int i=0;i<times;i++)
- {
- if(g_ok&&isOk(numa)||g_ok&&isOk(numc))
- {
- se_c+="=";
- System.out.print("="+"\n");
- ve.add(numc);
- numc=-2000;
- break;
- }
- if(!g_ok)
- {
- numa=(int)(Math.random()*600+1);
- numb=(int)(Math.random()*600+1);
- if(numg==0)
- numg=numa;
- }
- type=!g_list?(int)(Math.random()*4+1):(int)(Math.random()*2+1);
- //System.out.print("\t"+type+"\t");
- if(type==4)
- {
- if(!g_ok)
- {
- while(numa%numb!=0||numb==1)
- {
- numa=(int)(Math.random()*600+1);
- numb=(int)(Math.random()*600+1);
- }
- numc=numa/numb;
- }
- else
- {
- while(numg%numb!=0)
- {
- numb=(int)(Math.random()*600+1);
- }
- numc-=g_xiuzheng?-numg:numg;
- if(!g_xiuzheng)
- numc+=numg/numb;
- else numc-=numg/numb;
- }
- g_list=true;
- g_run=!g_run;
- }
- else if(type==3)
- {
- if(!g_ok)
- {
- while(numa*numb>1000||numa==1||numb==1)
- {
- numa=(int)(Math.random()*600+1);
- numb=(int)(Math.random()*600+1);
- }
- numc=numa*numb;
- }
- else
- {
- while(numg*numb>1000||numb!=1)
- {
- numb=(int)(Math.random()*600+1);
- }
- numc-=g_xiuzheng?numg:numg;
- numc+=numg*numb;
- }
- g_list=true;
- }
- else if(type==2)
- {
- if(!g_ok)
- numc=numa-numb;
- else numc-=numb;
- g_list=false;
- }
- else if(type==1)
- {
- if(!g_ok)
- numc=numa+numb;
- else numc+=numb;
- g_list=false;
- }
- if(!g_ok)
- {
- System.out.print(numa+szM[type-1]+numb);
- se_c+=numa+szM[type-1]+numb;
- }
- else
- {
- System.out.print(szM[(type==4&&!g_run)?(int)(Math.random()*2):type-1]+numb);
- se_c+=szM[(type==4&&!g_run)?(int)(Math.random()*2):type-1]+numb;
- }
- g_ok=true;
- if(type==2)
- g_xiuzheng=true;
- else g_xiuzheng=false;
- numg=numb;
- numb=(int)(Math.random()*600+1);
- }
- if(numc!=-2000)
- {
- ve.add(numc);
- System.out.print("="+"\r\n");
- se_c+="=";
- }
- se.add(se_c);
- numc=0;
- times=(int)(Math.random()*(SXSIZE-1)+1);
- }
- // Document document = null;
- // try {
- // document = XmlUtils.getDocument();
- // } catch (DocumentException e) {
- // // TODO 自动生成的 catch 块
- // e.printStackTrace();
- // }
- // Element root = document.getRootElement();
- // Element user_node = root.addElement("sz"); //创建user结点,并挂到root
- //
- // //user_node.setAttributeValue("ve", ve);
- }
- public static boolean isOk(int num)
- {
- if(num==1||num<=0||num==2)
- return false;
- for(int i=2;i<=Math.sqrt(num);i++)
- {
- if(num%i==0)
- return false;
- }
- return true;
- }
- public void daochu() throws IOException
- {
- String st="";
- int an=0;
- Iterator<String> it;
- Iterator<Integer> it1;
- File a=new File("timu.txt");
- FileOutputStream b = new FileOutputStream(a);
- OutputStreamWriter c=new OutputStreamWriter(b,"UTF-8");
- it=se.iterator();
- it1=ve.iterator();
- int i=0;
- while(it.hasNext())
- {
- i++;
- if(i!=1)
- c.append("\r\n");
- st=it.next();
- an=it1.next();
- c.append(st+an+"*");
- }
- c.close();
- b.close();
- }
- public static void main(String args[])
- {
- Sizezt.run();
- Sizezt a=new Sizezt();
- try {
- a.daochu();
- } catch (IOException e) {
- // TODO 自动生成的 catch 块
- e.printStackTrace();
- }
- Daoru b=new Daoru();
- try {
- b.daoru();
- } catch (IOException e) {
- // TODO 自动生成的 catch 块
- e.printStackTrace();
- }
- }
- }
导入类我单独写了一个,方便文件的导入。
//Daoru.java的代码如下所示:
- package sz;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.Scanner;
- import java.util.Vector;
- public class Daoru {
- Scanner can=new Scanner(System.in);
- public boolean isNum(char a)
- {
- if(a-'0'>=0&&a-'0'<=9)
- return true;
- return false;
- }
- public void daoru() throws IOException
- {
- File a=new File("timu.txt");
- FileInputStream b = new FileInputStream(a);
- InputStreamReader c=new InputStreamReader(b,"UTF-8");
- // {
- // BufferedReader bufr =new BufferedReader(c);
- // String line = null;
- // while((line = bufr.readLine())!=null){
- // Student st=new Student();
- // String ook[]=line.toUpperCase().split(" ");
- // st.set(ook[0],ook[1],ook[2]);
- // map.add(st);
- // }
- // bufr.close();
- // //System.out.println(sb.toString());
- //
- // }
- int num=0,count=0,account=0;
- Vector<Integer> num1=new Vector<Integer>();
- int g_score=0;
- boolean g_deng=false,g_xin=true,g_jian=false;
- while(c.ready())
- {
- char sz=(char)c.read();
- if(g_xin)
- {
- ++account;
- System.out.print("\r\n第"+account+"题目:");
- if(account==1)
- {
- System.out.print("\r\n");
- }
- g_xin=false;
- }
- if(isNum(sz)&&g_deng)
- {
- num1.add((int)(sz-'0'));
- count++;
- }
- else if(sz=='=')
- {
- num1.clear();
- num=0;
- count=0;
- g_deng=true;
- System.out.print(sz);
- }
- else if(sz=='*')
- {
- for(int i=num1.size()-1;i>=0;i--)
- {
- num+=num1.get(num1.size()-1-i)*Math.pow(10, i);
- }
- if(g_jian)
- num=-num;
- //System.out.print(num);
- num1.clear();
- g_deng=false;
- g_xin=true;
- g_jian=false;
- boolean g_down=true;
- while(g_down)
- {
- if(can.hasNextInt())
- {
- int daan=can.nextInt();
- if(daan==num)
- {
- g_score++;
- }
- g_down=false;
- }
- else
- {
- can.next();
- System.out.println("\r\n格式错误,请重新输入");
- }
- }
- num=0;
- count=0;
- }
- else if(sz=='-'&&g_deng)
- {
- g_jian=true;
- }
- else
- {
- System.out.print(sz);
- }
- }
- c.close();
- b.close();
- System.out.println("\r\n你最终的得分是:"+g_score);
- }
- }
生成的size.txt文件如下所示
实验的截图如下:
此次的实验大概就是如此
java实验(三)——课堂小测的更多相关文章
- 20165324 Java实验三 敏捷开发与XP实验
20165324 Java实验三 敏捷开发与XP实验 一.实验报告封面 课程:Java程序设计 班级:1653班 姓名:何春江 学号:20165324 指导教师:娄嘉鹏 实验日期:2018年4月16日 ...
- 20145203盖泽双java实验三 敏捷开发与XP实践
java实验三 敏捷开发与XP实践 实验内容 XP基础 XP核心实践 相关工具 实验要求 1.没有Linux基础的同学建议先学习<Linux基础入门(新版)><Vim编辑器> ...
- MySQL课堂小测
目录 一.基本知识与操作方法 二.小测具体内容 (一)向数据库表中添加记录 (二)下载并导入world.sql (三)数据库查询与输出 (四)查询数据库并求某字段和 (五)查询数据库并取最大& ...
- java实验三 敏捷开发与XP实践
一.实验内容 (一)敏捷开发与XP 软件开发流程的目的是为了提高软件开发.运营.维护的效率,并提高软件的质量.用户满意度.可靠性和软件的可维护性. 光有各种流程的思想是不够的,我们还要有一系列的工具来 ...
- Java实验三
20145113 20145102实验三 实验步骤 编码标准 编程标准包含:具有说明性的名字.清晰的表达式.直截了当的控制流.可读的代码和注释,以及在追求这些内容时一致地使用某些规则和惯用法的重要性 ...
- java实验三——求平均数,数组排序(有关java保留小数位数,由于编译器版本未到1.5导致的报错format函数第二个参数不对,要求是Object[])
package hello; import java.util.Arrays; public class 实验三更正版 { public static void main(String[] args) ...
- JAVA实验三及总结
JAVA第五周作业 Java实验报告三 第一题 1.已知字符串:"this is a test of java".按要求执行以下操作:(要求源代码.结果截图.) (1).统计该字符 ...
- Java实验三报告
一. 实验内容 (一)敏捷开发与XP 摘要:一项实践在XP环境中成功使用的依据通过XP的法则呈现,包括:快速反馈.假设简单性.递增更改.提倡更改.优质工作.XP软件开发的基石是XP的活动,包括:编码 ...
- java实验三20135104
北京电子科技学院(BESTI) 实 验 报 告 课程:Java程序设计 班级:1351 姓名:刘帅 ...
随机推荐
- java nio 读取大文件
package com.yao.bigfile; import java.io.File; import java.io.IOException; import java.io.RandomAcces ...
- SQL Server中的事务日志管理的阶梯,级别1:事务日志概述
SQL Server中的事务日志管理的阶梯,级别1:事务日志概述 翻译:刘琼滨 谢雪妮 许雅莉 赖慧芳 级别1:事务日志概述 事务日志是一个文件,其中SQL服务器存储了所有与日志文件关联的数据库执行的 ...
- maven打包的含义
我们在用maven构建java项目时,最常用的打包命令有mvn package.mvn install.deploy,这三个命令都可完成打jar包或war(当然也可以是其它形式的包)的功能,但这三个命 ...
- core 中使用 nlog
引包 代码 public void Configure(IApplicationBuilder app, IHostingEnvironment env,ILoggerFactory logFac) ...
- python 人脸识别试水(一)
1.安装python,在这里我的版本是python 3.6 2.安装pycharm,我的版本是pycharm 2017 3.安装pip pip 版本10 4.安装 numpy :pip ins ...
- vux安装
1. 在项目里安装vux cnpm install vux --save 2. 安装vux-loader cnpm install vux-loader --save-dev 3. 安装less-lo ...
- 失误1: 把i放到循环体内部,i++失效
54 while($lines_num_of_whole_table>=1){ 55 my $i = 1; ...
- 洛谷 P1518 两只塔姆沃斯牛
P1518 两只塔姆沃斯牛 The Tamworth Two 简单的模拟题,代码量不大. 他们走的路线取决于障碍物,可以把边界也看成障碍物,遇到就转,枚举次,因为100 * 100 * 4,只有4个可 ...
- go语言的碎片整理:time
时间和日期是我们编程中经常用到的,本文主要介绍了Go语言内置的time包的基本用法. Go语言中导入包 单行导入 import "time" import "fmt&qu ...
- BZOJ2726【SDOI2012】任务安排(斜率优化Dp+二分查找)
由题目条件显然可以得到状态 f[i][j] 表示以 i 为结尾且 i 后作为断点,共做了 j 次分组的最小代价. 因此转移变得很显然:f[i][j]=min{f[k][j-1]+(s×j+sumT[i ...