学以致用——Java源码——小学中低年级四则运算练习及考试程序(Computer-Assisted Instruction)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/hpdlzu80100/article/details/85234868

这个程序用来给小孩做数学练习应该还是不错的!

程序主要功能:

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

 

猜你喜欢

转载自blog.csdn.net/hpdlzu80100/article/details/85234868