zoukankan      html  css  js  c++  java
  • 《构建之法》第二次作业

    需求分析

    功能需求:

    1、能自动生成小学四则运算题目。

    2,能满足用户的需求,让用户想测多少道就测多少道

    3、能对用户的答案进行判断对错。

    4、能算出用户答对了多少道。

    设计

    1、定义s为一组1至4的随机数,使用which(s)语句,case s 分别为四则运算的加减乘除。在case 语句中定义两个变量,为a和b,使它们能产生0至100之间的随机数,用来做四则运算。

    2、which语句外层在编写一个while语句,使得which能循环多次,进行多个计算。

    3、定义一个h记录做题正确的数目,初始值为0。

    4,程序中还设置一个用户想进行的多少个四则运算。

    5,通过数字1,2,3,4,能让用户判断自己在进行什么运算。

    代码实现:

     1 import java.io.BufferedReader;
     2 import java.io.IOException;
     3 import java.io.InputStreamReader;
     4 import java.util.Scanner;
     5 
     6 public class czy5 {
     7     public static int suiji(int num1, int num2) {
     8         int C = (int) num1 + (int) (Math.random() * (num2 - num1));
     9         return C;
    10     }
    11 
    12     public static void main(String[] args) {
    13 
    14         int S, a, b, i = 1, f = 0, h = 0, c;
    15         String ss;
    16 
    17         double D;
    18         while (i <= 5) {
    19             S = suiji(1, 4);
    20             i = i + 1;
    21             a = suiji(0, 100);
    22             b = suiji(0, 100);
    23 
    24             System.out.println("你想输出四则运算的个数是:");
    25             Scanner in = new Scanner(System.in);
    26             int num = in.nextInt();
    27 
    28             System.out.println("请输入4个数,1代表加法,2代表减法,3代表乘法,4代表除法");
    29             try {
    30                 BufferedReader b1 = new BufferedReader(new InputStreamReader(
    31                         System.in));
    32                 ss = b1.readLine();
    33                 c = Integer.parseInt(ss);
    34             } catch (IOException e) {
    35             }
    36 
    37             switch (S) {
    38             case 0:
    39                 break;
    40             case 1: {
    41                 System.out.println(a + "+" + b + "=");
    42                 Scanner reader = new Scanner(System.in);
    43                 D = reader.nextInt();
    44                 if (D == a + b) {
    45                     h++;
    46                 } else {
    47                     System.out.println("答错了,正确答案是:" + (a + b));
    48 
    49                 }
    50             }
    51             case 2: {
    52                 System.out.println(a + "-" + b + "=");
    53                 Scanner reader1 = new Scanner(System.in);
    54                 D = reader1.nextInt();
    55                 if (D == a - b) {
    56                     h++;
    57                 } else {
    58                     System.out.println("答错了,正确答案是:" + (a - b));
    59 
    60                 }
    61             }
    62             case 3: {
    63                 System.out.println(a + "*" + b + "=");
    64                 Scanner reader2 = new Scanner(System.in);
    65                 D = reader2.nextInt();
    66                 if (D == a * b) {
    67                     h++;
    68                 } else {
    69                     System.out.println("答错了,正确答案是:" + (a * b));
    70 
    71                 }
    72             }
    73             case 4: {
    74                 System.out.println(a + "/" + b + "=");
    75                 Scanner reader3 = new Scanner(System.in);
    76                 D = reader3.nextInt();
    77                 if (D == a / b) {
    78                     h++;
    79                 } else {
    80                     System.out.println("答错了,正确答案是:" + (a / b));
    81 
    82                 }
    83             }
    84             }
    85 
    86             System.out.print("你答对的总题数为:" + h + "");
    87         }
    88     }
    89 }

    PSP耗时图

    反思:这个在每个运算里面都要进行一次用户输入答案与正确答案比较,觉得特别麻烦,但是不知道怎么实现,而且生成的数字不知道为什么四个都一样,很想得到老师的帮助,改好这个程序

  • 相关阅读:
    GC 的认识
    SSRF 攻击技术
    文件包含漏洞
    文件的上传和下载
    XSS
    SQL注入工具 sqlmap
    自动化测试框架
    mac配置环境变量
    pycharm与git想集成 上传下载代码
    测试人员需要掌握的linux基本操作
  • 原文地址:https://www.cnblogs.com/CZY134717/p/4415540.html
Copyright © 2011-2022 走看看