其他分享
首页 > 其他分享> > 计应193张跃 四则运算系统

计应193张跃 四则运算系统

作者:互联网

计划:

做一个可以随机出题的程序,小学题目,做完并且有得分。

开发:

开发

1.需求分析

作为一个小学生的家长(老师),我想让我的孩子(学生),能在家里自主学习算法,并自己认识到错误,希望能有这样的一个程序

2.生成设计文档

3.设计复审

审核一下设计文档

4.代码规范

要有一些注释,注意缩进,及时分行,代码清晰

5.具体设计

用户可以选择1位数的加法或减法,2位数的加法或减法或混合运算

运算题目随机生成

练习完成后,给出得分

6.具体编码

public static void main(String[] args) {

    System.out.println("请选择类型\n 1.1位数加减法    2.2位数加减法    3.混合加减法  【-1.退出练习】");

    Scanner scanner = new Scanner(System.in);

 

    String type = scanner.nextLine();

 

    if (type == null || type.isEmpty())

        System.out.println("未选择练习类型,程序退出");

    else if (type.length() > 1) {

        if (type.equals("-1"))

            System.out.println("程序退出");

        else

            System.out.println("无此选项,程序退出");

    } else {

        try {

            int t = Integer.valueOf(type);

            if (t < -1 || t > 3)

                System.out.println("无此选项,程序退出");

            else {

                int sum = 0; // 定义总题数

                int okSum = 0; // 定义正确题数

                Random random = new Random(); // 随机数对象

                int a = 0, b = 0, okRes, userRes;// 定义运算的变量

                List<String> errs = new ArrayList<String>();

                 

                while (true) {

                    // 运算开始

                    if (t == 1) { // 生成 0 -9

                        a = random.nextInt(10);

                        b = random.nextInt(10);

                    } else if (t == 2) { // 生成 10 -99

                        a = random.nextInt(90) + 10;

                        b = random.nextInt(90) + 10;

                    } else if (t == 3) { // 生成 0 -99

                        a = random.nextInt(100);

                        b = random.nextInt(100);

                    }

 

                    if ((a - b) >= 0) { // 小学生可能没学过负数,排除下

                        int flag = random.nextInt(2);

 

                        String errInfo = "";

                        String info = "请输入 " + a;

                         

                        if (flag == 0) {

                            okRes = a + b;

                            info += " + " + b + "的答案 【-1.退出练习】:";

                            errInfo = a + " + " + b + "的答案: ";

                        } else {

                            okRes = a - b;

                            info += " - " + b + "的答案 【-1.退出练习】:";

                            errInfo = a + " - " + b + "的答案: ";

                        }

 

                        System.out.print(info);

                        String userResStr = scanner.nextLine();

 

                        if (userResStr == null || userResStr.isEmpty()) {

                            sum++;

                            errs.add(errInfo + "是 " + okRes + " 你的答案是: " + userResStr);

                        }

 

                        userRes = Integer.valueOf(userResStr);

 

                        if (userRes == -1) {

                            System.out.println();

                             

                            if (sum == okSum)

                                System.out.println("******一共 " + sum + " 道题目你全作对了.");

                            else {

                                System.out.println("******一共 " + sum + " 道题目, 对了 " + okSum + "道,错题集:");

                                for(int i = 0;i < errs.size();i++) {

                                    System.out.println("第 " + (i  + 1) + " 道错题 [" + errs.get(i) + "]");

                                }

                            }

 

                            break;

                        } else {

                            sum++;

 

                            if (userRes != okRes) {

                                errs.add(errInfo + "是 " + okRes + " 你的答案是: " + userResStr);

                            } else {

                                okSum++;

                            }

                        }

                    }

                }

            }

        } catch (Exception e) {

            System.out.println("无此选项,程序退出");

        }

    }

}

标签:张跃,random,System,193,else,nextInt,计应,println,out
来源: https://www.cnblogs.com/xiaolixiaojiao/p/14833477.html