Java実験項目2——小学生試験システム(単純四則演算)
21163 ワード
Program:小学生の数学試験システムを実現して、ランダムな出題(簡単な四則演算)を完成します。学生は問題を解いて、自動採点の機能があります。
Description:コードは以下の通りです。
Description:コードは以下の通りです。
1 /*
2 * Description: TestSystem
3 *
4 * */
5
6 package entity;
7
8 public class TestSystem {
9
10 private int num1; //
11 private int num2;
12 private String operateEle; //
13 private static int grade = 0; //
14
15 //
16 public TestSystem() {
17
18 }
19
20 //
21 public TestSystem(int num1,int num2,String operateEle) {
22
23 this.num1 = num1;
24 this.num2 = num2;
25 this.operateEle = operateEle;
26 }
27
28 // setter() getter()
29 public int getNum1() {
30 return num1;
31 }
32
33 public void setNum1(int num1) {
34 this.num1 = num1;
35 }
36
37 public int getNum2() {
38 return num2;
39 }
40
41 public void setNum2(int num2) {
42 this.num2 = num2;
43 }
44
45 // toString()
46 public String toString() {
47
48 return this.num1 + this.operateEle + this.num2;
49 }
50
51 //
52 public int getResult() {
53
54 int result = 0;
55 switch(this.operateEle) {
56
57 case "+": result = this.num1 + this.num2;break;
58
59 case "-": result = this.num1 - this.num2;break;
60
61 case "*": result = this.num1 * this.num2;break;
62
63 case "/": result = this.num1 / this.num2;break;
64 }
65
66 return result;
67 }
68
69 //
70 public static void getGrade() {
71
72 System.out.println( " , :" + TestSystem.grade );
73 }
74
75 // ,
76 public static void setGrade(int grade) {
77
78 TestSystem.grade += grade;
79 }
80
81 //
82 public static void clear() {
83
84 TestSystem.grade = 0;
85 }
86
87
88 }
1 /*
2 * Description: Operate,
3 *
4 * */
5
6
7 package tools;
8
9 import java.util.Random;
10 import java.util.Scanner;
11
12 public class Operate {
13
14 // , 100
15 public static int getRandom() {
16
17 Random ran = new Random();
18
19 return ran.nextInt(100);
20 }
21
22 // ,
23 public static String getOperateEle() {
24
25 String[] operateEle = {"+","-","*","/"};
26 Random ran = new Random();
27
28 return operateEle[ran.nextInt(4)];
29 }
30
31
32 // ,
33 public static int getInput() {
34
35 Scanner scan = new Scanner(System.in); // Scanner
36 int result = 0;
37
38 System.out.println( " :" );
39 result = scan.nextInt();
40 return result;
41
42 }
43
44
45 }
1 /*
2 * Description: , , 20
3 *
4 * Written By:Cai
5 *
6 * Date Written:2017-09-25
7 *
8 * */
9
10 package main;
11
12 import tools.Operate; //
13 import entity.TestSystem;
14
15 public class DemoTwo4 {
16
17 public static void main(String args[]) {
18
19 int i = 0; //
20 int inputResult = 0; //
21 do {
22 // TestSystem
23 TestSystem ts = new TestSystem(Operate.getRandom(),Operate.getRandom(),Operate.getOperateEle());
24 System.out.println(ts); //
25 inputResult = Operate.getInput(); //
26 if( inputResult == ts.getResult() ) { // ,
27
28 System.out.println( " !" );
29 TestSystem.setGrade(20);
30 }else { //
31
32 System.out.println( " !" );
33 }
34 i++;
35
36 }while(i < 5); // ( )
37
38 TestSystem.getGrade(); //
39 TestSystem.clear(); //
40
41 }
42 }