本博客是对结对编程队友王同学编写的个人项目的分析评价,编写语言为java。
用户:小学、初中、高中数学老师。
主要功能:输入用户名和密码登录,可以操作生成小初高数学卷子,题目不重复,保存在.TXT文件中。
题目要求:
小学 | 初中 | 高中 |
---|---|---|
难度要求 | +,-,*./ | 平方、开方 |
备注 | 只能有+,-,*./和() | 题目中至少有一个平方或开根号的运算符 |
import java.util.HashMap; import java.util.List; public class user { private String name; private String password; private String grade; public void setName(String name) { this.name = name; } public void setPassword(String password) { this.password = password; } public void setGrade(String grade) { this.grade = grade; } public String getName() { return name; } public String getPassword() { return password; } public String getGrade() { return grade; } public static HashMap<String, List<String>> userHashMap = new HashMap<>(); static { userHashMap.put("张三1", List.of("123", "小学")); userHashMap.put("张三2", List.of("123", "小学")); userHashMap.put("张三3", List.of("123", "小学")); userHashMap.put("李四1", List.of("123", "初中")); userHashMap.put("李四2", List.of("123", "初中")); userHashMap.put("李四3", List.of("123", "初中")); userHashMap.put("王五1", List.of("123", "高中")); userHashMap.put("王五2", List.of("123", "高中")); userHashMap.put("王五3", List.of("123", "高中")); } }
import java.io.*; import java.text.SimpleDateFormat; import java.util.Date; import java.util.HashSet; import java.util.Random; import java.util.Scanner; public class paper { private int state; public static String dir=".\\"; public static String buf = ""; public void setState(int state) { this.state = state; } static String getProblem(int state){ String[] symbol = new String[]{"+","-","*","/","^2","√","sin","cos","tan"}; Random r = new Random(); String problem = new String(); if(state == 1){ while (true){ problem = ""; int length = 0; int kuohao = r.nextInt(3); int left = 0; int gap = 0; while (true){ if(r.nextInt(2) == 0 && kuohao > 0){ problem += "("; kuohao--; left++; gap = 0; } int number = r.nextInt(100)+1; problem += String.valueOf(number); gap++; if(r.nextInt(2) == 0 && left > 0 && gap >= 2){ problem += ")"; left--; } if(length >= 2 && kuohao <= 0 && left <= 0){ break; } problem += symbol[r.nextInt(4)]; length++; } if(length < 5 && gap < 3){ break; } } } else if(state == 2){ while (true) { problem = ""; int length = 0; int sym = r.nextInt(2) + 1; int kuohao = r.nextInt(2); int left = 0; int gap = 0; while (true) { if (r.nextInt(2) == 0 && kuohao > 0) { problem += "("; kuohao--; left++; gap = 0; } gap++; int number = r.nextInt(100) + 1; if (r.nextInt(2) == 0) { if (r.nextInt(2) == 0) { problem += (String.valueOf(number) + symbol[4]); } else { problem += (symbol[5] + String.valueOf(number)); } sym--; } else { problem += String.valueOf(number); } if (r.nextInt(2) == 0 && left > 0 && gap >= 2) { problem += ")"; left--; } if (length >= 2 && kuohao <= 0 && left <= 0) { break; } problem += symbol[r.nextInt(4)]; length++; } if (length < 5 && gap < 5) { break; } } } else if(state ==3){ while (true) { problem = ""; int length = 0; int sym = 1; int sym2 = r.nextInt(3) + 1; int kuohao = r.nextInt(3); int left = 0; int gap = 0; while (true) { if (r.nextInt(2) == 0 && kuohao > 0) { problem += "("; kuohao--; left++; gap = 0; } gap++; int number = r.nextInt(100) + 1; if (r.nextInt(3) == 0) { if (r.nextInt(2) == 0) { problem += (String.valueOf(number) + symbol[4]); } else { problem += (symbol[5] + String.valueOf(number)); } sym--; } else { if(r.nextInt(2) == 0){ int symloc = r.nextInt(3)+6; problem += (symbol[symloc] + String.valueOf(number)); sym2--; } else { problem += String.valueOf(number); } } if (r.nextInt(2) == 0 && left > 0 && gap >= 2) { problem += ")"; left--; } if (length >= 2 && sym <= 0 && sym2 <= 0 && kuohao <= 0 && left <= 0) { break; } problem += symbol[r.nextInt(4)]; length++; } if (length < 5 && gap < 5) { break; } } } problem += "="; return problem; } static HashSet<String> getPast(String name){ String path = dir+name; //要遍历的路径 File file = new File(path); //获取其file对象 if(!file.exists()){//如果没有该文件夹进行创建 file.mkdir(); } File[] fs = file.listFiles(); //遍历path下的文件和目录,放在File数组中 HashSet<String> pastProblem= 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中 pastProblem.add(line); } br.close(); } catch (Exception e) { e.printStackTrace(); } } } return pastProblem; } public void createF(user userInput) { while (true){ System.out.println("准备生成"+userInput.getGrade()+"题目,请输入生成题目数量:"); Scanner scanner = new Scanner(System.in); buf = scanner.next(); try { int num = Integer.parseInt(buf); while(num < 10 || num > 30){ if(num == -1){ System.out.println("退出"); Main.state = 0; return ; } else { System.out.println("输入范围有误,请重新输入"); } scanner = new Scanner((System.in)); num = scanner.nextInt(); } String name = userInput.getName(); //state = userInput.getGrade(); HashSet<String> past = getPast(name); String time = new SimpleDateFormat("yyyy-MM-dd-hh-mm-ss").format(new Date().getTime()); File file = new File(userInput.getName()); if(!file.exists()){ file.mkdir(); } String path = file + "\\" + time + ".txt"; try { FileWriter fw = new FileWriter(path,true); for(int i =0; i < num;i++){ String problem = getProblem(state); if(!past.contains(problem)){ problem = String.valueOf(i+1) + "." + problem; fw.write(problem + "\n" + "\n"); } } System.out.println("卷子生成完毕"); fw.close(); } catch (IOException e) { e.printStackTrace(); } } catch (Exception e) { return; } } } }
import javax.swing.*; import java.util.List; import java.util.Scanner; public class Main { public static int choice = 1; public static int state = 0; public static void main(String[] args) throws Exception { user userNow = new user(); paper paperTmp = new paper(); while (true){ if(choice == 1){ //login System.out.println("请输入用户名和密码:"); Scanner input = new Scanner(System.in); String str[] = input.nextLine().split("\\s+"); if(str.length < 2){ System.out.println("请输入正确的用户名、密码"); choice = 1; continue; } String name = str[0]; String password = str[1]; if(user.userHashMap.containsKey(name) && user.userHashMap.get(name).contains(password)){ userNow.setName(name); userNow.setGrade(user.userHashMap.get(name).get(1)); if(user.userHashMap.get(userNow.getName()).get(1) == "小学"){ state = 1; } if(user.userHashMap.get(userNow.getName()).get(1) == "初中"){ state = 2; } if(user.userHashMap.get(userNow.getName()).get(1) == "高中"){ state = 3; } paperTmp.setState(state); paperTmp.createF(userNow); choice = 2; } else { //cannot login System.out.println("请输入正确的用户名、密码"); choice = 1; continue; } } else if(choice == 2){ Scanner inputChange = new Scanner(System.in); String strChange = paper.buf; if(strChange.equals("切换为小学")){ state = 1; userNow.setGrade("小学"); } else if(strChange.equals("切换为初中")){ state = 2; userNow.setGrade("初中"); } else if(strChange.equals("切换为高中")){ state = 3; userNow.setGrade("高中"); } else{ System.out.println("输入错误,请重新输入"); } paperTmp.setState(state); paperTmp.createF(userNow); choice = 1; } if(state == 0){ userNow.setGrade(null); userNow.setName(null); choice = 1; continue; } } } }
·类的封装性好 由于我使用的是c++设计,虽然相对简洁明了,但封装是没有java优越的,类逻辑很好有利于代码复用
·功能实现非常完美,简洁的操作界面
·使用哈希表查重,有数据结构的功底
·对java语言的使用不太规范,例如类名大写,拼音命名法
·几乎没有注释,对于他人阅读来说是一大困难