Java实验项目二——小学生考试系统(简单四则运算)

时间:2024-03-05 22:14:07

Program:设计实现一个小学生数学考试系统,完成随机出题(简单的四则运算),学生答题,自动判分的功能。

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 }