编写一个帮助小学生练习数学的程序,帮助小学生练习 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)在屏幕上输出以下信息:(3 列信息,第 1 列是 b)中的测试题,蓝色部分)
问题 | 正确答案 | 你的答案
c) 成绩检查:从文件“record.txt”中列出该学生的所有历史成绩( 其他学生的不显示 )。
import java.io.*;
import java.util.*;
import java.util.regex.*;

public class MathTest {
	static List<Character> operate = new ArrayList<Character>(Arrays.asList(new Character[10]));
	static int[] a = new int[10];
	static int[] b = new int[10];
	static int[] ans = new int[10];
	static int[] correct = new int[10];
	static String PATH = "record.txt";
	
	public static void main(String[] args) {
		System.out.println("请输入用户ID号:");
		Scanner sc = new Scanner(System.in);
		String id = sc.next();
		Pattern p = Pattern.compile("^[A-Z]{2}[0-9]{4}$");//校验用户名是否合格
		Matcher m = p.matcher(id);
		while(!m.find()) {
			System.out.println("请输入正确的用户ID号:");
			id = sc.next();
			m = p.matcher(id);
		}
		int option = 3,score = 0;
		long time = 0;
		do {
			System.out.println("(1)开始测试\n(2)检查分数\n(3)退出");
			option = sc.nextInt();
			if(option == 1) {
				testpaperMaking(); // 准备试题
				time = test(); //开始答题,并记录答题时间
				printResult(); //答题结果格式化打印
				score = getScore(); //计算分数
				saveToFile(id,score,time); //保存在文件中
			}
			if(option == 2)
				pastRecords(id);//查看历史记录
		} while(option != 3);
	}
	//根据id查找历史记录
	public static void pastRecords(String id) {
		try {
			FileInputStream fileInputStream = new FileInputStream(PATH);
			BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(fileInputStream));
	        String line = null;
	        System.out.println("你以前的记录是:");
	        while ((line = bufferedReader.readLine()) != null) {
	            if(line.contains(id)) {
	            	System.out.println(line);
	            }
	        }
	        fileInputStream.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static void saveToFile(String id,int score,long time) {
		try {
			//第二个参数true表示以追加形式写文件
			FileWriter writer = new FileWriter("record.txt", true);
			String content = id + " " + score + " " + time + "秒\n";
			writer.write(content);  //将结果写入文件中
			writer.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	//打印答题结果
	public static void printResult() {
		Formatter f = new Formatter(System.out);
		//问题 | 正确答案 | 你的答案
		f.format("%-10s|%-4s|%-4s\n", "问题","正确答案","你的答案");
		for (int i = 0; i < operate.size(); i++) { //格式化输出结果
			f.format("%-12s| %4d | %4d\n", 
				a[i] + " " + operate.get(i).toString() + " " + b[i], correct[i], ans[i]);
		}
		
	}
	//计算分数
	public static int getScore() {
		int sum = 0;
		for (int i = 0; i < correct.length; i++) {
			if(correct[i] == ans[i])
				sum += 10;
		}
		return sum;
	}
	//答题
	public static long test() {
		long startTime = new Date().getTime();
		Scanner sc = new Scanner(System.in);
		for (int i = 0; i < a.length; i++) {
			System.out.print(a[i] +" "+ operate.get(i).toString() +" "+ b[i] + " = ");
			ans[i] = sc.nextInt();
			System.out.println();
		}
		long endTime = new Date().getTime();
		return (endTime - startTime) / 1000;
	}
	//答卷生成
	public static void testpaperMaking() {
		operation();
		for (int i = 0; i < operate.size(); i++) {
			if(operate.get(i).equals('+')) {
				a[i] = (int)(Math.random()*98)+1; //产生一个1到98的随机数
				b[i] = (int)(Math.random()*(99-a[i]))+1 ;// 产生一个1到99-a[i]的随机数
				correct[i] = a[i] + b[i];
			}
			if(operate.get(i).equals('-')) {
				a[i] = (int)(Math.random()*98)+2; //产生一个2到99的随机数
				b[i] = (int)(Math.random()*(a[i]-1))+1 ;// 产生一个1到a[i]-1的一个随机数
				correct[i] = a[i] - b[i];
			}
			if(operate.get(i).equals('*')) {
				int A = (int)(Math.random()*99) + 1; //产生一个1到99的随机数
				int B = (int)(Math.random()*99) + 1 ;// 产生一个1到99的一个随机数
				int dividend = A > B ? A : B;
				a[i]= A < B ? A : B;
				b[i] = dividend / a[i];
				correct[i] = a[i] * b[i];
			}
			if(operate.get(i).equals('/')) {
				int A = (int)(Math.random()*99) + 1; //产生一个1到99的随机数
				int B = (int)(Math.random()*99) + 1 ;// 产生一个1到99的一个随机数
				int dividend = A > B ? A : B; //大的做被除数
				b[i] = A < B ? A : B; // 小的做除数
				correct[i] = dividend / b[i];
				a[i] = b[i] * correct[i];
			}
		}
	}
	//生成运算符
	public static void operation() {
		for (int i = 0; i < 10; i++) {
			int flag = ((int)(Math.random()*100+1))%4;
			switch(flag) {
				case 0: operate.set(i, '+'); break;
				case 1: operate.set(i, '-'); break;
				case 2: operate.set(i, '*'); break;
				case 3: operate.set(i, '/'); break;
			}
			if(i > 0 && (operate.get(i) == operate.get(i-1))) {
				i--;
			}
		}
		Set<Character> set = new HashSet<Character>(operate);//将产生的运算符序列放入Set中
		if(set.size() < 4) {//小于4说明有运算符没有生成 重新生成一次
			operation();
		}
	}
	
}

  • 7
    点赞
  • 50
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 好的,以下是一个简单计算器的程序,可以实现、乘、除四种运算功能: ``` while True: # 读取用户输入的数字和运算符 num1 = float(input("请输入第一个数字:")) op = input("请输入运算符(+、-、*、/):") num2 = float(input("请输入第二个数字:")) # 根据运算符进行相应的计算 if op == "+": result = num1 + num2 elif op == "-": result = num1 - num2 elif op == "*": result = num1 * num2 elif op == "/": result = num1 / num2 else: print("无效的运算符") continue # 输出计算结果 print("计算结果为:", result) ``` 程序会不断循环,每次读取用户输入的数字和运算符,然后根据运算符进行相应的计算,并输出计算结果。如果用户输入了无效的运算符,程序会提示并继续等待用户输入。 ### 回答2: 这里我将介绍如何使用Python编写一个简单计算器,支持、乘、除四种运算功能。 首先,我们需要让用户输入运算符和两个操作数。以下是代码示例: ```python operation = input("请输入运算符 (+, -, *, /) : ") num1 = float(input("请输入第一个操作数: ")) num2 = float(input("请输入第二个操作数: ")) ``` 接下来,我们需要根据用户输入的运算符执行相应的操作。这可以通过使用if-elif-else语句实现。以下是示例代码: ```python if operation == "+": result = num1 + num2 elif operation == "-": result = num1 - num2 elif operation == "*": result = num1 * num2 elif operation == "/": result = num1 / num2 else: print("抱歉,不支持该运算符") exit() ``` 最后,我们将打印结果并结束程序。以下是完整代码: ```python operation = input("请输入运算符 (+, -, *, /) : ") num1 = float(input("请输入第一个操作数: ")) num2 = float(input("请输入第二个操作数: ")) if operation == "+": result = num1 + num2 elif operation == "-": result = num1 - num2 elif operation == "*": result = num1 * num2 elif operation == "/": result = num1 / num2 else: print("抱歉,不支持该运算符") exit() print("结果为:", result) ``` 这就是一个简单的计算器的实现。当然,在实际的程序中,我们需要注意输入数据的合性,例如除数不能为0等等。 ### 回答3: 一个简单的计算器程序可以使用Python编写。首先,我们需要从用户那里获取要运算的两个数字,以及他们希望执行哪种操作(,乘,除之一)。我们可以使用Python的input()函数来获取用户的输入,如下所示: num1 = float(input("输入第一个数字: ")) num2 = float(input("输入第二个数字: ")) operation = input("选择运算 (+, -, *, /): ") 接下来,我们需要根据用户选择的操作来执行适当的计算并输出结果。我们可以使用if/elif语句来决定执行哪种操作,如下所示: if operation == '+': result = num1 + num2 elif operation == '-': result = num1 - num2 elif operation == '*': result = num1 * num2 elif operation == '/': result = num1 / num2 else: print("无效的运算符") 最后,我们将结果输出给用户,如下所示: print("结果: ", result) 这就是一个简单的计算器程序所需要的全部代码。完整代码如下所示: num1 = float(input("输入第一个数字: ")) num2 = float(input("输入第二个数字: ")) operation = input("选择运算 (+, -, *, /): ") if operation == '+': result = num1 + num2 elif operation == '-': result = num1 - num2 elif operation == '*': result = num1 * num2 elif operation == '/': result = num1 / num2 else: print("无效的运算符") print("结果: ", result)

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值