第一次个人作业

作业

一、预估与实际| PSP2.1 | Personal Software Process Stages | 预估耗时(分钟) | 实际耗时(分钟) |

| --------------------------------------- | -------------------------------- | -------- | -------- |
| Planning | 计划 | 20 | 30 |
| • Estimate | • 估计这个任务需要多少时间 | 500 | 600 |
| Development | 开发 | 450 | 500 |
| • Analysis | • 需求分析 (包括学习新技术) | 30 | 60 |
| • Design Spec | • 生成设计文档 | 30 | 15 |
| • Design Review | • 设计复审 | 20 | 20 |
| • Coding Standard | • 代码规范 (为目前的开发制定合适的规范) | 20 | 15 |
| • Design | • 具体设计 | 60 | 150 |
| • Coding | • 具体编码 | 300 | 300 |
| • Code Review | • 代码复审 | 150 | 180 |
| • Test | • 测试(自我测试,修改代码,提交修改) | 300 | 400 |
| Reporting | 报告 | 30 | 20 |
| • Test Repor | • 测试报告 | 20 | 20 |
| • Size Measurement | • 计算工作量 | 20 | 20 |
| • Postmortem & Process Improvement Plan | • 事后总结, 并提出过程改进计划 | 50 | 30 |
| | | 合计 | 2000 |2370 |

二、需求分析

我通了解到,小学一年级数学有如下的几个特点:

循序渐进
要注重题目的简易程度
使用的数字不大于100
一年级加减法运算结果均为整数;
二年级乘除运算结果都为正数;
经过分析,我认为,这个程序应当:
基本的要求就是小学的加减乘除的四则运算,所以题目的主体应该是判断使用者的年级,
从而判断使用者做题的难易程度,若为一年级,则做加减法,若为二年级,则出加减乘除

三、设计

1. 设计思路

说明你如何设计这个程序

将要输入的n个题的n存在args[0]中
设置随机输入的函数
将运行结果存在out.txt
grade1运行一年级的加减,grade2运行二年级的代码

2. 实现方案

比如:

  • 准备工作:先在Github上创建仓库,克隆到本地...
  • 技术关键点:梳理一下设计思路,可能遇到哪些技术关键点
  • ...

四、编码

用args[0]和args[1]数组来实现从命令行中接收到的参数
生成随机数Manth.random()

1. 调试日志

如果数组越界,就扩大数组
本来题目是数量是10,运行后只输出5套题,原因是用了同一个循环变量i来统计题号和参与循环,导致集合越界

2. 关键代码

private void outPut() {
    File file = new File("out.txt");//创建文本
    Date date = new Date();/
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
    String currentTime = "\r\n         211606352   陈彬   " + sdf.format(date);
    byte[] bytesdate = currentTime.getBytes();

    if(!file.exists()){ 
        try {
            file.createNewFile();
        } catch (IOException e) {// TODO 自动生成的 catch 块
            System.out.println("文件创建时出现错误!");
        }
    }
    
    try {
        String str = "\r\n";
        byte[] bytes = str.getBytes();      
        byte[] bytescut = cutLine.getBytes();
        
        FileOutputStream fos = new FileOutputStream(file);  
        for (int i = 0; i < count; i++) {
            byte[] b_MathQuestion = str_MathQuestion[i].getBytes();
            fos.write(b_MathQuestion);
            fos.write(bytes);
        }
        fos.write(bytescut);
        fos.write(bytes);
        fos.flush();    
        for (int i = 0; i < count; i++) {
            byte[] b_MathAnswer = str_MathAnswer[i].getBytes();
            fos.write(b_MathAnswer);
            fos.write(bytes);
        }
        fos.write(bytesdate);
        fos.flush();
        fos.close();    
        
        System.out.print("-------  本次共生成" + count + "道小学"+ grade + "年级算数题,请打开out.txt文件查看详情    -------"); 


    } catch (FileNotFoundException e) {
        System.out.println("未找到指定文件!");
    } catch (IOException e) {
        System.out.println("文件写入有误!");
    } 
    
}


