Java language to achieve primary school math exercises

Java language to achieve primary school math exercises

Topic:
[Problem description]
Write a program to help primary school students practice mathematics, and help primary school students practice four mathematical operations within 100: addition, subtraction, multiplication, and division.

[Basic requirements]
a) The program should first ask for the user's ID number (the ID number includes two uppercase letters and 4 digits), for example:
please enter the user ID number: AB1234
should verify the entered ID number and meet the ID number requirements format, then the program prompts three options:
(1) start the test
(2) check the fraction
(3) to exit
b) test: this program 10 will be given math, for example:
12 * 3 = 36
48 + 32 = 80
"
56/28=2

Note:
i) Students will answer each question in turn (the answer is given after the equal sign), and then give the next question.
ii) The test questions should contain four mathematical operations: addition, subtraction, multiplication, and division. They are generated randomly. Adjacent problems should be different operations, and
each operation must occur at least once.
iii) Randomly generate numbers for each question, but you must ensure that the numbers and results involved in the calculation are both less than 100 and greater than zero.
iv) After completing the ten questions, record the time it takes for students to complete the ten questions.
v) Give each student a score. Save the student's ID, grades and usage time to a file named record.txt.
vi) Output the following information on the screen:
Question|Correct answer|Your answer
c) Score check: List all historical scores of the student from the file "record.txt". For example:
your previous record is:
AB1234 80 150 seconds
AB1234 50 182 seconds
AB1234 90 98 seconds

Java code:

package per.java.shejiti;

import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;

public class XiaoXueShuXueJiSuan{
    
    
	static Scanner sc = new Scanner(System.in);                      
	static int score;        // 分数
	static long time;        // 时间
	static int book;         // 控制操作不重复
	static String [] achievement = new String[11];     // 成绩单
	static int index = 0;           // 第 index 次成绩
	public static void main(String[] args) {
    
    
		
		System.out.println("请输入用户名ID:");                       // 输入用户名ID
		String str = sc.next();
		
		while(!judge(str)) {
    
              		// 判断用户名ID是否正确, 错误重新输入直到用户名ID正确为止
			System.out.print("请重新输入用户名ID: ");
			str = sc.next();
		}
		System.out.println("用户名正确!");
		try {
    
    
			// 此处我放在了这个位置,可以更换
			File file = new File("E:\\Java语言\\Java语言代码\\javaexample//record");
			if(!file.isFile())                                         // 如果没有该文件,就新建一个
				file.createNewFile();
			FileWriter fileWriter = new FileWriter(file, true);        // 通过 true 设置追加
			fileWriter.write("你以前的记录是: \n");
			fileWriter.flush();                                        // 清空缓存区,压入文件
			fileWriter.close();
			boolean flag = true;
			List<Integer> list = new ArrayList<Integer>();      
			for(int i = 0;i < 4;i++)             // 初始化数组,控制前四个运算
				list.add(i);
			while(flag){
    
     
				System.out.println("请选择:\n(1)开始测试\n(2)检查分数\n(3)退出");
				int choice = sc.nextInt();
				switch(choice) {
    
    
					case 1:{
    
                                                 // 开始测试
						score = 0;                                      // 重置分数为 0 
						book = list.get(3);                             // 标记上一次运算
						long start = System.currentTimeMillis();        // 开始时间
						Collections.shuffle(list);                      // 对数组乱序,保证前四次运算乱序
						for(int j = 0;j < 10;j++) 
							if(j < 4) calculate(list.get(j));           // 前四次包括 +-*/ 保证每个至少出现一次
							else calculate();
						long end = System.currentTimeMillis();                // 结束时间
						time = (end - start) / 1000;                          // 共用时间
						fileWriter = new FileWriter(file, true);              // 通过 true 设置追加
						fileWriter.write(str + "  " + score + "  " + time + " 秒\n");
						fileWriter.flush();                                   // 清空缓存区,压入文件
						fileWriter.close();                                   // 关闭写入流
						
						System.out.println("\n问题      | 正确答案  | 你的答案 ");
						for(int i = 0;i < 10;i++) 
							System.out.println(achievement[i]);    // 输出每次成绩
						System.out.println();
						index = 0;                                 // 重置成绩单
						
						break;
					}
					case 2:{
    
    
						FileReader fileReader = new FileReader(file);
						BufferedReader in = new BufferedReader(fileReader);
						String s;
						while((s = in.readLine()) != null)     // 读取文件全部内容
							System.out.println(s);
						System.out.println();
						fileReader.close();               
						break;
					}
					case 3:{
    
    
						System.out.println("退出成功!");
						flag = false;
						file.delete();                    // 删除文件
						break;
					}
				}
			}
		}
		catch(Exception e) {
    
    
			e.printStackTrace();
		}
        sc.close();
	}

