软件工程第一次作业,小学生四则运算的出题程序

一、背景

    阿超有个儿子上小学二年级,老师每天让家长给孩子出30道加减法题,虽然不多,但是每天都做也算是个负担,阿超作为一个老牌程序员当然想用计算机来解决这个小问题,目前对于这个问题对于任何语言都不是问题,比如:

      C/C++、C#、Java、Python、VB、JavaScript、Perl……

    具体要求如下:

  • 能自动生成小学四则运算题目(注意是给小学生用的,要是结果出现负数的话他们会迷茫的!)
  • 除了整数外,还要支持真分数的四则运算

    请大家用任何一种自己擅长的语言来编写这段程序,并把程序的介绍和自己编写的过程写一个博客

二、分析

   (一) 自己擅长的是c语言,准备用c语言,但是自己学的java,想用java试试,支持真分数运算,如果用C语言,我们可以这么考虑,a,b,c,d随机生成。

a/b      c/d,

(1)可能存在a,b,c,d;a和b存在公约数,c和d存在公约数。

解决办法:先求a和b的最大公约数m,先求c和d的最大公约数n,然后a=a/m;b=b/m;c=c/m;d=d/m;,然后就可以算加"+"法(a*d+b*c)/(b*d);减“-”法(a*d-b*c)/(b*d);乘“*”法a*c/(b*d);除法“/”,判断一下分母是否为0,分数符号直接输出;

(2)可能存在a>b,c>d的情况,真假分数情况。输出直接输出符号“/”。

(二)有判断正确和错误,每答一次就判断一次,回答正确和回答错误,一次性答对是10分,答两次才答对得5分,答三次才答对得3分。

(三)输入一个数,知道出题的数目,随机产生的题数目,多输也会提示输入错误。

三、代码部分

我用的是java写的:

复制代码
import java.util.InputMismatchException;
import java.util.Random;
import java.util.Scanner;
import java.io.*;

public class Pratices {
public static void main(String[] args) {
  new Pratices().list_Pratices();
  }

  public int random_Num(int range) {
  return (int) (Math.random() * range);
  }
  public void list_Pratices() {
    int right = 0;
    int wrongtimes = 0;  
    int num_1, num_2, temp;
  int type = random_Num(4);
  int score = 0;  
    int count = 1;
    System.out.println("请输入题目数量:");
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
  while (count <= n) {
  type = random_Num(2);
  num1 = random_Num(100);  //100以内随机数
  num2 = random_Num(100);  //100以内的随机数
  wrongtimes = 0;

  if (type == 0)
    {
  System.out.print("(" + count + ")  " + num1 + " + " + num2+ " = ");//加法
  }
    else if(type == 1)
    {
  if ((num1 < num2))
     {
  temp = num1;
  num1 = num2;
  num2 = temp;
  }
  System.out.print("(" + count + ")  " + num1 + " - " + num2+ " = ");//减法
  }
    else if(type == 2)

  System.out.print("(" + count + ")  " + num1 + " * " + num2+ " = ");//乘法
    }
      else if(type == 3)
    {
        if(num2!=0)
  System.out.print("(" + count + ")  " + num1 + " / " + num2+ " = ");//除法
      else
        System.out.println("分母为零");
    }

  int answer = this.getAnswer(count);
  boolean flag = check(num1, num2, type, answer, count);
  if (flag) {
  right++;
  System.out.println("回答正确");
  score += this.getScore(wrongtimes);
  } else {
  while (wrongtimes < 2) {
  wrongtimes++;
  System.out.println("回答错误 " + wrongtimes + " 次");
  answer = this.getAnswer(count);
  flag = check(num1, num2, type, answer, count);
  if (flag) {
  score += this.getScore(wrongtimes);
  right++;
  wrongtimes = 0;
  break;
  }
  }
  if (wrongtimes == 3)
  System.out.println("回答错误 ");
  else
  System.out.println("回答正确");
  }
  count++;
  }
  System.out.println("回答正确 : " + right);
  System.out.println("回答错误: " + (10 - right));
  System.out.println("获得分数: " + score);
  System.out.println(getDegree(score));
  }

  public boolean check(int num_1, int num_2, int type, int my_Answer,
  int count) {
  int answer = 0;
  if (type == 1) {
  answer = num_1 - num_2;
  } else if (type == 0) {
  answer = num_1 + num_2;
  }
  return my_Answer == answer;
  }
  public int getAnswer(int count) {
  int my_Answer = 0;
  BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  try {
  my_Answer = Integer.parseInt(br.readLine());
  } catch (IOException e) {
  e.printStackTrace();
  } catch (NumberFormatException e) {
  System.out.println("输入有误");
  return 0;
  } finally {
  if (count >= n && (br != null)) {//不会超出输入的n
  try {
  br.close();
  } catch (IOException e) {
  e.printStackTrace();
  }
  br = null;
  }
  }
  return my_Answer;
  }
  public int getScore(int wrongtimes) {
  if (wrongtimes == 0) {
  return 10;
  } else if (wrongtimes == 1) {
  return 7;
  } else if (wrongtimes == 2) {
  return 5;
  } else
  return 0;
  }

  public String getDegree(int score) {
  if (score > 90)
  return "SMART";
  else if (score > 80)
  return "GOOD";
  else if (score > 70)
  return "OK";
  else if (score > 60)
  return "PASS";
  else
  return "TRY AGAIN";
  }
  }
复制代码
posted @   saucxs  阅读(265)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· [AI/GPT/综述] AI Agent的设计模式综述
点击右上角即可分享
微信分享提示