前言:

花了一整天时间修修补补写完代码,现在写篇博客,一是希望后来的人有个参考,二是记录下自己的所获方便以后查阅,三是趁众大佬还没做,混点访问量

以前做项目都是自己做,这次是真切的体会到了为别人做事多么麻烦,这次还好,提需求的客户比较专业

设置:

根号为√,如果里面是多项式用括号括起来

平方为^,因为并没有说可以更多次方

输入为

输出

这里有一个小歧义,需求中说的题目至少有一个三角函数到底是一道题还是一套题,从经验上看,应该是一套题

11:03:17

注意点:

构造输出到文件的流

file=new File("D:/java/calculate/"+un+"/"+title+".txt");

FileOutputStream out=new FileOutputStream(file) ;

PrintStream printToFile=new PrintStream(out);

PrintStream printToConsole=System.out;

将各种难度的功能拆分

public void sqrt() {

System.out.print(sign2[0]);

}

public void square() {

System.out.print(sign2[1]);

}

public void trigonometric() {

int temp=random.nextInt(3);

System.out.print(sign3[temp]);

}

对输出到文件的执行

public void result(FirestProjct f,int n,String g,PrintStream printToFile,PrintStream printToConsole) throws IOException {

System.out.println("开始输出到文件");

System.setOut(printToFile);

f.calculate(4,n,g);

System.setOut(printToConsole);

System.out.println("输出成功");

}

其他的就是边写边做,对照着需求来,但是弄了几个小时感觉有一些的需求又费时又学不到东西,so~我选择忽略

因为还有一些更重要的事情做,这种任务只能尽快做完并消化

以下是源代码

package zuoye;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.OutputStream;

import java.io.OutputStreamWriter;

import java.io.PrintStream;

import java.io.UnsupportedEncodingException;

import java.text.SimpleDateFormat;

import java.util.Date;

import java.util.Random;

import java.util.Scanner;

