Java语言实现小学数学练习

Java语言实现小学数学练习

题目:
【问题描述】
编写一个帮助小学生练习数学的程序,帮助小学生练习100以内的四种数学运算:加、减、乘、除。

【基本要求】
a)程序应先询问用户的ID号(ID号包括两个大写字母和4位数字),例如:
请输入用户ID号:AB1234
程序应对输入的ID号验证,符合ID号要求的格式,然后程序提示三种选择:
(1)开始测试
(2)检查分数
(3)退出
b)测试:该程序将给出10道数学题,例如:
12*3=36
48+32=80

56/28=2

注意:
i)学生将依次回答每一个问题(在等于号后面给出答案),然后给出下一道题。
ii)试题应包含四种数学运算:加、减、乘、除,它们是随机产生的。相邻的问题应该是不同的操作,
每个操作必须至少出现一次。
iii)为每道题随机生成数字,但必须确保参与运算的数字和结果都小于100且大于零的整数。
iv)十道题做完后,记录学生完成这十道题所用的时间。
v)给每个学生一个分数。将该学生的ID、成绩和使用时间保存到一个名为record.txt的文件中。
vi)在屏幕上输出以下信息:
问题|正确答案|你的答案
c)成绩检查:从文件“record.txt”中列出该学生的所有历史成绩。例如:
你以前的记录是:
AB1234 80 150秒
AB1234 50 182秒
AB1234 90 98秒

Java代码:

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;
	}
}

猜你喜欢

转载自blog.csdn.net/qq_45772965/article/details/108891382