1. 程式人生 > >學以致用——Java原始碼——小學中低年級四則運算練習及考試程式(Computer-Assisted Instruction)

學以致用——Java原始碼——小學中低年級四則運算練習及考試程式(Computer-Assisted Instruction)

這個程式用來給小孩做數學練習應該還是不錯的!

程式主要功能:

1. 可選擇練習模式或考試模式

    練習模式下可選擇四則運算的型別,考試模式下程式隨機選擇運演算法則,並具有評分功能

2. 每答完一道題,系統即時提示是否回答正確,並隨機輸出一些誇獎評語(回答正確時)或鼓勵性評語(回答錯誤時)

3. 考慮了小學中低年級學生的算術能力,根據運算型別限定了隨機數的範圍

 

程式碼如下:

package exercises.ch6Methods;

//Java How to Program, Exercise 6.35 -6.38  (Computer-Assisted Instruction)
//by 
[email protected]
/** * As computer costs decline, it becomes feasible for every student, regardless * of economic circumstance, to have a computer and use it in school. This * creates exciting possibilities for improving the educational experience of * all students worldwide, as suggested by the next five exercises. [Note: Check * out initiatives such as the One Laptop Per Child Project (www.laptop.org). * Also, research “green” laptops—what are some key “going green” * characteristics of these devices? Look into the Electronic Product * Environmental Assessment Tool (www.epeat.net), which can help you assess the * “greenness” of desktops, notebooks and monitors to help you decide which * products to purchase.] * * 6.35 (Computer-Assisted Instruction) The use of * computers in education is referred to as computer-assisted instruction (CAI). * Write a program that will help an elementary school student learn * multiplication. Use a SecureRandom object to produce two positive one-digit * integers. The program should then prompt the user with a question, such as * How much is 6 times 7? The student then inputs the answer. Next, the program * checks the student’s answer. If it’s correct, display the message "Very * good!" and ask another multiplication question. If the answer is wrong, * display the message "No. Please try again." and let the student try the same * question repeatedly until the student finally gets it right. A separate * method should be used to generate each new question. This method should be * called once when the application begins execution and each time the user * answers the question correctly. * * 6.36 (Computer-Assisted Instruction: Reducing * Student Fatigue) One problem in CAI environments is student fatigue. This can * be reduced by varying the computer’s responses to hold the student’s * attention. Modify the program of Exercise 6.35 so that various comments are * displayed for each answer as follows: Possible responses to a correct answer: * Very good! Excellent! Nice work! Keep up the good work! Possible responses to * an incorrect answer: No. Please try again. Wrong. Try once more. Don't give * up! No. Keep trying. Use random-number generation to choose a number from 1 * to 4 that will be used to select one of the four appropriate responses to * each correct or incorrect answer. Use a switch statement to issue the * responses. * * 6.37 (Computer-Assisted Instruction: Monitoring Student * Performance) More sophisticated computer-assisted instruction systems monitor * the student’s performance over a period of time. The decision to begin a new * topic is often based on the student’s success with previous topics. Modify * the program of Exercise 6.36 to count the number of correct and incorrect * responses typed by the student. After the student types 10 answers, your * program should calculate the percentage that are correct. If the percentage * is lower than 75%, display "Please ask your teacher for extra help.", then * reset the program so another student can try it. If the percentage is 75% or * higher, display "Congratulations, you are ready to go to the next level!", * then reset the program so another student can try it. * * 6.38 (Computer-Assisted * Instruction: Difficulty Levels) Exercises 6.35–6.37 developed a * computer-assisted instruction program to help teach an elementary school * student multiplication. Modify the program to allow the user to enter a * difficulty level. At a difficulty level of 1, the program should use only * single-digit numbers in the problems; at a difficulty level of 2, numbers as * large as two digits, and so on. 6.39 (Computer-Assisted Instruction: Varying * the Types of Problems) Modify the program of Exercise 6.38 to allow the user * to pick a type of arithmetic problem to study. An option of 1 means addition * problems only, 2 means subtraction problems only, 3 means multiplication * problems only, 4 means division problems only and 5 means a random mixture of * all these types. */ import java.security.SecureRandom; import java.util.Scanner; public class MathExaminer { // create secure random number generator private static final SecureRandom randomNumbers = new SecureRandom(); private static final int COUNT = 3; // plays one game of craps public static void main(String[] args) { int score = 0; // 學生成績 int mode; // 程式退出標誌 int count = 0; int operation = 1; Scanner input =new Scanner(System.in); do { count = 1; //考試開始前,重置計數器 System.out.print("歡迎你,未來的數學之王!,練習請輸入1,考試請輸入2(輸入-1退出):"); mode = input.nextInt(); //練習模式 if(mode ==-1) {System.out.print("已退出程式"); break; } if(mode == 1) { do { System.out.printf("%n請輸入四則運算的型別(1:加法, 2:乘法, 3:減法, 4:除法)(輸入-1退出):"); operation = input.nextInt(); if(operation ==-1) {System.out.printf("已退出練習模式%n%n"); break; } int correctAnswer = getQuestion(operation); int answer = input.nextInt(); boolean checkResult = check (answer, correctAnswer); chat(checkResult); } while (operation != -1); } //考試模式 else if (mode ==2) { System.out.printf("已進入考試模式,共%d道題目,滿分%d分%n",COUNT,COUNT*10); while (count <=COUNT) //開始考試後,需答完全部題目 { System.out.printf("%n題目%d:%n",count); int correctAnswer = getQuestion(); int answer = input.nextInt(); boolean checkResult = check (answer, correctAnswer); chat(checkResult); if (checkResult) score += 10; count++; } System.out.printf("考試結束,成績為%d%n%n",score); } } while (mode != -1); input.close(); } //輸出下一道試題 public static int getQuestion() { int operand1 = 1; int operand2 = 1; //考試模式下,運算型別隨機生成 int operatorNum = 1 + randomNumbers.nextInt(4); // 生成運算子(1,2,3,4 分別對應加、乘、減、除運算) //根據運算子,限定隨機數的範圍,控制試題難度 switch (operatorNum){ //加法控制在1000位以內 case 1: operand1 = 1 + randomNumbers.nextInt(1000); // 生成1000以內的隨機數作為運算元1 operand2 = 1 + randomNumbers.nextInt(1000); // 生成1000以內的隨機數作為運算元1 break; //乘法控制在100位以內 case 2: operand1 = 1 + randomNumbers.nextInt(100); // 生成100以內的隨機數作為運算元1 operand2 = 1 + randomNumbers.nextInt(100); // 生成100以內的隨機數作為運算元1 break; //減法控制在1000位以內,且保證被減數大於等於減數 case 3: operand1 = 1 + randomNumbers.nextInt(1000); // 生成1000以內的隨機數作為運算元1 operand2 = 1 + randomNumbers.nextInt(1000); // 生成1000以內的隨機數作為運算元2 while (operand1 < operand2) operand2 = 1 + randomNumbers.nextInt(1000); // 被減數小於減數時,重新生成減數 break; //被除數控制在1000位以內,除數控制在100以內,且保證被除數大於等於除數 case 4: operand1 = 1 + randomNumbers.nextInt(1000); // 生成1000以內的隨機數作為運算元1 operand2 = 1 + randomNumbers.nextInt(100); // 生成100以內的隨機數作為運算元2 while (operand1 < operand2) operand2 = 1 + randomNumbers.nextInt(100); // 被除數小於除數時,重新生成除數 break; } System.out.printf("%d %s %d = ",operand1, getOperatorText(operatorNum), operand2 ); //計算並返回正確答案 int answer = 0; switch (operatorNum){ case 1: answer = operand1 + operand2; break; case 2: answer = operand1 * operand2; break; case 3: answer = operand1 - operand2; break; case 4: answer = (int) (operand1 / operand2); //除法運算簡化位只需求商的整數部分 break; } return answer; } //輸出下一道練習題 public static int getQuestion(int operation) { int operand1 = 1; int operand2 = 1; int operatorNum = operation; //練習模式,運算模式由使用者指定 //根據運算子,限定隨機數的範圍,控制試題難度 switch (operatorNum){ //加法控制在1000位以內 case 1: operand1 = 1 + randomNumbers.nextInt(1000); // 生成1000以內的隨機數作為運算元1 operand2 = 1 + randomNumbers.nextInt(1000); // 生成1000以內的隨機數作為運算元1 break; //乘法控制在100位以內 case 2: operand1 = 1 + randomNumbers.nextInt(100); // 生成100以內的隨機數作為運算元1 operand2 = 1 + randomNumbers.nextInt(100); // 生成100以內的隨機數作為運算元1 break; //減法控制在1000位以內,且保證被減數大於等於減數 case 3: operand1 = 1 + randomNumbers.nextInt(1000); // 生成1000以內的隨機數作為運算元1 operand2 = 1 + randomNumbers.nextInt(1000); // 生成1000以內的隨機數作為運算元2 while (operand1 < operand2) operand2 = 1 + randomNumbers.nextInt(1000); // 被減數小於減數時,重新生成減數 break; //被除數控制在1000位以內,除數控制在100以內,且保證被除數大於等於除數 case 4: operand1 = 1 + randomNumbers.nextInt(1000); // 生成1000以內的隨機數作為運算元1 operand2 = 1 + randomNumbers.nextInt(100); // 生成100以內的隨機數作為運算元2 while (operand1 < operand2) operand2 = 1 + randomNumbers.nextInt(100); // 被除數小於除數時,重新生成除數 break; } System.out.printf("%d %s %d = ",operand1, getOperatorText(operatorNum), operand2 ); //計算並返回正確答案 int answer = 0; switch (operatorNum){ case 1: answer = operand1 + operand2; break; case 2: answer = operand1 * operand2; break; case 3: answer = operand1 - operand2; break; case 4: answer = (int) (operand1 / operand2); //除法運算簡化位只需求商的整數部分 break; } return answer; } //判斷答題的正確性 public static boolean check(int answer, int correctAnswer) { boolean checkResult = false; if (answer == correctAnswer) checkResult = true; else checkResult = false; return checkResult; } public static String getOperatorText(int operatorNum) { String operator =""; switch (operatorNum){ case 1: operator = "+"; break; case 2: operator = "×"; break; case 3: operator = "-"; break; case 4: operator = "÷"; break; } return operator; } // 輸出隨機聊天資訊 public static void chat(boolean checkResult) { int rightMsg = 1 + randomNumbers.nextInt(4); // 生成隨機誇獎評語編號 int wrongMsg = 1 + randomNumbers.nextInt(4); // 生成隨機鼓勵評語編號 //答對時顯示誇獎評語 final String RIGHT_MSG1 = "答對了,你很棒!"; final String RIGHT_MSG2 = "答對了,厲害!"; final String RIGHT_MSG3 = "答對了,了不起!"; final String RIGHT_MSG4 = "答對了,你就是未來的數學之王!"; //答錯時顯示鼓勵評語 final String WRONG_MSG1 = "哦哦,不對哦!"; final String WRONG_MSG2 = "哦哦,回答錯誤,答題要細心哦!"; final String WRONG_MSG3 = "哦哦,回答錯誤,不能再錯下去了!"; final String WRONG_MSG4 = "哦哦,回答錯誤,要多練哦!!"; //答對時顯示誇獎評語,答錯時顯示鼓勵評語 if (checkResult) { switch (rightMsg){ case 1: System.out.println(RIGHT_MSG1); break; case 2: System.out.println(RIGHT_MSG2); break; case 3: System.out.println(RIGHT_MSG3); break; case 4: System.out.println(RIGHT_MSG4); break; } } else { switch (wrongMsg){ case 1: System.out.println(WRONG_MSG1); break; case 2: System.out.println(WRONG_MSG2); break; case 3: System.out.println(WRONG_MSG3); break; case 4: System.out.println(WRONG_MSG4); break; } } } } // end class

運算結果:

歡迎你,未來的數學之王!練習請輸入1,考試請輸入2(輸入-1退出)1

 

請輸入四則運算的型別(1:加法, 2:乘法, 3:減法, 4:除法)(輸入-1退出)1

115 + 51 = 166

答對了,你很棒!

 

請輸入四則運算的型別(1:加法, 2:乘法, 3:減法, 4:除法)(輸入-1退出)2

53 × 22 = 1166

答對了,你就是未來的數學之王!

 

請輸入四則運算的型別(1:加法, 2:乘法, 3:減法, 4:除法)(輸入-1退出)3

329 - 120 = 219

哦哦,回答錯誤,要多練哦!!

 

請輸入四則運算的型別(1:加法, 2:乘法, 3:減法, 4:除法)(輸入-1退出)4

374 ÷ 2 = 187

答對了,你就是未來的數學之王!

 

請輸入四則運算的型別(1:加法, 2:乘法, 3:減法, 4:除法)(輸入-1退出)-1

已退出練習模式

 

歡迎你,未來的數學之王!,練習請輸入1,考試請輸入2(輸入-1退出)2

已進入考試模式,共3道題目,滿分30

 

題目1

642 + 138 = 780

答對了,你就是未來的數學之王!

 

題目2

33 ÷ 33 = 1

答對了,了不起!

 

題目3

191 ÷ 70 = 27

哦哦,回答錯誤,要多練哦!!

考試結束,成績為20