ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

计应193张跃 四则运算系统

2021-05-31 22:03:55  阅读:190  来源: 互联网

标签:张跃 random System 193 else nextInt 计应 println out


计划:

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

开发:

开发

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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有