ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

结对编程代码互评

2022-09-14 01:00:56  阅读:212  来源: 互联网

标签:结对 String userHashMap 编程 互评 state new problem public


一、介绍

本博客是对结对编程队友王同学编写的个人项目的分析评价,编写语言为java。

二、项目需求

用户:小学、初中、高中数学老师。

主要功能:输入用户名和密码登录,可以操作生成小初高数学卷子,题目不重复,保存在.TXT文件中。

题目要求:

小学 初中 高中
难度要求 +,-,*./ 平方、开方
备注 只能有+,-,*./和() 题目中至少有一个平方或开根号的运算符

三、代码分析

User类

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", "高中"));
    }

}


Paper类

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;
            }

        }
    }

}

main

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语言的使用不太规范,例如类名大写,拼音命名法
·几乎没有注释,对于他人阅读来说是一大困难

标签:结对,String,userHashMap,编程,互评,state,new,problem,public
来源: https://www.cnblogs.com/ygdsg/p/16691566.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有