考試系統
阿新 • • 發佈:2017-09-24
pack bsp float class 小學 math con continue 設計
設計實現一個小學生數學考試系統,完成隨機出題(簡單的四則運算),學生答題,自動判分的功能。
1 package experiment; 2 3 import java.util.Scanner; 4 5 6 public class Arithmetic { 7 public static void main(String args[]) { 8 begin_test(); 9 } 10 //隨機產生數字 11 public static double r_random(int min, int max) { 12 doubleii; 13 ii = (double)(min + Math.random() * (max - min + 1)); 14 return ii; 15 } 16 //隨機產生符號 17 public static char make_sym() 18 { 19 int ii; 20 ii = (int)(Math.random() * 3); 21 if(ii == 0) 22 return ‘+‘; 23 else if(ii == 1) 24return ‘-‘; 25 else if(ii == 2) 26 return ‘*‘; 27 else 28 return ‘/‘; 29 } 30 //隨機產生式子 31 public static int make_formula(int min, int max) 32 { 33 int ii, ij; 34 char operator; 35 36 37 ii = (int)r_random(min, max); 38 ij = (int)r_random(min, max); 39 40 operator = make_sym(); 41 System.out.println("隨機產生的四則運算為:" + ii + " " + operator + " " + ij + " = ?"); 42 if(operator == ‘+‘) 43 return (ii + ij); 44 else if(operator == ‘-‘) 45 return (ii - ij); 46 else if(operator == ‘*‘) 47 return (ii * ij); 48 else 49 { 50 return Math.round(ii / ij); 51 } 52 } 53 54 //判斷結果是否正確 55 public static boolean test( int min, int max) 56 { 57 double answer, r_answer; 58 System.out.println("請輸入答案(註意除法時答案四舍五入取整):"); 59 Scanner sc = new Scanner(System.in); 60 answer = make_formula(min, max); 61 r_answer = sc.nextDouble(); 62 if(answer == r_answer) { 63 System.out.println("正確!"); 64 return true; 65 } 66 else { 67 System.out.println("錯誤!"); 68 return false; 69 } 70 } 71 72 //計分 73 public static double score(int num, int min, int max) { 74 double sum = 0; 75 double one_score = (float)(100 / num); 76 for(int i = 0; i < num; i++) { 77 if(test(min, max)) { 78 sum += one_score; 79 } 80 else { 81 continue; 82 } 83 } 84 return sum; 85 } 86 //考試開始 87 public static void begin_test() { 88 int num, min, max; 89 double sum; 90 Scanner sc = new Scanner(System.in); 91 System.out.println("請輸入需要產生的試題數:"); 92 num = sc.nextInt(); 93 System.out.println("請輸入產生四則運算的範圍(最小值和最大值)"); 94 min = sc.nextInt(); 95 max = sc.nextInt(); 96 sum = score(num, min, max); 97 System.out.println("考試結束,最後得分為:" + sum); 98 } 99 }
考試系統