1. 程式人生 > >Java實驗項目二——小學生考試系統(簡單四則運算)

Java實驗項目二——小學生考試系統(簡單四則運算)

pro tint scanner 運算 表達 his ++ private blog

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 }

Java實驗項目二——小學生考試系統(簡單四則運算)