	//判断用户名是否正确方法
	public static boolean judge(String str) {
    
    
		if(str.length() != 6) {
    
                                     // 六位组成
			System.out.println("提示: 憨批, 用户名ID是六位大写字母和数字组成的哦! ");
			return false;
		}
		for(int i = 0;i < 2;i++) 
			if(!Character.isUpperCase(str.charAt(i))) {
    
         // 前两位不是大写字母
				System.out.println("提示: 憨批, 用户名ID的前两位应该是大写字母哦! ");
				return false;
			}
		for(int i = 2;i < 6;i++) 
			if(!Character.isDigit(str.charAt(i))) {
    
             // 后四位不是数字
				System.out.println("提示: 憨批, 用户名ID的后四位应该是数字哦! ");
				return false;
			}
		return true;		
	}
	
	// 随机运算操作
	public static void calculate() {
    
    
		int n = (int)(Math.random()*8)%4;    // 四种运算法,随机产生
		while(book == n)                     // 相邻运算不能相同
			n = (int)(Math.random()*8)%4;   
		book = n;
		switch(n) {
    
    
			case 0:add();break;
			case 1:subtract();break;
			case 2:multiplication();break;
			case 3:division();break;
		}
	}
	// 有参数运算操作, 重载方法
	public static void calculate(int n) {
    
    
		switch(n) {
    
    
			case 0:add();break;
			case 1:subtract();break;
			case 2:multiplication();break;
			case 3:division();break;
		}
	}
	
	// +
	public static void add() {
    
                
		int num1 = (int)(Math.random()*99 + 1);              // 产生 [1, 99] 的整数
		int num2 = (int)(Math.random()*(100-num1-1)+1);      // 保证两数运算结果小于100, 以下同理
		System.out.print(num1 + " + " + num2 + " = ");
		int result = sc.nextInt();
		
		achievement[index++] = String.format("%-2d + %-2d = |    %-2d    | %-2d", num1, num2, num1+num2, result);
		if(result == num1+num2)
			score += 10;                                     // 得分加 10 分
	}
	// -
	public static void subtract() {
    
    
		int num1 = (int)(Math.random()*99 + 1);
		int num2 = (int)(Math.random()*99 + 1);  
		while(num1 == num2)               // 保证两数不相等
			num2 = (int)(Math.random()*99 + 1);
		if(num1 < num2) {
    
       // 用最大值-最小值
			int temp = num1;
			num1 = num2;
			num2 = temp;
		}
		System.out.print(num1 + " - " + num2 + " = "); 
		int result = sc.nextInt();
		achievement[index++] = String.format("%-2d - %-2d = |    %-2d    | %-2d", num1, num2, num1-num2, result);
		if(Math.abs(Math.abs(num1-num2) - result) < 1e-6)    // 精确到10的-6次方算得分
			score += 10;
	}
	// *
	public static void multiplication() {
    
    
		int num1 = (int)(Math.random()*99 + 1);              // 产生 [1, 99] 的整数
		int num2 = (int)(Math.random()*(100/num1-1)+1);   
		System.out.print(num1 + " * " + num2 + " = ");
		int result = sc.nextInt();
		achievement[index++] = String.format("%-2d * %-2d = |    %-2d    | %-2d", num1, num2, num1*num2, result);
		if(result == num1*num2)
			score += 10;
	}
	// /
	public static void division() {
    
    
		double weighting = Math.random();
		int num1, num2;
		if(weighting < 50) 
			num1 = (int)(Math.random()*20 + 1);              // 产生 [1, 20] 的整数   加权 50%
		else if(weighting < 80) 
			num1 = (int)(Math.random()*30 + 21);             // 产生 [21, 50] 的整数   加权 30%
		else
			num1 = (int)(Math.random()*49 + 51);             // 产生 [51, 99] 的整数   加权 20%
		if(num1 > 50)                                        // 如果num1 > 50, num2 只能等于 num1 
			num2 = num1;
		else
			num2 = ((int)(Math.random()*(100/num1-1) + 1) )*num1; // num2 必须是 num1 的整数倍,保证结果为整数
		System.out.print(num2 + " / " + num1 + " = ");
		int result = sc.nextInt();
		achievement[index++] = String.format("%-2d / %-2d = |    %-2d    | %-2d", num2, num1, num2/num1, result);
		if(result == num2/num1)
			score += 10;
	}
}

Guess you like

Origin blog.csdn.net/qq_45772965/article/details/108891382