• 四则运算题目生成程序(基于控制台)2.0


    码市地址:https://git.coding.net/qwx_hh/java-szys.git

    需求分析:

      为了做一个小学生四则运算题目生成程序,根据小学生的计算水平,我以100以内的整数二元四则运算和真分数的二元四则运算为出题范围,因为是命令行程序,小学生做题,界面要求简易,输出数值不能有错误。判断正确与否,在答题结束后得出正确率。

    基本功能:

      1.除了整数以外,还支持真分数的四则运算,真分数的运算

      2.运算符为 +, −, ×, ÷

      3.并且要求能处理用户的输入,并判断对错,打分统计正确率

      4.要求能处理用户输入的真分数

      5.使用 -n 参数控制生成题目的个数

    扩展功能:

      1.在开始程序前先验证命令行传入参数是否正确

      2.使用人性化的提示方式

      3.当答案错误时提示错误并输出正确答案

    高级功能:

          1.在d盘创建文件夹Myapp并形成一个histroy.txt文件储存有答题错误的题目与答案

      2.在正常完成题目并出现正确率后会提示是否复习之前错误的题目

    设计实现:

    Java类有:

      1.Test

      2.Arithmetic

      3.Histroy

      Test类负责代码的运行和调用Arithmetic和Histroy中的方法,验证答案并输出正确率。Arithmetic类负责出题以及运算并输出结果。Histroy类负责错误题目的读取与保存。Test类:输出方法。Arithmetic类有方法:整数运算方法,分数运算方法,公约数方法,输出字符串。Histroy类:创建文件方法,更新文件方法,读取文件方法。

    代码说明:

    Test类:

     1 package szys;
     2 
     3 import java.io.FileNotFoundException;
     4 import java.util.Random;
     5 import java.util.Scanner;
     6 
     7 public class Test {
     8     static int rn=0;
     9     static int wn=0;
    10     public static void main(String[] args) throws FileNotFoundException {
    11         // TODO Auto-generated method stub
    12         if(args[0].equals("-n")&&args[1].matches("\d*"))
    13         {
    14             System.out.printf("%n");
    15             System.out.println("要开始了哦,试试看(。_。):");
    16             System.out.printf("%n");
    17             int i = Integer.parseInt(args[1]);
    18             Histroy hs = new Histroy();
    19             hs.Histroy_create();
    20             for(int a=0;a<i;a++)
    21             {
    22                 boolean x= new Random().nextBoolean();
    23                 Arithmetic hh = new Arithmetic(x);
    24                 String int_str = hh.int_operation();
    25                 String fra_str = hh.fra_operation();
    26                 if(x==true)
    27                     print(hh.toString(),int_str,hs);
    28                 if(x==false)
    29                     print(hh.toString(),fra_str,hs);
    30             }
    31             double tru=rn;
    32             double tot=i;
    33             System.out.printf("%n正确率:%f",tru/tot*100);
    34             System.out.println("%");
    35             rn=0;wn=0;
    36             System.out.println("复习?(N/Y)");
    37             Scanner scanner = new Scanner(System.in);
    38             String a2 = scanner.next();
    39             if(a2.equals("Y"))
    40                 review();
    41             else
    42                 System.exit(0); 
    43         }
    44         else
    45         {
    46             System.out.println("别调皮,参数有问题");
    47             System.exit(0); 
    48         }
    49     }
    50     public static void print(String toString,String str,Histroy hs){
    51         hs.scan(str,toString);
    52         System.out.printf(toString);
    53         Scanner scanner = new Scanner(System.in);
    54         String a1 = scanner.next();
    55         if(str.equals(a1))
    56         {
    57             rn++;
    58             System.out.println("        (≧▽≦)正确");
    59         }
    60         else
    61         {
    62             hs.Histroy_save();
    63             wn++;
    64             System.out.printf("        (>﹏<)错误%n        答案%s%n",str);
    65         }
    66     }
    67     public static void review() throws FileNotFoundException {
    68         Histroy hs = new Histroy();
    69         hs.Histroy_read();
    70         System.out.printf("题量多少?");
    71         System.out.printf("(最高%d题)",hs.astrlist.size());
    72         Scanner scanner = new Scanner(System.in);
    73         int n = scanner.nextInt();
    74         for(int i=0;i<n;i++)
    75         {
    76             int b = new Random().nextInt(hs.astrlist.size())%(hs.astrlist.size()-1+1) + 1;
    77             Histroy hs2 = new Histroy();
    78             print(hs.qstrlist.get(b-1), hs.astrlist.get(b-1),hs2);
    79         }
    80             double tru=rn;
    81             double tot=n;
    82             System.out.printf("%n正确率:%f",tru/tot*100);
    83             System.out.println("%");
    84             System.out.println("再来?(Y/N)");
    85             Scanner scanner2 = new Scanner(System.in);
    86             String a2 = scanner.next();
    87             if(a2.equals("Y"))
    88                 review();
    89             else
    90                 System.exit(0);
    91     }
    92 }

    Arithmetic类:

      1 package szys;
      2 
      3 import java.util.Random;
      4 
      5 public class Arithmetic {
      6     int a = new Random().nextInt(4);
      7     int b = new Random().nextInt(10)%(10-1+1) + 1;
      8     int c = new Random().nextInt(10)%(10-2+1) + 2;
      9     int d = new Random().nextInt(10)%(10-1+1) + 1;
     10     int e = new Random().nextInt(10)%(10-2+1) + 2;
     11     int f = new Random().nextInt(100);
     12     int g = new Random().nextInt(100);
     13     String astr="";
     14     String qstr="";
     15     boolean x;
     16     public Arithmetic(boolean x) {
     17         this.x=x;
     18     }
     19     public String int_operation()
     20     {
     21         int result = 0;
     22         if(a==0)
     23             result=f+g;
     24         if(a==1)
     25             result=f-g;
     26         if(a==2)
     27             result=f*g;
     28         astr = String.valueOf( result);
     29         if(a==3)
     30         {
     31             if(g==0)
     32             {
     33                 astr=int_operation();
     34                 return astr;
     35             }
     36             else
     37             {
     38                 if(g!=0&&g!=1){
     39                     int d=common_divisor(f,g);
     40                     f=f/d;
     41                     g=g/d;
     42                     astr = (f+"/"+g);
     43                 }
     44                 if(g==1)
     45                     astr=(""+f);
     46             }
     47             
     48         }
     49         return astr;
     50     }
     51     public String fra_operation(){
     52         this.b = new Random().nextInt(10)%(10-1+1) + 1;
     53         this.c = new Random().nextInt(10)%(10-2+1) + 2;
     54         this.d = new Random().nextInt(10)%(10-1+1) + 1;
     55         this.e = new Random().nextInt(10)%(10-2+1) + 2;
     56         if(c<b||e<d||c%b==0||e%d==0)
     57         {
     58             astr=fra_operation();
     59             return astr;
     60         }
     61             
     62         int fz=1,fm=c*e;
     63         if(a==0)
     64             fz=b*e+c*d;
     65         if(a==1){
     66             fz=b*e-c*d;
     67             if(fz==0)
     68             {
     69                 return astr=("0");
     70             }
     71         }
     72             
     73         if(a==2)
     74             fz=b*d;
     75         if(a==3)
     76         {
     77             fz=b*e;
     78             fm=c*d;
     79         }
     80         int f=common_divisor(fm,fz);
     81         if(f>0){
     82             fm=fm/f;
     83             fz=fz/f;
     84         }
     85         if(f<0){
     86             fm=-fm/f;
     87             fz=-fz/f;
     88         }
     89         astr = (fz+"/"+fm);
     90         return astr;
     91         
     92     }
     93     public static int common_divisor(int m,int n)
     94     {
     95         while(m%n!=0){
     96             int t=m%n;
     97             m=n;
     98             n=t;
     99         }
    100         return n;
    101     }
    102     public String toString(){
    103         if(x==true){
    104             if(a==0)
    105                 qstr=(f+"+"+g+"=");
    106             if(a==1)
    107                 qstr=(f+"-"+g+"=");
    108             if(a==2)
    109                 qstr=(f+"×"+g+"=");
    110             if(a==3)
    111                 qstr=(f+"÷"+g+"=");
    112         }
    113         if(x==false){
    114             if(a==0)
    115                 qstr=(b+"/"+c+"+"+d+"/"+e+"=");
    116             if(a==1)
    117                 qstr=(b+"/"+c+"-"+d+"/"+e+"=");
    118             if(a==2)
    119                 qstr=(b+"/"+c+"×"+d+"/"+e+"=");
    120             if(a==3)
    121                 qstr=(b+"/"+c+"÷"+d+"/"+e+"=");
    122         }
    123         return qstr;
    124     }
    125     
    126 }

    Histroy类

     1 package szys;
     2 
     3 import java.io.File;
     4 import java.io.FileNotFoundException;
     5 import java.io.FileWriter;
     6 import java.io.IOException;
     7 import java.util.ArrayList;
     8 import java.util.List;
     9 import java.util.Scanner;
    10 
    11 public class Histroy {
    12     String qstr;
    13     String astr;
    14     String str;
    15     public List<String> qstrlist = new ArrayList<String>();
    16     public List<String> astrlist = new ArrayList<String>();
    17     public void scan(String qstr,String astr){
    18         str=(astr+" "+qstr);
    19     }
    20     public void Histroy_create(){
    21         String path = "d:\Myapp";
    22         File f = new File(path);
    23         if(!f.exists()){
    24             f.mkdirs();
    25             String fileName="histroy.txt";
    26             File file = new File(f,fileName);
    27             if(!file.exists()){
    28                 try {
    29                 file.createNewFile();
    30                 } catch (IOException e) {
    31                 // TODO Auto-generated catch block
    32                 e.printStackTrace();
    33                 }
    34             }
    35         }
    36         
    37     }
    38     public void Histroy_save(){
    39          FileWriter writer;
    40          String fileName = ("d:\Myapp\histroy.txt");
    41         try {
    42             writer = new FileWriter(fileName, true);
    43             writer.write(str);
    44             writer.write("
    ");
    45             writer.close();
    46         } catch (IOException e) {
    47             // TODO Auto-generated catch block
    48             e.printStackTrace();
    49         }
    50     }
    51     public void Histroy_read()  throws FileNotFoundException{
    52         Scanner in = new Scanner(new File("d:\Myapp\histroy.txt"));//为历史信息.tit这个File创建一个扫描器in
    53         while(in.hasNextLine()){
    54             String line = in.nextLine();//读出历史信息.txt的下一行
    55             Scanner lineScanner = new Scanner(line);//为每一行建立一个扫描器
    56             lineScanner.useDelimiter(" ");//使用空格作为分隔符
    57             qstr = lineScanner.next();//问题
    58             astr = lineScanner.next();//答案
    59             qstrlist.add(qstr);
    60             astrlist.add(astr);
    61         }
    62         in.close();
    63     }
    64 }

    测试运行:

    命令行参数错误:

    存储文件

    psp:

    Personal Software Process Stages

    Time (%) Senior Student

    Time (%)

    计划

    5

    5

    估计这个任务需要多少时间

    5

    5

    开发

    85

    70

    需求分析 (包括学习新技术)

    10

    10

    生成设计文档

    0

    0

    设计复审

    0

    0

    代码规范

    10

    5

    具体设计

    15

    15

    具体编码

    40

    30

    代码复审

    10

    10

    测试(自我测试,修改代码,提交修改)

    15

    25

    报告

    10

    15

    测试报告

    2

    2

    计算工作量

    1

    1

    并提出过程改进计划

    2

    2

    小结:通过此次项目实践我又丰富了我的Java编程经验,还锻炼了我的写报告的能力(笑),今后会为认真地完成每一次项目而努力。我觉得在代码规范方面2.0相比初代来说有了很大的提高。

  • 相关阅读:
    判断正整数的正则表达式
    ALL、ALLNOBLANKROW、VALUES、DISTINCT 去重的区别
    正则匹配双字节字符
    才子佳人小说研究.PDF
    第二性台版 第一卷 事实与迷思
    第二性 合卷本 横本.PDF
    架构的生态系 资讯环境被如何设计至今.PDF
    年羹尧奏摺专集(下).PDF
    第二性 合卷本 横本.EPUB
    小僧の神様・城の崎にて.PDF
  • 原文地址:https://www.cnblogs.com/qwx-hh/p/6490264.html
Copyright © 2020-2023  润新知