public class FirestProjct {

public static Random random=new Random();

private char[] sign1= {'+','-','*','/'};//符号

private char[] sign2= {'√','^','%'};//根号,平方和,取余(可加可不加)

private String[] sign3= {"sin","cos","tan"};//三角函数

private static String [] grade={"小学","初中","高中"};

int p=3;//算式符号出现的概率1/3

//主方法主要负责密码验证,传入参数,递归调用

public static void main(String [] args) throws IOException{

Scanner scanner=new Scanner(System.in);

String [] username= {"张三1","张三2","张三3","李四1","李四2","李四3","王五1","王五2","王五3"};//账号

String password="123";

String un,pwd;

System.out.println("请输入用户名和密码");

while(true) {//判定密码

boolean input=false;

un=scanner.nextLine();

pwd=scanner.nextLine();

for(int i=0;i<9;i++) {

if(username[i].equals(un)&&password.equals(pwd))input=true;

}

if(input)break;

else System.out.println("请输入正确的用户名、密码");

}

System.out.println("请输入小学、初中和高中三个选项中的一个");

String g=scanner.nextLine();

work(un,pwd,g);

}

//work方法用以执行整个过程

public static void work(String un,String pwd,String g) throws IOException {

Scanner scanner=new Scanner(System.in);

FirestProjct f=new FirestProjct();

int n=0;//题目数量

//做计算

while(true) {

//创建文件

SimpleDateFormat dFormat=new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");

String title=dFormat.format(new Date()).toString();

File file=new File("D:/java/calculate/"+un+"/");

if(!file.exists()) {

file.mkdirs();

}

file=new File("D:/java/calculate/"+un+"/"+title+".txt");

FileOutputStream out=new FileOutputStream(file) ;

PrintStream printToFile=new PrintStream(out);

PrintStream printToConsole=System.out;

f.prepare(g);

while(true) {

n=scanner.nextInt();

if(!f.judgeover(n))

System.out.println("数量应在10~30,请重新输入");

else break;

}

//第一次题目的输出

f.result(f, n, g, printToFile, printToConsole);

System.out.println("请输入“切换为xx”或者“继续输入”");

String command=scanner.next();//nextline不能把式子停下来~

String gra=command.substring(0,3);

String de=command.substring(3,5);

//切换为后面的输入错误则重新输入

while(command.length()>3&&gra.equals("切换为")&&!(de.equals("小学")||de.equals("初中")||de.equals("高中"))) {

System.out.println("请输入小学、初中和高中三个选项中的一个");

command=scanner.next();

gra=command.substring(0,3);

de=command.substring(3,5);

}

if(command=="继续输入")work(un,pwd,g) ;

else {

for(int i=0;i<3;i++) {

if(("切换为"+grade[i]).equals(command)) {

g=grade[i];

work(un,pwd,g);

}

}

}

}

}

public void result(FirestProjct f,int n,String g,PrintStream printToFile,PrintStream printToConsole) throws IOException {

System.out.println("开始输出到文件");

System.setOut(printToFile);

f.calculate(4,n,g);

System.setOut(printToConsole);

System.out.println("输出成功");

}

public void prepare(String g) {

Scanner scanner=new Scanner(System.in);

System.out.println("当前选择为"+g+"出题");

System.out.println("准备生成"+g+"数学题目,请输入生成题目数量:");

}

public boolean judgeover(int n) {//判定题数是否合适

if(n<10||n>30) {

return false;

}

return true;

}

public void calculate(int si,int n,String g) {//计算过程 si表示sign1用到的符号个数,n表示数字个数

System.out.println(g+"试卷");

boolean [] cou= {false,false,false};//用以判定直到最后是否出现了根号,三角函数,如果没有,就让最后一个数带上

for(int i=0;i

int count=random.nextInt(2)+3;

System.out.print(i+1+"、");

for(int j=0;j

int number=random.nextInt(100)+1;

int number2=random.nextInt(100)+1;

int temp=0;

int bracket=random.nextInt(p);//随机出现括号,概率为1/3

int sqrt=random.nextInt(p);

int square=random.nextInt(p);

int tri=random.nextInt(p);

if(bracket==2) {//加括号的情况输出类似于(x+y)-

bracket(number, number2, temp, si);

System.out.print(sign1[temp]);

}

if(g.equals(grade[1])&&(sqrt==2)){//只输出根号

sqrt();

cou[1]=true;

}

if(g.equals(grade[2])&&tri==2) {//只输出三角函数符号

trigonometric();

cou[2]=true;

}

if(j==count-1&&g.equals(grade[2])&&!cou[2])trigonometric();

System.out.print(number);

if(g.equals(grade[1])&&(square==2)){//只输出平方

square();

cou[3]=true;

}

temp=random.nextInt(si);

if(j

if(j==count-1&&g.equals(grade[1])&&!cou[1]&&!cou[3])square();

}

System.out.println("=");

System.out.println();

}

}

public void bracket(int number,int number2,int temp,int sig) {

System.out.print("(");

System.out.print(number);

temp=random.nextInt(sig);

System.out.print(sign1[temp]);

System.out.print(number2);

System.out.print(")");

}

//根号和平方位置不同,只能分开,目前不知道怎么办,难受

public void sqrt() {

System.out.print(sign2[0]);

}

public void square() {

System.out.print(sign2[1]);

}

public void trigonometric() {

int temp=random.nextInt(3);

System.out.print(sign3[temp]);

}

}

java 数学测试_自动生成不同难度的数学试卷系统,并输出到txt文件中,命名为当前时间(java)...相关推荐

  1. 用java在d盘创建test文件_在D盘中创建文件test.txt,文件中内容为:hello Java,然后利用流把该文件拷贝到E盘根目录中...

    // 在D盘中创建文件test.txt,文件中的内容为:"hello Java" File file = new File("D:/test.txt"); St ...

  2. Java——接收用户在控制台的信息,将取到的信息写入lianxi.txt文件中,并且将lianxi.txt内容复制到home.txt中

