参考题目4——四则运算(三)

(1)题目要求
1.定义主界面菜单
参数设置
小学二年级口算题
小学三年级口算题
小学四年级口算题
错题集
2.其中参数设置包括出题个数(缺省值为30)、操作数数值范围。
3.小学二年级口算题操作数为两个、可进行加减乘除运算(除法必须可以整除),操作数范围不超过100.
4.小学三年级口算题操作数不超过4个,可以进行加减乘除,操作数范围不超过1000.(要求采用继承小学二年级出题类的方式,实现小学三年级的出题类)
5.小学四年级口算题操作数不超过5个,可以进行加减乘除,还可以加入括号运算。 (要求采用继承小学三年级出题类的方式,实现小学四年级的出题类)
6.学生实时答题结束后,可以选择是否进行下一套题目答题,如果选择是,则抽取下一套题进行答题,答题结束可以通过查看错题本,查询今日做题正确率。也可以针对错题进行二次答题。

代码:

package jichensizeyunsuan;
import java.util.*;

public class zhujiemian {
static Scanner sc = new Scanner(System.in);

//主菜单界面
public static int systemMenu()
{
System.out.println("***************"+"\n"+
"四则运算" +"\n"+
"1.小学二年级口算题" +"\n"+
"2.小学三年级口算题" +"\n"+
"3.小学四年级口算题" +"\n"+
"4.退出" +"\n"+
"***************");
System.out.println("请选择:");
int ch;
ch = sc.nextInt();
return ch;
}

//主方法
public static void mainFrame()
{
int choose;
choose = systemMenu();

if(choose>=1&&choose<=6)
{
switch(choose)
{
case 1:SecondExercise er= new SecondExercise();
er.czshuRewrite();
break;
case 2:ThirdExercise san = new ThirdExercise();
san.czshuRewrite();
break;
case 3:FouthExercise si = new FouthExercise();
si.czshuRewrite();
break;
case 4:systemExit();break;
}
}
else //输入主界面选项不存在,返回主界面
{
System.out.println("该选项不存在,请重新选择!");
choose = systemMenu();
}
}

//退出系统
public static void systemExit()
{
System.out.println("系统已退出,欢迎下次使用!");
System.exit(0);
}

// 主函数
public static void main(String[] args) {
mainFrame();
}
}

