JAVA實現簡單的四則運算


GitHub 項目地址

https://github.com/745421831/-/tree/master


 

PSP

 

PSP2.1

Personal Software Process Stages

預估耗時(分鍾)

實際耗時(分鍾)

Planning

計划

10

20

· Estimate

· 估計這個任務需要多少時間

10

10

Development

開發

360

600

· Analysis

· 需求分析 (包括學習新技術)

30

40

· Design Spec

· 生成設計文檔

30

40

· Design Review

· 設計復審 (和同事審核設計文檔)

10

15

· Coding Standard

· 代碼規范 (為目前的開發制定合適的規范)

5

5

· Design

· 具體設計

40

80

· Coding

· 具體編碼

300

500

· Code Review

· 代碼復審

60

120

· Test

· 測試(自我測試,修改代碼,提交修改)

180

120

Reporting

報告

120

60

· Test Report

· 測試報告+博客

120

120

· Size Measurement

· 計算工作量

10

10

· Postmortem & Process Improvement Plan

· 事后總結, 並提出過程改進計划

40

30

合計

 

1325

1770

 

項目要求

 

  • 能自動生成小學四則運算題目
  • 除了整數以外,還要支持真分數的四則運算

 

解題思路

  • 了解四則運算的基本法則
  • 利用隨機函數隨機生成數字以及運算符
  • 用戶輸入答案程序需要判斷答案是否正確
  • 支持真分數運算

 

