优点:她把各个模块儿完成的很好并且生成的卷子没有异常
缺点:使用路径是使用了绝对路径这导致可移植性差的原因,代码布局上不是很好。
如下伪代码:
package page;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.*;
import static page.Login.pattern;
public class CreatePaper {
public static String dir = "D:\\桌面\\桌面\\软件2103-木叶斯尔·亚森\\";
public static int readInput(){
Login log = new Login();
Scanner in = new Scanner(System.in);
System.out.println("正在为您生成"+pattern+"试题,请输入题目数量(数量区间在【10,30】或您需要改变的题目难度");
String qNum = in.nextLine();
int qnum = 0;
if (qNum.equals("小学")) {
pattern = "小学";
System.out.println("已修改为"+pattern+"试题,请输入题目数量(数量区间在【10,30】");
qNum = in.nextLine();
}
else if (qNum.equals("初中")) {
pattern = "初中";
System.out.println("已修改为"+pattern+"试题,请输入题目数量(数量区间在【10,30】");
qNum = in.nextLine();
}
else if (qNum.equals("高中")) {
pattern = "高中";
System.out.println("已修改为"+pattern+"试题,请输入题目数量(数量区间在【10,30】");
qNum = in.nextLine();
}
else if (qNum.equals("-1")) {
log.signIn();
qnum = -1;
}
else if (qNum.matches("\\d+")) {
qnum = Integer.valueOf(qNum);
if (qnum<10 || qnum>30) { // 题目数量不在【10,30】
System.out.println("题目数量需在【10,30】,请重新输入");//不满足条件重新输入
qnum= Integer.parseInt(in.nextLine());
}
else {
int a= qnum;
System.out.println("为您生成"+a+"道题目");
}
}
return qnum;
}
public static String createQuestion(String pattern) {
Random rand = new Random();
String[] arithmetic = null; //生成的算术题
String[] signs = new String[]{"+", "-", "*", "÷"};
int operand = rand.nextInt(4) + 2; //至少要有2个操作数
arithmetic = new String[operand];
for (int i = 0; i < operand; i++) {
arithmetic[i] = String.valueOf(rand.nextInt(100));
}
if(Login.pattern.equals("初中")) {
for (int i = 0; i < operand; i++) {
if (rand.nextBoolean()) {
arithmetic[i] = arithmetic[i] + "^2";
} else {
arithmetic[i] = "√" + arithmetic[i];
}
}
}
else if (Login.pattern.equals("高中")) {
int[] j = {0, 30, 45, 60, 90, 120, 135, 150, 180};
String[] s = {"sin", "cos", "tan"};
boolean flag = false;
while (!flag) {
for (int i = 0; i < operand; i++) {
if (rand.nextBoolean()) {
int temp = rand.nextInt(3);
int r = rand.nextInt(j.length);
arithmetic[i] = s[temp] + String.valueOf(j[r]);
flag = true;
}
}
}
}
if (operand >= 3) {//加括号
int length = rand.nextInt(operand -1); //括号内操作数长度
int lbracket = rand.nextInt(operand - length); // 左括号
int rbracket = lbracket + length; // 右括号
arithmetic[lbracket] = "(" + arithmetic[lbracket];
arithmetic[rbracket] = arithmetic[rbracket] + ")";
}
for (int i=0;i<operand-1; i++ ) {
int j = rand.nextInt(4);
arithmetic[i] = arithmetic[i] +signs[j];
}
arithmetic[operand-1] =arithmetic[operand-1]+"=";
//Arrays.toString(arithmetic[])
//String result = String.join(arithmetic);
String s2 = String.join(" ",arithmetic);
return s2;
}
static HashSet<String> getAllPapers(String name) {
String path = dir+name;
File file = new File(path);
if(!file.exists()){//没有该文件夹就创建
file.mkdir();
}
File[] fs = file.listFiles(); //遍历path下的文件和目录,放在File数组中
HashSet<String> allPapers= new HashSet<String>();
for(File f:fs) { //遍历File[]数组
if (!f.isDirectory()) //若非目录(即文件),则读入
{
try {
BufferedReader br = new BufferedReader(new FileReader(f));
String line;
while ((line = br.readLine()) != null) {
// 一次读入一行数据并加入到结果的hashset中
allPapers.add(line);
}
br.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
return allPapers;
}
public static void createPaper(String name,int fNum) {
HashSet<String> papers=getAllPapers(name);//获得之前生成的题目的hashset
String dirPath = dir+name;// 文件夹路径
Date date = new Date(System.currentTimeMillis());
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
String time = simpleDateFormat.format(date);
String path = dirPath +"\\"+time; // 文件名
try {
FileWriter fw = new FileWriter(path,true); // 设置追加属性,不断对文件进行追加
for(int i=0;i<fNum;i++) {//生成对应数量题目
String problem = createQuestion(pattern);
if(!papers.contains(problem)){ //如果已有的题目中不包含该题目
problem=String.valueOf(i+1)+"."+problem;//加上序号
fw.write(problem+"\n"+"\n");
}
}
System.out.println("卷子生成完毕");
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
package page;
import java.util.Scanner;
public class Login {
private static String name;
private static String password;
public static String pattern;
public static String dir = "";
static boolean flag = false;
public static void main(String[] args) {
while (true) {
signIn();
int fNum = CreatePaper.readInput();
if (fNum == -1) {
continue;
}
CreatePaper.createPaper(name,fNum);
}
}
public static void compareInformation(String name,String password) { // 用户登录
if (name.equals("张三1") || name.equals("张三2") || name.equals("张三3")) {
if (password.equals("123")) {
pattern = "小学";
flag = true;
}
} else if (name.equals("李四1") || name.equals("李四2") || name.equals("李四3")) {
if (password.equals("123")) {
pattern = "初中";
flag =true;
}
} else if (name.equals("王五1") || name.equals("王五2") || name.equals("王五3")) {
if (password.equals("123")) {
pattern = "高中";
flag =true;
}
}
if (!flag){
System.out.println("请输入正确的账号与密码!");
}
}
public static void signIn() {
Scanner in = new Scanner(System.in);
System.out.println("请输入账号与密码登录:");
while(!flag) {
String str[] = in.nextLine().split(" "); // 将输入的内容以空格分隔为姓名与密码
name = str[0];
password = str[1];
compareInformation(name, password);
}
}
}
运行结果:
C:\Users\LENOVO\.jdks\corretto-19.0.2\bin\java.exe "-javaagent:D:\Java\IntelliJ IDEA Community Edition 2022.2.1\lib\idea_rt.jar=63274:D:\Java\IntelliJ IDEA Community Edition 2022.2.1\bin" -Dfile.encoding=UTF-8 -Dsun.stdout.encoding=UTF-8 -Dsun.stderr.encoding=UTF-8 -classpath D:\桌面\桌面\软件2103-木叶斯尔·亚森\project1\out\production\project1 page.Login
请输入账号与密码登录:
张三1 123
正在为您生成小学试题,请输入题目数量(数量区间在【10,30】或您需要改变的题目难度
12
为您生成12道题目
卷子生成完毕
请输入账号与密码登录:
正在为您生成小学试题,请输入题目数量(数量区间在【10,30】或您需要改变的题目难度
高中
已修改为高中试题,请输入题目数量(数量区间在【10,30】
12
卷子生成完毕
请输入账号与密码登录:
标签:name,个人,项目,int,pattern,equals,评价,arithmetic,String From: https://www.cnblogs.com/aziz011300/p/17727604.html