//二年级类
class SecondExercise
{
static Scanner sc = new Scanner(System.in);
public static int num=30; //初始化题数为30
public static int scope=100; //初始化范围为100
public static int operand=2; //初始化操作数为2

//操作数限定
public static void czshuRewrite()
{
czfwRewrite();
}

//操作范围限定
public static void czfwRewrite()
{
System.out.println("请输入数值范围:");
int fw=sc.nextInt();
scope=fw;
if(scope>100||scope<0)
{
System.out.println("操作数范围应在100内,请重新输入!");
czfwRewrite();
}
else
{
tshuRewrite();
}
}

//题数设置
public static void tshuRewrite()
{
System.out.println("请输入题目个数:");
int tishu=sc.nextInt();
num=tishu;
pdyuejie();
}

//判断越界
public static void pdyuejie()
{
if(num*operand>scope)
{
System.out.println("设置越界,请重新设置!");
czshuRewrite();
}
else
{
chuti();
}
}

//生成随机符号
public static char operator() {
char op=' ';
int n = (int)(Math.random()*999+1);
switch(n%4)
{
case 0:op = '+';break;
case 1:op = '-';break;
case 2:op = '*';break;
case 3:op = '/';break;
}
return op;
}

//出题所需参数
static int nnum=0;
static int ccount=0;
static int cuoticount=0; //记录错题数
static int []zqda = new int[100000]; //存正确答案
static int []cishu = new int[100000]; //存答错次数
static String []arr = new String[100000]; //存题目
//出题
public static void chuti()
{
int a = (int)(Math.random()*scope+1);
int b = (int)(Math.random()*scope+1);
char c = operator();
String str = ""+a+c+b;
int []cunshu = new int[1000];
char []cunfuhao =new char[1000];
int shu=0;
int fuhao=0;
if(c=='*')
{
cunshu[shu]=a*b;
}
else if(c=='/')
{
cunshu[fuhao]=a/b;
}
else
{
cunshu[shu]=a;
shu++;
cunshu[shu]=b;
cunfuhao[fuhao]=c;
}
for(int i=2;i<operand;i++)
{
int zenjiashu = (int)(Math.random()*scope+1);
char zenjiac = operator();
if(zenjiac=='*')
{
cunshu[shu]=cunshu[shu]*zenjiashu;
}
else if(c=='/')
{
cunshu[shu]=cunshu[shu]/zenjiashu;
}
else
{
shu++;
cunshu[shu]=zenjiashu;
fuhao++;
cunfuhao[fuhao]=zenjiac;
}
str = str + zenjiac + zenjiashu;
}

//存每题的正确答案
int zhengquedaan=cunshu[0];
int cou=0;
for(int j=0;j<=fuhao;j++)
{
if(cunfuhao[cou]=='+')
{
zhengquedaan+=cunshu[cou+1];
}
else
{
zhengquedaan-=cunshu[cou+1];
}
}
chutimu(str,zhengquedaan);
}

//显示题目判断答案
public static void chutimu(String str,int zhengquedaan)
{
if(ccount<num)
{
arr[ccount]=str;
zqda[ccount]=zhengquedaan;
System.out.println(arr[ccount]+"=");
int daan;
daan = sc.nextInt();
if(daan!=zhengquedaan)
{
System.out.println("答案错误");
cishu[ccount]=1;
cuoticount++;
}
else
{
System.out.println("答案正确");
cishu[ccount]=0;
}
ccount++;
chuti();
}
else
{
System.out.println("错误率"+cuoticount*100/num+"%");
jixun();
}
}

//做错题&新题
public static void jixun()
{
System.out.println("请选择: "+"\n"+
"1.错题重做 "+"\n"+
"2.再练一套 "+"\n"+
"3.退出系统");
int xuan;
xuan = sc.nextInt();
switch(xuan)
{
case 1:cuoti();break;
case 2:zailian();break;
case 3:System.out.println("系统已退出,欢迎下次使用!");
System.exit(0);
}
}

//做错题
public static void cuoti()
{
nnum=cuoticount;
cuoticount=0;
for(int i=0;i<num;i++)
{
if(cishu[i]==1)
{
System.out.println(arr[i]+"=");
int da;
da = sc.nextInt();
if(da!=zqda[i])
{
System.out.println("答案错误");
cuoticount++;
}
else
{
cishu[i]=0;
System.out.println("答案正确");
}
}
}
System.out.println("错误率"+cuoticount*100/nnum+"%");
jixun();
}

//再练一套
public static void zailian()
{
ccount=0;
cuoticount=0;
nnum=0;
chuti();
}
}

//三年级类
class ThirdExercise extends SecondExercise
{
//重写父类操作数函数
public static void czshuRewrite()
{
System.out.println("请输入操作数:");
int czs=sc.nextInt();
operand=czs;
if(operand>4||operand<0)
{
System.out.println("操作数应在不超过4个,请重新输入!");
czshuRewrite();
}
else
{
czfwRewrite();
}
}

//重写父类操作范围函数
public static void czfwRewrite()
{
System.out.println("请输入数值范围:");
int fww=sc.nextInt();
scope=fww;
if(scope>1000||scope<0)
{
System.out.println("操作数范围应在1000内,请重新输入!");
czfwRewrite();
}
else
{
tshuRewrite();
}
}
}

//四年级类
class FouthExercise extends ThirdExercise
{
////重写父类操作范围函数
public static void czfwRewrite()
{
System.out.println("请输入数值范围:");
int fwww=sc.nextInt();
scope=fwww;
if(scope>10000||scope<0)
{
System.out.println("操作数范围应在10000内,请重新输入!");
czfwRewrite();
}
else
{
tshuRewrite();
}
}
}

posted @   奉禾  阅读(20)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· 葡萄城 AI 搜索升级:DeepSeek 加持,客户体验更智能
· 什么是nginx的强缓存和协商缓存
· 一文读懂知识蒸馏
点击右上角即可分享
微信分享提示
主题色彩