1.1【问题描述】
设计题目
编写一个帮助小学生练习数学的程序,帮助小学生练习 100 以内的四种数学运算:加、减、乘、除。
1.2【基本要求】
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 AB1234 50 AB1234 90
150 秒 182 秒 98 秒
package 实习;
import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
//author:Guo Jinglong
//school:taiyuanligong
//time:7/12
public class demo01 {
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;
public static void main(String[] args) {
System.out.println("请输入用户ID号:");
String id=sc.nextLine();
while (!check(id)){
System.out.println("请重新输入用户ID号:");
id=sc.nextLine();
}
System.out.println("用户名正确!");
try {
File file =new File("record.txt");
if(!file.isFile()){
file.createNewFile();
}
FileWriter fileWriter=new FileWriter(file,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("(1)开始测试\t\n(2)检查分数\n(3)退出\n");
int chice=sc.nextInt();
switch (chice){
case 1:{
score=0;
book=list.get(3);
long start=System.currentTimeMillis();
Collections.shuffle(list);
for (int j = 0; j < 10; j++) {
if(j<4){
calulate(list.get(j));
}
else
{
calulate();
}
}
long end =System.currentTimeMillis();
time=(end-start)/1000;
fileWriter=new FileWriter(file,true);
fileWriter.write(id+" "+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 (IOException e) {
e.printStackTrace();
}
}
public static boolean check(String id){
if(id.length()!=6){
System.out.println("提示:憨批,用户名是由六位大写字母和数字组成的哦!");
return false;
}
for (int i = 0; i < 2; i++) {
if(!Character.isUpperCase(id.charAt(i))){
System.out.println("憨批,用户名的前二位是由大写字母组成的哦!");
return false;
}
}
for (int i = 2; i < 6; i++) {
if(!Character.isDigit(id.charAt(i))){
System.out.println("憨批,后四位是由数字组成的哦!");
return false;
}
}
return true;
}
public static void calulate(){
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:multi();break;
case 3:division();break;
}
}
public static void calulate(int n){
switch (n){
case 0:add();break;
case 1:subtract();break;
case 2:multi();break;
case 3:division();break;
}
}
public static void add(){
int n1=(int)(Math.random()*99+1);
int n2=(int)(Math.random()*(100-n1-1)+1);
System.out.println(n1+" + "+n2+" = ");
int result=sc.nextInt();
achievement[index++]=String.format("%-2d + %-2d = | %-2d |%-2d",n1,n2,n1+n2,result);
if (result==n1+n2){
score+=10;
}
}
public static void subtract(){
int n1=(int)(Math.random()*99+1);
int n2=(int)(Math.random()*99+1);
while (n1==n2){
n2=(int)(Math.random()*99+1);
}
if(n1<n2){
int t;
t=n1;
n1=n2;
n2=t;
}
System.out.println(n1+" - "+n2+" = ");
int result=sc.nextInt();
achievement[index++]=String.format("%-2d - %-2d = | %-2d |%-2d",n1,n2,n1-n2,result);
if(Math.abs(n1-n2)-result<1e-6){
score+=10;
}
}
public static void multi(){
int n1=(int)(Math.random()*99+1);
int n2=(int)(Math.random()*(100/n1-1)+1);
System.out.println(n1+" * "+n2+" = ");
int result=sc.nextInt();
achievement[index++]=String.format("%-2d * %-2d = | %-2d |%-2d",n1,n2,n1*n2,result);
if (result==n1*n2){
score+=10;
}
}
public static void division(){
double weighting=Math.random();
int n1,n2;
if(weighting<50){
n1=(int)(Math.random()*20+1);
}else if(weighting>80){
n1=(int)(Math.random()*30+21);
}
else {
n1=(int)(Math.random()*49+51);
}
if(n1>50){
n2=n1;
}
else{
n2=((int)(Math.random()*(100/n1-1)+1))*n1;
}
System.out.println(n1+" / "+n2+" = ");
int result=sc.nextInt();
achievement[index++]=String.format("%-2d / %-2d = | %-2d |%-2d",n2,n1,n2/n1,result);
if (result==n2/n1){
score+=10;
}
}
}