zoukankan      html  css  js  c++  java
  • 软件工程个人作业02

    自动出题系统提出了新的要求:
    1、题目避免重复;
    2、可定制(数量/打印方式);
    3、可以控制下列参数:

    〇是否有乘除法;
    〇是否有括号(最多可以支持十个数参与计算);
    〇数值范围;
    〇加减有无负数;
    〇除法有无余数!

    设计思路:在第一次设计的基础上,又添加了三个要求,前两个是在算法完成后实现,第三个是在算法当中实现,所以我先考虑第三个要求。第三个要求是使参数可控,第一个程序中已经定义随机运算符+,-,*,/为0,1,2,3,所以如果选择无乘除法则从0(+),1(-)中进行随机选取,不加入2(*),3(/)。在百度“小学四则运算”后,我发现五~六年级的题的乘除法只有两位数或一位数的,而加减法有一~四位数的,所以数值范围我准备在整数加减法中1~4位可控,而在整数乘除法中1~2位可控。有无括号可在多位运算中进行插入。由于小学加减法中没有负数,负数是初中的知识,所以题目中没有,答案中也不能有,在遇到减法中如果被减数小于减数则两数交换位置。控制重复我用了string的方法,把每道题作为一个string,并两两比较,如果重复重新随机题目。输出方式有控制台输出和用txt文档输出至d盘。

    源代码:

    import java.io.FileNotFoundException;
    import java.util.*;//引用util包
    
    public class Yunsuan {
    
        public static void main(String[] args) throws FileNotFoundException  {
            // TODO Auto-generated method stub
        
            int numtishu;
            int numchangdu;
            int judgezhengfen;
            int judgechengchu;
            int judgekuohao;
            int judgeyushu = 0;
            int judgeweishu = 0;
            int judgefangshi = 0;
            String last = new String();
            
            Scanner sca = new Scanner(System.in);
            Yunsuan01 y = new Yunsuan01(); 
    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~判断类型~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~            
            while(true){
                System.out.println("真分数运算(1),整数运算(0):");
                judgezhengfen = sca.nextInt();
                if(judgezhengfen!=1 && judgezhengfen!=0)
                {
                    System.out.println("输入有误,请重新输入");
                }
                if(judgezhengfen == 0)
                {
                    while(true)
                    {
                        System.out.println("位数:四位数(3),三位数(2),两位数(1),一位数(0):");
                        judgeweishu = sca.nextInt();
                        if(judgeweishu!=1 && judgeweishu!=0 && judgeweishu!=2&&judgeweishu!=3)
                        {
                            System.out.println("输入有误,请重新输入");
                        }
                        else break;
                    }
                    break;
                }
                else break;
            }
            
            while(true){
                System.out.println("有乘除法(1),无乘除法(0)");
                judgechengchu = sca.nextInt();    
                if(judgechengchu!=1 && judgechengchu!=0)
                {
                    System.out.println("输入有误,请重新输入");
                }
                if(judgechengchu == 1)
                {
                    while(true)
                    {
                        System.out.println("除法有余数(1),无余数(0)");
                        judgeyushu = sca.nextInt();
                        if(judgeyushu!=1 && judgeyushu!=0)
                        {
                            System.out.println("输入有误,请重新输入");
                        }
                        else break;
                    }
                    break;
                } 
                else break;
            }
            
            while(true){
                System.out.println("有括号(1),无括号(0)");
                judgekuohao = sca.nextInt();    
                if(judgekuohao!=1 && judgekuohao!=0)
                {
                    System.out.println("输入有误,请重新输入");
                }
                else break;
            }            
                
                while(true)
                {
                    System.out.println("输入有几个数参与运算(2~10):");
                    numchangdu = sca.nextInt();//输入题的长度
                    if(numchangdu<2 || numchangdu>10)
                    {
                        System.out.println("输入有误,请重新");
                    }
                    else break;
                }
                
                System.out.println("输入题目的数量:");
                numtishu = sca.nextInt();//输入题数
                String result[] = new String[numtishu];    
                
                for(int i=0; i<numtishu;i++)
                {
                    result[i]="";
                }
                while(true){
                    System.out.println("打印方式:txt格式(1),控制台(0):");
                    judgefangshi = sca.nextInt();    
                    if(judgekuohao!=1 && judgekuohao!=0)
                    {
                        System.out.println("输入有误,请重新输入");
                    }
                    else break;
                }    
                
                
                
    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~给出算式~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                if(judgezhengfen == 0  &&  judgekuohao == 0){
                //整数,无括号                            
                    for(int i=0; i<numtishu; i++)
                    {
                        int k = i+1;
                        int num[] = new int[numchangdu];
                        char chars[] = new char[numchangdu-1];
                        result[i] = result[i]+"["+k+"]";
    
                        for(int j=0;j<numchangdu;j++)
                        {
                            num[j] = y.weishu(judgeweishu);//按位数出随机数
                        }
                        for(int j=0;j<numchangdu-1;j++)
                        {
                            chars[j] = y.fuhao(judgechengchu);//按是否乘除出符号
                        }
                        for(int j=0;j<numchangdu-1;j++)
                        {
                            if(chars[j] == '-')//减法不出现负数
                            {
                                if(num[j]<num[j])
                                {
                                    int temp = num[j];
                                    num[j] = num[j+1];
                                    num[j+1] = temp;
                                }
                            }
                        }
                        
                        for(int j=0;j<numchangdu-1;j++)
                        {
                            result[i] = result[i]+num[j]+chars[j];
                        }
                        result[i] += num[numchangdu-1];
                        
                        result[i] += "="+"
    " ;
                    }
                    
                    for(int j = 0; j<numtishu;j++)
                    {
                        last = last + result[j];
                    }
                    y.dayinfangshi(judgefangshi, last);//最终输出
                }
    main
    import java.util.Random;
    import java.io.*;
    
    public class Yunsuan01 {//判断出题是否重复
        int chongfu(int numtishu,String result[],int i)
        {
            for(int j=0;j<i;j++)
            {
                    if(result[i] == result[i-j-1]);
                    i = i-1;
            }
            return i;
        }
        
        int weishu(int judgeweishu)//随机位数
        {
            int numsuiji = 0;
            Random ran = new Random();//初始化随机数
            if(judgeweishu == 0)
            {
                numsuiji = ran.nextInt(10);
            }
            else if(judgeweishu == 1)
            {
                numsuiji = ran.nextInt(100);
            }
            else if(judgeweishu == 2)
            {
                numsuiji = ran.nextInt(1000);
            }
            else if(judgeweishu == 3)
            {
                numsuiji = ran.nextInt(10000);
            }
            return numsuiji;
        }
        char fuhao(int judgechengchu)
        {
            char fuhao = ' ';
            Random ran = new Random();//初始化随机数
            int suijijiajian = ran.nextInt(2);
            int suijichengchu = ran.nextInt(4);
            if(judgechengchu == 0)
            {
                if(suijijiajian == 0)
                    fuhao = '+';
                if(suijijiajian == 1)
                    fuhao = '-';
            }
            if(judgechengchu == 1)
            {
                if(suijichengchu == 0)
                    fuhao = '+';
                if(suijichengchu == 1)
                    fuhao = '-';
                if(suijichengchu == 2)
                    fuhao = '*';
                if(suijichengchu == 3)
                    fuhao = '/';
            }
            return fuhao ;
        }
        void dayinfangshi(int judgefangshi,String last) throws FileNotFoundException
        {
            if(judgefangshi == 1)
            {
             FileOutputStream  fs = new FileOutputStream(new File("D:\四则运算.txt"));
             PrintStream p = new PrintStream(fs);
             p.print(last);
            }
             if(judgefangshi == 0)
                 System.out.println(last);
        }
    }
    class

    结果截图:

    每周计划:

      听课 编程 读书 总计
    周一 2 2 0 4
    周二 0 1 0 1
    周三 0 1 0.5 1.5
    周四 0 1 0 1
    周五 0 1 0.5 1.5
    周六 0 1 0 1
    周日 0 1 0 1

    时间记录日志:

    日期 开始时间 结束时间 中断时间(min) 净时间(min) 活动 备注
    3.14 8:00 9:50 10 100 上课 课间休息十分钟
    13:30 15:30 0 120 编程 学习用php播放音乐和插入网页背景等
    3.15 13:30 14:30 0 60 装系统  用启动盘装系统失败,改用虚拟光驱
               
    3.16 12:00 12:20 10 10 读书  吃饭
    16:00 16:30 10 20 读书  
    20:00 20:20 10 10 读书  
    3.17 19:30 20:20 0 50 编程  
    3.18 19:30 20:30 10 50 编程  
    22:00 22:30 0 30 读书  
    3.19 13:40 15:40 0 120 编程  
    总计 / / 50 520 /  

    缺陷记录日志:暂无

  • 相关阅读:
    lamp架构之一键编译安装lamp搭建wordpress和discuz脚本(基于centos)
    更换公网IP导致wordpress网站无法正常访问解决方法
    centos7一键编译安装Apache2.4.46脚本
    redis之RDB备份脚本
    一键编译安装redis5.0.9脚本(centos)
    一键二进制安装mysql8.0.19脚本(shell)
    c语言 扫雷游戏(才做了三分之一)
    C语言 数据结构单链表(未解决版)
    JAVA final关键字的使用
    JAVA 520 无限循环I love you
  • 原文地址:https://www.cnblogs.com/xiaosongbiog/p/5294863.html
Copyright © 2011-2022 走看看