設計實現及代碼說明

      1、Arithmetic.java

        主函數

 

 1 package jisuanwork;
 2 
 3 import java.util.Scanner;
 4 
 5 public class Arithmetic {
 6     public static void main(String[] args) {
 7         Scanner scanner = new Scanner(System.in);
 8         System.out.println("請輸入產生幾以內的數字:");
 9         int num = scanner.nextInt();
10         System.out.println("請輸入產生多少道題目:");
11         int t = scanner.nextInt();
12         Subject subject = new Subject();
13         subject.set(num, t);
14         
15     }
16 }

 

         2、Subject.java

               隨機生成的四則運算表達式

  1 package jisuanwork;
  2 
  3 import java.util.Random;
  4 import java.util.Scanner;
  5 
  6 public class Subject {
  7     void set (int num , int t){
  8         Random random = new Random();
  9         Scanner scanner = new Scanner(System.in);
 10         JiSuan j = new JiSuan();
 11         
 12         String arithmetic[] = new String[t];
 13         for(int i = 0 ; i < t ; i++) {
 14             int a = (int)random.nextInt(num);//分子
 15             int b = (int)random.nextInt(num);//分母
 16             int c = (int)random.nextInt(num);//另一個分子
 17             int d = (int)random.nextInt(num);//另一個分母
 18             int fuhao = random.nextInt(4);//0代表+,1代表-,2代表乘,3代表除
 19             if(b!=0 && d!=0) {
 20                 if(fuhao==0) {
 21                     int fenzi = a*d + b*c;
 22                     int fenmu = b*d;
 23                     arithmetic[i] = biaodashi(a,b) + '+' + biaodashi(c,d) + '=';
 24                     System.out.println((i+1)+":"+arithmetic[i]);
 25                     j.anw(fenzi, fenmu);
 26                     
 27                 }
 28                 if(fuhao==1 && a*d - b*c >= 0) {
 29                     int fenzi = a*d - b*c;
 30                     int fenmu = b*d;
 31                     arithmetic[i] = biaodashi(a,b) + '-' + biaodashi(c,d) + '=';
 32                     System.out.println((i+1)+":"+arithmetic[i]);
 33                     j.anw(fenzi, fenmu);
 34                 }
 35                 if(fuhao==1 && a*d - b*c < 0) {
 36                     int fenzi = b*c - a*d;
 37                     int fenmu = b*d;
 38                     arithmetic[i] = biaodashi(c, d) + '-' + biaodashi(a, b) + '=';
 39                     System.out.println((i+1)+":"+arithmetic[i]);
 40                     j.anw(fenzi, fenmu);
 41                 }
 42                 if(fuhao==2) {
 43                     int fenzi = a*c;
 44                     int fenmu = b*d;
 45                     arithmetic[i] = biaodashi(a, b) + '×' + biaodashi(c, d) + '=';
 46                     System.out.println((i+1)+":"+arithmetic[i]);
 47                     j.anw(fenzi, fenmu);
 48                 }
 49                 if(fuhao==3 && c!=0) {
 50                     int fenzi = a*d;
 51                     int fenmu = b*c;
 52                     arithmetic[i] = biaodashi(a, b) + '÷' + biaodashi(c, d) + '=';
 53                     System.out.println((i+1)+":"+arithmetic[i]);
 54                     j.anw(fenzi, fenmu);
 55                 }
 56                 if(fuhao==3 && c==0) {
 57                     i--;
 58                 }
 59             }
 60             else {
 61                 b=1;
 62                 d=1;
 63                 if(fuhao==0) {
 64                     int fenzi = a*d + b*c;
 65                     int fenmu = b*d;
 66                     arithmetic[i] = a + "+" +c + "=";
 67                     System.out.println((i+1)+":"+arithmetic[i]);
 68                     j.anw(fenzi, fenmu);
 69                 }
 70                 if(fuhao==1 && a*d - b*c >= 0) {
 71                     int fenzi = a*d - b*c;
 72                     int fenmu = b*d;
 73                     arithmetic[i] = a + "-" +c + "=";
 74                     System.out.println((i+1)+":"+arithmetic[i]);
 75                     j.anw(fenzi, fenmu);
 76                 }
 77                 if(fuhao==1 && a*d - b*c < 0) {
 78                     int fenzi = b*c - a*d;
 79                     int fenmu = b*d;
 80                     arithmetic[i] = c + "-" +a + "=";
 81                     System.out.println((i+1)+":"+arithmetic[i]);
 82                     j.anw(fenzi, fenmu);
 83                 }
 84                 if(fuhao==2) {
 85                     int fenzi = a*c ;
 86                     int fenmu = b*d;
 87                     arithmetic[i] = a + "×" +c + "=";
 88                     System.out.println((i+1)+":"+arithmetic[i]);
 89                     j.anw(fenzi, fenmu);
 90                 }
 91                 if(fuhao==3 && c!=0) {
 92                     int fenzi = a*d ;
 93                     int fenmu = b*c;
 94                     arithmetic[i] = a + "÷" +c + "=";
 95                     System.out.println((i+1)+":"+arithmetic[i]);
 96                     j.anw(fenzi, fenmu);
 97                 }
 98                 if(fuhao==3 && c==0) {
 99                     i--;
100                 }
101                 
102             }
103         }
104     }
105     public static String biaodashi(int a,int b) {//判斷假分數,並化假分數為帶分數
106         if(a>=b) {
107             int c;
108             c = a/b;
109             int d;
110             d = a%b ;
111             if(d==0)
112                 return c+"";
113             else
114                 return a+"/"+b;
115         }
116         else {
117             return a+"/"+b;
118         }
119     }
120 }

  3、JiSuan.java

             計算隨機生成的表達式的答案,並對用戶輸入的答案進行判斷對錯

 1 package jisuanwork;
 2 
 3 import java.util.Scanner;
 4 
 5 public class JiSuan {
 6     void anw(int fenzi,int fenmu){
 7         Scanner scanner = new Scanner(System.in);
 8         
 9         double sum = (double)fenzi/fenmu;
10         
11         String anw = scanner.nextLine();
12         String string[] =anw.split("/");
13         double [] result =new double[string.length];
14         for(int i=0;i<string.length ;i++) {
15             result[i]=Double.parseDouble(string[i]);
16         }
17         double sum1 =0;
18         if(string.length == 1 ) {
19             sum1 = (int) result[0];
20         }
21         else {
22             sum1 = result[0]/result[1];
23         }
24     
25         if(sum==sum1)
26             System.out.println("true");
27         else {
28             System.out.println("false");
29             System.out.println("正確答案為:"+sum);
30         }
31         
32     }
33 }

 

測試運行

 

總結

這次利用JAVA實現簡單的四則運算還不夠完美,比如沒有隨機生成帶括號的,沒有隨機生成帶多個運算符的式子,由於時間原因以及個人能力有限,這些只能以后慢慢完善。同時這次的四則運算也充分的顯示了自己基礎薄弱,大部分學過的知識都有遺忘,所以需要好好的回去復習基礎知識。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM