时隔一学期,重新尝试写博客,各位客官走过路过不要错过
目录
问题描述
编写一个帮助小学生练习数学的程序,帮助小学生练习 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”中列出该学生的所有历史成绩(其他学生的不显示)。
例如: 你以前的记录是:
AB1234 80 150 秒
AB1234 50 182 秒
AB1234 90 98 秒
问题分析
10道选择题 循环
输入ID 检查ID正确性
正确:
1.开始测试 2.检查分数 3.退出
开始测试:
开始计时
随机加减乘除(参与运算的数字和结果都小于 100 且大于零的整数)
加:
随机出题判断结果大小 结果若大于100则此题重出
减:
随机出题判断结果大小 结果小于0则重出
乘:
随机出题判断结果大小 结果若大于100则此题重出
除:
随机出题判断结果大小 判断两数是否能整除 如果不能重新选数
十题结束 计时结束 给出分数与时间
将该学生的 ID、成绩和使用时间保存到一个名为 record.txt 的文件中。
屏幕输出相应信息
成绩检查:
列出历史该学生成绩
退出:
退出程序
测试实例及运行结果
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Random;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Math {
static int[] str1 = new int[10];//全局数组,储存正确答案和自己的结果
static int[] str2 = new int[10];
static String[] str3 = new String[10];
public static void main(String[] args) {
System.out.println("请输入用户 ID 号");
String ID = checkID();//检查ID
option(ID , str1 , str2);//开始执行程序
}
public static String checkID(){//正则表达式检查输入ID是否正确
Scanner sc = new Scanner(System.in);
String ID = sc.next();
String regex = "^[A-Z]{2}[0-9]{4}$";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(ID);
while(m.matches() == false) {//循环输入直到正确
System.out.println("格式错误,请再次输入");
ID = sc.next();
m = p.matcher(ID);
}
return ID;
}
public static void option(String ID , int[] str1 , int[] str2){//三个选项
System.out.println("(1)开始测试 (2)检查分数 (3)退出");
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
while(a == 1){
long startTime = System.currentTimeMillis();//开始计时
randomMethods();//随机生成十次运算的顺序
int[] arr = randomMethods();//存入数组
int score = 0;//计分
for(int i = 0 ; i < 10 ; i++) {
if(arr[i] == 1) {
score = add(score , i);
}else if(arr[i] == 2) {
score = sub(score , i);
}else if(arr[i] == 3) {
score = mul(score , i);
}else if(arr[i] == 4) {
score = div(score , i);
}
}
long endTime = System.currentTimeMillis();//结束计时
long usedTime = (endTime-startTime)/1000;
System.out.println("总分:" + score + " 用时:" + usedTime + " 秒");;
System.out.println("问题 | 正确答案 | 你的答案");
for(int i = 0 ; i < 10 ; i++) {
System.out.println(str3[i] + " " + str2[i] + " " + str1[i]);
}
writeFile(ID , score , usedTime);//将本次做题数据存入
System.out.println("本次做题数据已成功储存!");
option(ID , str1 , str2);//提供多次操作
}
while(a == 2){
readTxt(ID);//读取文档内容
option(ID , str1 , str2);//提供多次操作
}
while(a == 3){
System.out.println("程序已退出,感谢使用!");
System.exit(0);//退出程序
}
}
public static int[] randomMethods() {//随机生成十次运算 保证加减乘除都涉及到
Random rand = new Random();
int a;
int count1 = 0 ,count2 = 0 ,count3 = 0 ,count4 = 0;//加减乘除出现次数
int[] arr = new int[10];
for(int i = 0 ; i < 10 ; i++) {
a = rand.nextInt(4) + 1;
arr[i] = a;//每次运算符号代表的数字存入数组
if(a == 1) {
count1++;
}else if(a == 2) {
count2++;
}else if(a == 3) {
count3++;
}else if(a == 4) {
count4++;
}
}
while(count1 == 0 || count2 == 0 || count3 == 0 ||count4 == 0) {//四种运算都保证到
count1 = 0;
count2 = 0;
count3 = 0;
count4 = 0;
for(int i = 0 ; i < 10 ; i++) {
a = rand.nextInt(4) + 1;
arr[i] = a;
if(a == 1) {
count1++;
}else if(a == 2) {
count2++;
}else if(a == 3) {
count3++;
}else if(a == 4) {
count4++;
}
}
}
}
public static int add(int score , int i) {//加法运算
Scanner sc = new Scanner(System.in);
Random rand = new Random();
int num1 = rand.nextInt(100) + 1;//第一个数
int num2 = rand.nextInt(100) + 1;//第二个数
while(num1 + num2 > 100) {
num1 = rand.nextInt(100) + 1;
num2 = rand.nextInt(100) + 1;
}
System.out.print(num1 + " + " + num2 + " = ");
str3[i] = num1 + " + " + num2 + " = ";
int num3 = sc.nextInt();
str1[i] = num3;//储存输入数据
str2[i] = num1+num2;//存储正确答案
if(num3 == num1+num2) {
System.out.println("正确!");
score+=10;//正确加分
return score;
}else {
System.out.println("错误!");
return score;//错误直接返回
}
}
public static int sub(int score , int i) {//减法运算
Scanner sc = new Scanner(System.in);
Random rand = new Random();
int num1 = rand.nextInt(100) + 1;//第一个数
int num2 = rand.nextInt(100) + 1;//第二个数
while(num1 - num2 < 0) {
num1 = rand.nextInt(100) + 1;
num2 = rand.nextInt(100) + 1;
}
System.out.print(num1 + " - " + num2 + " = ");
str3[i] = num1 + " - " + num2 + " = ";
int num3 = sc.nextInt();
str1[i] = num3;//储存输入数据
str2[i] = num1-num2;//存储正确答案
if(num3 == num1-num2) {
System.out.println("正确!");
score+=10;//正确加分
return score;
}else {
System.out.println("错误!");
return score;//错误直接返回
}
}
public static int mul(int score , int i) {
Scanner sc = new Scanner(System.in);
Random rand = new Random();
int num1 = rand.nextInt(100) + 1;
int num2 = rand.nextInt(100) + 1;
while(num1 * num2 >100) {
num1 = rand.nextInt(100) + 1;
num2 = rand.nextInt(100) + 1;
}
System.out.print(num1 + " * " + num2 + " = ");
str3[i] = num1 + " * " + num2 + " = ";
int num3 = sc.nextInt();
str1[i] = num3;//储存输入数据
str2[i] = num1*num2;//存储正确答案
if(num3 == num1*num2) {
System.out.println("正确!");
score+=10;//正确加分
return score;
}else {
System.out.println("错误!");
return score;//错误直接返回
}
}
public static int div(int score , int i) {
Scanner sc = new Scanner(System.in);
Random rand = new Random();
int num1 = rand.nextInt(100) + 1;
int num2 = rand.nextInt(100) + 1;
while(num1 % num2 != 0) {
num1 = rand.nextInt(100) + 1;
num2 = rand.nextInt(100) + 1;
}
System.out.print(num1 + " / " + num2 + " = ");
str3[i] = num1 + " / " + num2 + " = ";
int num3 = sc.nextInt();
str1[i] = num3;//储存输入数据
str2[i] = num1/num2;//存储正确答案
if(num3 == num1/num2) {
System.out.println("正确!");
score+=10;//正确加分
return score;
}else {
System.out.println("错误!");
return score;//错误直接返回
}
}
public static void writeFile(String ID , int score , long usedTime) {
try {
File file = new File("src/record.txt");
if(!file.exists()) {
file.createNewFile(); // 创建新文件,有同名的文件的话直接覆盖
}
FileOutputStream fos = new FileOutputStream(file,true);//输出流
OutputStreamWriter osw = new OutputStreamWriter(fos);//转换流
BufferedWriter bw = new BufferedWriter(osw);//缓冲输入流
bw.write(ID + " " + score + " " + usedTime + " 秒");
bw.newLine();
bw.flush();
bw.close();
osw.close();
fos.close();
}catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e2) {
e2.printStackTrace();
}
}
public static String readTxt(String ID) {
File file = new File("src/record.txt");
if(file.isFile() && file.exists()){
try {
FileInputStream fileInputStream = new FileInputStream(file);
InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String text;
while((text = bufferedReader.readLine()) != null){
if(text.contains(ID))
System.out.println(text);
}
} catch (Exception e) {
e.printStackTrace();
}
}
return null;
}
}
以上就是这道程序设计题的解析与源代码,喜欢的话点个赞吧,您的点赞是我坚持的动力~