时淺

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

1. 需求分析

  自动生成300道100以内的2个操作数的四则运算算式(+ - * /),要求运算结果也在100以内

2. 扩展功能分析

  -题目数量可设置

  -可设置数字的范围

  -可设置计算机结果的范围

  -直接生成文件,便于打印

  -顺便生成带有答案的文件,便于老师批改作业

3.代码实现

  3.1 代码包括无参构造方法和含参构造方法,使用无参构造方法即使用默认值,使用含参构造方法可以自行更改各种参数要求

    简单介绍一些变量:

      formulaTotal:生成的公式总数

      numberRange:随机生成数值范围

      maxResult:最大计算结果 

 1 import java.io.File;
 2 import java.io.FileWriter;
 3 import java.util.ArrayList;
 4 import java.util.Random;
 5 
 6 
 7 public class RandomFormula {
 8     int formulaTotal;
 9     int numberRange;
10     int maxResult;
11     
12     public RandomFormula() {
13         this.formulaTotal = 300;
14         this.numberRange = 100;
15         this.maxResult = 100;
16     }
17     public RandomFormula( int formulaTotal, int numberRange, int maxResult) {
18         this.formulaTotal = formulaTotal;
19         this.numberRange = numberRange;
20         this.maxResult = maxResult;
21     }
22 }

  3.2  产生随机运算数

1     public int getRandomNumber() {
2         Random rand = new Random();
3         return rand.nextInt(this.numberRange) + 1;
4     }

  3.3  产生随机运算符号

1     public String getRandomOperator() {
2         Random rand = new Random();
3         String[] operations = { "+", "-", "*", "/" };
4         return operations[rand.nextInt(4)];
5     }

  3.4 生成算术集合,计算结果并保存到praxis.txt和answer.txt文件

 1     public void output(){
 2         ArrayList<String> list = new ArrayList<String>();
 3         ArrayList<String> listS = new ArrayList<String>();
 4         while (list.size() <= this.formulaTotal) {
 5             String formula = "";
 6             String formulaS = "";
 7             int num1 = this.getRandomNumber();
 8             int num2 = this.getRandomNumber();
 9             String  operator = this.getRandomOperator();
10             int result = 0;
11             switch ( operator) {
12                 case "+":
13                     result = num1 + num2;
14                     break;
15                 case "-":
16                     result = num1 - num2;
17                     break;
18                 case "*":
19                     result = num1 * num2;
20                     break;
21                 case "/":
22                     result = num1 / num2;
23                     break;
24             }
25             formula = num1 + operator+ num2 ;
26             formulaS = num1 + operator + num2  + "=" + result;
27             
28         
29             
30             
31             if(this.maxResult>=result) {
32 //                System.out.print(formula);
33 //                System.out.print(formulaS+"\n");
34                 list.add(formula);
35                 listS.add(formulaS);
36             }
37             
38         }
39 //        System.out.print(list + "\n");
40 //        System.out.print(listS);
41     
42         //输出算式到文件
43         File file=new File("praxis.txt");
44         try {
45             FileWriter fw = new FileWriter(file);
46             for (String str : list) {
47                 fw.write(str + "\n");
48             }
49             fw.close();
50         } catch (Exception e) {
51             System.out.println("Error" + e.getMessage());
52             System.exit(0);
53         }
54             
55         File fileS=new File("answer.txt");
56         try {
57             FileWriter fw = new FileWriter(fileS);
58             for (String str : listS) {
59                 fw.write(str + "\n");
60             }
61             fw.close();
62         } catch (Exception e) {
63             System.out.println("Error" + e.getMessage());
64             System.exit(0);
65         }
66         
67     }

  3.5 测试代码

1 public class test {
2     public static void main(String[] args){
3         RandomFormula a = new RandomFormula();
4         a.output();
5     }
6 }

  3.6 结果预览

  

 

   3.7 笔者不足

    对于生成算式不可重复问题,目前没有解决,使用set接口可以确保不可重复,但是其具有存储无序性,即生成的praxis.txt和answer.txt文件无法一一对应,因此改为使用list接口,有提议的小伙伴可以在博客下留言哦

  

 

posted on 2020-10-08 00:51  时淺  阅读(298)  评论(0)    收藏  举报