    一.题目 接收用户在控制台的信息,将取到的信息写入lianxi.txt文件中,并且将lianxi.txt内容复制到home.txt中 二.实现代码 package day08;import java. ...

  3. java 日志 生成器_自动生成 java 测试 mock 对象框架 DataFactory-01-入门使用教程

    项目简介 Data-Factory 用于根据对象,随机自动生成初始化信息,避免了手动创建对象的繁琐,便于测试. 特性 8 大基本类型的支持 String.Date.金额,日期等常见类型的支持 java ...

  4. java 生成txt日志方法,java定向输出程序日志(输出到txt文件中)

    public class RedirectOutputStream { public static void main(String[] args) { try { PrintStream out = ...

  5. java定向输出程序日志(输出到txt文件中)

    public class RedirectOutputStream { public static void main(String[] args) { try { PrintStream out = ...

  6. 用java做小学数学系统_小学生数学练习题目自动生成系统——java课程设计

    <小学生数学练习题目自动生成系统--java课程设计>由会员分享,可在线阅读,更多相关<小学生数学练习题目自动生成系统--java课程设计(37页珍藏版)>请在金锄头文库上搜索 ...

  7. JAVA课程设计 小学生数学练习题目自动生成系统

    课程设计的任务和目标 目标: 编写一个能够自动生成小学生数学练习题目的程序.设计一个程序,能够根据用户的选择生成"10以内加法"."10以内减法"." ...

  8. Java api文档自动生成工具smartdoc+torna

    首先,一般做java服务端都用过postman,并且都写过前端调用的api文档接口,并且也用过市面上的一些工具. API自文档动生成,能够省去了写文档的时间. 当然,当前也会有很多类似的开源工具,我们 ...

  9. mysql xml生成工具_自动生成po、dao、xml 工具:mybatis-generator

    需要注意:文件路径关系,xml路径,jar包路径,以及xml中生成文件的路径等. 由于MyBatis属于一种半自动的ORM框架,所以主要的工作将是书写Mapping映射文件,但是由于手写映射文件很容易 ...

最新文章

  1. Matlab数据的可视化 -- 条形图
  2. matlab潮流程序,IEEE33节点matlab潮流程序.doc
  3. hMailServer 邮件系统安装配置篇
  4. html标签名都是小写,到底啥是w3c标准(示例代码)
  5. MFC对话框动态刷新图片(仿照全屏屏保)
  6. [翻译]JUnit 5 用户手册
  7. 5种电脑定时关机的方法分享
  8. 安装装ankhsvn
  9. android图片添加文字,android图片上添加文字
  10. Win系统 - WIN10 版本号说明
  11. SpringCloud实战微服务之——Ribbon详解
  12. Java实现Socket网络聊天室
  13. JavaWeb购物系统(七)购物系统主页,商品的管理
  14. 【matlab图像处理】matlab数据结构(1)
  15. 腾讯视频投屏显示无法连接服务器,腾讯视频投屏不能快进
  16. flash源文件小,导出后变大的问题
  17. tplink720n变无线打印服务器,TL-WR720N路由器无线路由模式设置
  18. 英勇青铜5基金个人策略
  19. 工具类:自动生成名字工具类
  20. GaussDB 200单机实例安装使用教程【入门必看】

热门文章

  1. ZZULIOJ 1788 小金刚的宝藏 (01背包)
  2. 我思故我在-记录我的困惑
  3. c# aspx转为html,asp.net(c#)网页跳转七种方法小结
  4. 用思科模拟器实现三层交换机下不同VLAN之间的通信
  5. 操作系统怎么把文件放到docker容器里
  6. docker 雷电模拟器
  7. 用java设计一个动物声音 模拟器,Java语言 设计一个动物声音“模拟器”(用接口实现)...
  8. 题目 1878: 蓝桥杯2017年第八届真题-青蛙跳杯子
  9. 2019电赛a题资源整合
  10. 努力是为了不辜负自己