zoukankan      html  css  js  c++  java
  • 第三次作业 四则运算

    作业要求地址:http://www.cnblogs.com/xiangxuer/p/9695909.html

    github地址:https://github.com/lzic/arithmetic

    前言:

      上软件工程课中出现了一道例子,即阿超做了一个简单的四则运算用于给他儿子出题,为了练手,将在于此模仿阿超将简单的四则运算做出来,该四则运算为初步版本,仅仅包含正整数的加减乘除,不包括括号。

    题目要求:

     

      任何编程语言都可以,命令行程序接受一个数字输入,然后输出相应数目的四则运算题目和答案。例如输入数字是 30, 那就输出 30 道题目和答案。 运算式子必须至少有两个运算符,运算数字是在 100 之内的正整数,答案不能是负数。 如:23 - 3 * 4 = 11

    所用版本:

      操作系统:windows10

      开发环境:intellij IDEA

      开发语言:java

     实现思路:

      让系统随机生成两个运算符,和生成三个数0-100的数,拼接成四则运算式。如果有/,则被除数不能为0,根据被除数,生成除数。

    个人软件过程耗时估计与统计表:

    Personal Software Process Stages Time Senior Student Time
    计划 2 3
    估计这个任务需要多少时间 4 6
    开发 1 2
    需求分析 (包括学习新技术) 0.5 0.5
    生成设计文档 0.5 0.5
    设计复审 0.5 0
    代码规范 0 0.5
    具体设计 0.5 0.5
    具体编码 1 2
    代码复审 0.5 0.5
    测试(自我测试,修改代码,提交修改) 0.5 0.5
    报告 0.5 0.5
    测试报告 0.5 0.5
    计算工作量 2 2
    并提出过程改进计划 1 1

    实现代码:

    public class Arithmetic {

    private static int a;
    private static int b;
    private static int c;
    private static String symbolA;
    private static String symbolB;


    public static void main(String[] args) {
    int result = 0;
    System.out.println("请输入题目数量:");
    int scan = new Scanner(System.in).nextInt();
    for (int i = 0; i < scan; i++) {
    a = (int) (Math.random() * 100);
    b = (int) (Math.random() * 100);
    c = (int) (Math.random() * 100);
    symbolA = randomSymbol();
    symbolB = randomSymbol();
    if (symbolA.equals("/")) {
    b = (int) (Math.random() * 100) + 1;
    a = b * ((int)(Math.random() * 100));
    while (a > 100) {
    a = b * ((int)(Math.random() * 100));
    }
    if(symbolB.equals("/")){
    c = (int) (Math.random() * 100) + 1;
    while ((a/b)%c!=0){
    c = (int) (Math.random() * 100) + 1;
    }
    }
    }
    if(symbolA.equals("*")&&symbolB.equals("/")){
    c = (int) (Math.random() * 100) + 1;
    while ((a*b)%c!=0){
    c = (int) (Math.random() * 100) + 1;
    }
    }
    if((symbolA.equals("+")||symbolA.equals("-"))&&symbolB.equals("/")){
    c = (int) (Math.random() * 100) + 1;
    b = c * ((int)(Math.random() * 100));
    while (b > 100) {
    b = c * ((int)(Math.random() * 100));
    }
    }
    result = calculate(a, b, c, symbolA, symbolB);
    if (result < 0) {
    i--;
    continue;
    }

    String equaltion = a + symbolA + b + symbolB + c + "=";
    System.out.println(equaltion);
    if(result==new Scanner(System.in).nextInt()){
    System.out.println("答案正确");
    }else {
    System.out.println("答案错误");
    System.out.println("正确答案是:"+result);
    }
    System.out.println(result);

    }
    System.out.println(calculate(1, 60, 49, "*", "/"));
    }

    public static int calculate(int a, int b, int c, String symbolA, String symbolB) {
    int result = 0;
    if ((symbolA.equals("+") || symbolA.equals("-")) && (symbolB.equals("*") || symbolB.equals("/"))) {
    if (symbolB.equals("*")) {
    result = (b * c);
    } else if (symbolB.equals("/")) {
    if(c==0) return -1;
    result = (b / c);
    }
    if (symbolA.equals("-")) {
    result = a - result;

    } else {
    result = a + result;
    }
    } else {
    if (symbolA.equals("+")) {
    result = (a + b);
    } else if (symbolA.equals("-")) {
    result = (a - b);
    } else if (symbolA.equals("*")) {
    result = (a * b);
    }else {
    if(b==0) return -1;
    // if(a<b) return -1;
    // if(a%b!=0) return -1;
    result = (a / b);
    }
    if (symbolB.equals("+")) {
    result = result + c;
    } else if (symbolB.equals("-")) {
    result = result - c;
    } else if (symbolB.equals("*")) {
    result = result * c;
    } else {
    if(c==0) return -1;

    result = result / c;
    }
    }
    return result;
    }

    public static String randomSymbol() {
    String add = "+";
    String decrease = "-";
    String multiply = "*";
    String divide = "/";
    int ran = (int) (Math.random() * 4);
    if (ran == 0) {
    return add;
    } else if (ran == 1) {
    return decrease;
    } else if (ran == 2) {
    return multiply;
    } else {
    return divide;
    }
    }
    }

    运行结果:

     

  • 相关阅读:
    spring配置文件中util:properties和context:property-placeholder
    为啥Spring和Spring MVC包扫描要分开?
    spring和springmvc父子容器关系
    springmvc请求参数获取的几种方法
    请求转发(Forward)和重定向(Redirect)的区别
    如何设计一个秒杀系统
    Maven使用之packing篇
    ECharts基本设置
    @RequestParam详解以及加与不加的区别
    从网页提取的关键字
  • 原文地址:https://www.cnblogs.com/97lzc/p/9765606.html
Copyright © 2011-2022 走看看