private void mathProblem(int count) {   
    Random rnd = new Random();
    
    for (int i = 0; i < count; i++) {
        symbol = rnd.nextInt(grade*2);
        FN = (int)(Math.random()*20+1);
        SN = (int)(Math.random()*20+1);
        
        if(grade == 1){
            switch (symbol) {
            case 0:
                add(FN,SN,i);
                break;
                
            case 1:
                sub(FN,SN,i);
                break;
                
            default:
                break;
            }
        } else {
            switch (symbol) {
            case 0:
                add(FN,SN,i);
                break;
                
            case 1:
                sub(FN,SN,i);
                break;
                
            case 2:
                mul(FN,SN,i);
                break;
                
            case 3:
                div(i);
                break;
                
            default:
                break;
            }
        }
        
    }
}

private void add(int n1, int n2,int i) {
    result = n1 + n2;
    str_MathQuestion[i] = "( " + (i+1) +" ) " + n1 + " + " + n2 + " = ";
    str_MathAnswer[i] = "( " + (i+1) +" ) " + n1 + " + " + n2 + " = " + result;
}


private void sub(int n1, int n2,int i) {
    if (n1 < n2) {      
        int num;
        num = n1;
        n1 = n2;
        n2 = num;
    }
    result = n1 - n2;
    str_MathQuestion[i] = "( " + (i+1) +" ) " + n1 + " - " + n2 + " = ";
    str_MathAnswer[i] = "( " + (i+1) +" ) " + n1 + " - " + n2 + " = " + result;
}


private void mul(int n1, int n2,int i) {
    if (n1 > 9) {
        n1 = (int)(Math.random()*10);
    } 
    if (n2 > 9) {
        n2 = (int)(Math.random()*10);
    }
    result = n1 * n2;
    str_MathQuestion[i] = "( " + (i+1) +" ) " + n1 + " x " + n2 + " = ";
    str_MathAnswer[i] = "( " + (i+1) +" ) " + n1 + " x " + n2 + " = " + result;
}


private void div(int i) {
    while(true){
        int n1 = (int)(Math.random()*82);
        int n2 = (int)(Math.random()*81+1);
        if(n1 % n2 == 0){
            result = n1 / n2;
            str_MathQuestion[i] = "( " + (i+1) +" ) " + n1 + " / " + n2 + " = ";
            str_MathAnswer[i] = "( " + (i+1) +" ) " + n1 + " / " + n2 + " = " + result;
        }else if(n1 % n2 != 0){
            result = n1 / n2;
            str_MathQuestion[i] = "( " + (i+1) +" ) " + n1 + " / " + n2 + " = ";
            str_MathAnswer[i] = "( " + (i+1) +" ) " + n1 + " / " + n2 + " = " + result + "..." + (n1 % n2);
        }
        break;
    }
}


public static void main(String[] args) {
    if(args.length == 0){
        new MathExam6352();
    } else if(args.length == 1){
        new MathExam6352(args[0]);
    }
    else if(args.length == 2){
        new MathExam6352(args[0], args[1]);
    }else {
        new MathExam6352(args[0], args[1]);
    }
    
}

}

3. 代码规范

请给出本次实验使用的代码规范:

-第一条:变量都需要先赋予初始值
-第二条:代码中的命名严禁使用拼音与英文混合,更不许使用中文命名。
-第三条:不要使用一个常量类维护所有常量,按常量功能进行归类,分开维护。

并人工检查代码是否符合规范

五、测试

输入:3 1输出结果: 3道小学1年级的乘除算法题目
输入:a3 2 输出结果: 输入有误
输入:3 a1 输出结果:输入有误
输入:13 1 2 输出结果: 输入有误

六、总结

欠缺经常编代码的习惯 开学第一次作业做得烂

猜你喜欢

转载自www.cnblogs.com/FOREVER17olds/p/9631825.html