Harmony OS Next应用“丁斗口算”开发记录 (10) 原创

丁斗科技
发布于 2025-1-15 17:09
浏览
0收藏

第十节 自动生成题目及答案

本应用题目类型包括加减法、乘除法、方程等,题目生成基于随机数,但也有其他问题需要解决。以加减法为例,主要思路如下:

  1. 限定加减法的数值范围,包括10以内、20以内、50以内、100以内:这通过限定生成的随机数的范围实现。
  2. 限制加法题目两个加数不能相同:通过判断两个数是否相同,相同则重新生成一个数直到不同为止。
  3. 随机的使题目为加法或减法:通过比较两个数的大小实现,如果第一个数大于第二个数,则为减法。如果第一个数小于第二个数,则为加法。
  4. 当题目为减法时,不能让结果小于0:第3条可以保证减法的结果不会小于0.
    基于以上思路,代码如下:
    export default class Data_Add{
    public N1 :number = 1;
    public N2 :number = 1;
    public Symbol:string = ‘+’;
    public Rlt :number = 2;

public input : string = ‘’;
public color : Color = Color.Gray;

constructor(max:number) {
this.N1 = Math.floor(Math.random()*max/2+1);
this.N2 = Math.floor(Math.random()*max/2+1);
while(this.N1 == this.N2){
// 保证N1 != N2
this.N2 = Math.floor(Math.random()*max+1);
}
if( this.N1 > this.N2 ){
this.Symbol = ‘-’;
this.Rlt = this.N1 - this.N2;
}else{
this.Rlt = this.N1 + this.N2;
}
}
getDataString(disp:boolean) : string {
let rlt : string = this.N1.toString() + this.Symbol + this.N2.toString() + ‘= ?’;
return rlt;
}
}

以除法为例,主要思路如下:

  1. 非小数除法,需保证可以被整除:采用计算乘法方式,即生成两个随机数,将两个数的乘积作为被除数,其中一个随机数作为除数。(注:为提高生成速度,奖第计算量,这里不采用随机生成被除数和除数再判断商是否为整数的方式)
  2. 保证被除数的范围和除数的范围:限定两个随机数的范围即可。
  3. 保证除数和商不同:判断两个随机数是否相同,相同则重新生成直至不同为止。
    代码如下:
    export default class Data_Div{
    public N1 :number = 50;
    public N2 :number = 50;
    public Symbol:string = ‘➗’;
    public Rlt :number = 1;
    public input : string = ‘’;

constructor() {
while(this.Rlt == this.N2 || this.Rlt<11 || this.N2<11 ){
// 保证N1 N2 符合范围
this.Rlt = Math.floor(Math.random()*99+1);
this.N2 = Math.floor(Math.random()*99+1);
}
this.N1 = this.Rlt * this.N2;
}
getDataString(disp:boolean) : string {
let rlt : string = this.N1.toString() + this.Symbol + this.N2.toString() + ‘= ?’;
if( disp==true ) {
//rlt += this.Rlt;
}
return rlt;
}
}
用户从首页(index)点击菜单进入题目页面(PageEquation)后,传递一个选择的参数过来(详见上文),题目页面根据传来的参数,调用上文的类,自动生成题目。这里要考虑到平板的面积比手机大,因此平板上生成的题目要比手机上多一些,题目的排列方式(Grid组件)也不同。
代码如下:
createEqu(){
console.log(‘createEqu’)
switch(this.index){
case 0:
// 10以内加减法,生成数据
this.arrDataAdd = [];
if(this.deviceType==‘tablet’) {
this.equCount = 20;
this.gridrowtemp_v = ‘1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr’;
this.gridrowtemp_h = ‘1fr 1fr 1fr 1fr’;
this.gridcoltemp_v = ‘1fr 1fr’;
this.gridcoltemp_h = ‘1fr 1fr 1fr 1fr 1fr’;
}else{
this.equCount = 16;
this.gridrowtemp_v = ‘1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr’;
this.gridrowtemp_h = ‘1fr 1fr 1fr 1fr’;
this.gridcoltemp_v = ‘1fr 1fr’;
this.gridcoltemp_h = ‘1fr 1fr 1fr 1fr’;
}
for( let i=0;i<this.equCount;i++ ){
let bean = new Data_Add(10);
this.arrDataAdd.push(bean);
}
break;
case 1:
// 20以内加减法,生成数据
this.arrDataAdd = [];
if( this.deviceType==‘phone’ ){
this.equCount = 16;
this.gridrowtemp_v = ‘1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr’;
this.gridrowtemp_h = ‘1fr 1fr 1fr 1fr’;
this.gridcoltemp_v = ‘1fr 1fr’;
this.gridcoltemp_h = ‘1fr 1fr 1fr 1fr’;
}else{
this.equCount = 20;
this.gridrowtemp_v = ‘1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr’;
this.gridrowtemp_h = ‘1fr 1fr 1fr 1fr’;
this.gridcoltemp_v = ‘1fr 1fr’;
this.gridcoltemp_h = ‘1fr 1fr 1fr 1fr 1fr’;
}
for( let i=0;i<this.equCount;i++ ){
let bean = new Data_Add(20);
this.arrDataAdd.push(bean);
}
break;
case 2:
// 50以内加减法,生成数据
this.arrDataAdd = [];
if( this.deviceType==‘phone’ ){
this.equCount = 16;
this.gridrowtemp_v = ‘1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr’;
this.gridrowtemp_h = ‘1fr 1fr 1fr 1fr’;
this.gridcoltemp_v = ‘1fr 1fr’;
this.gridcoltemp_h = ‘1fr 1fr 1fr 1fr’;
}else{
this.equCount = 20;
this.gridrowtemp_v = ‘1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr’;
this.gridrowtemp_h = ‘1fr 1fr 1fr 1fr’;
this.gridcoltemp_v = ‘1fr 1fr’;
this.gridcoltemp_h = ‘1fr 1fr 1fr 1fr 1fr’;
}
for( let i=0;i<this.equCount;i++ ){
let bean = new Data_Add(50);
this.arrDataAdd.push(bean);
}
break;
case 3:
// 九九乘法,生成数据
this.arrDataMul = [];;
if( this.deviceType==‘phone’ ){
this.equCount = 16;
this.gridrowtemp_v = ‘1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr’;
this.gridrowtemp_h = ‘1fr 1fr 1fr 1fr’;
this.gridcoltemp_v = ‘1fr 1fr’;
this.gridcoltemp_h = ‘1fr 1fr 1fr 1fr’;
}else{
this.equCount = 20;
this.gridrowtemp_v = ‘1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr’;
this.gridrowtemp_h = ‘1fr 1fr 1fr 1fr’;
this.gridcoltemp_v = ‘1fr 1fr’;
this.gridcoltemp_h = ‘1fr 1fr 1fr 1fr 1fr’;
}
for( let i=0;i<this.equCount;i++ ){
let bean = new Data_Mul(9,1);
this.arrDataMul.push(bean);;
}
break;
case 4:
// 100以内加减法,生成数据
this.arrDataAdd = [];
if( this.deviceType==‘phone’ ){
this.equCount = 16;
this.gridrowtemp_v = ‘1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr’;
this.gridrowtemp_h = ‘1fr 1fr 1fr 1fr’;
this.gridcoltemp_v = ‘1fr 1fr’;
this.gridcoltemp_h = ‘1fr 1fr 1fr 1fr’;
}else{
this.equCount = 20;
this.gridrowtemp_v = ‘1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr’;
this.gridrowtemp_h = ‘1fr 1fr 1fr 1fr’;
this.gridcoltemp_v = ‘1fr 1fr’;
this.gridcoltemp_h = ‘1fr 1fr 1fr 1fr 1fr’;
}
for( let i=0;i<this.equCount;i++ ){
let bean = new Data_Add(100);
this.arrDataAdd.push(bean);
}
break;
case 5:
// 两位数乘法,生成数据
this.arrDataMul = [];
if( this.deviceType==‘phone’ ){
this.equCount = 16;
this.gridrowtemp_v = ‘1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr’;
this.gridrowtemp_h = ‘1fr 1fr 1fr 1fr’;
this.gridcoltemp_v = ‘1fr 1fr’;
this.gridcoltemp_h = ‘1fr 1fr 1fr 1fr’;
}else{
this.equCount = 20;
this.gridrowtemp_v = ‘1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr’;
this.gridrowtemp_h = ‘1fr 1fr 1fr 1fr’;
this.gridcoltemp_v = ‘1fr 1fr’;
this.gridcoltemp_h = ‘1fr 1fr 1fr 1fr 1fr’;
}
for( let i=0;i<this.equCount;i++ ){
let bean = new Data_Mul(29,11);
this.arrDataMul.push(bean);
this.textInput.push(‘’);
this.inputSelect.push(false);
this.rltCorrect.push(false);
this.bDispRlt.push(false);
}
break;
case 6:
// 四位数除法,生成数据
this.arrDataDiv = [];
if( this.deviceType==‘phone’ ){
this.equCount = 16;
this.gridrowtemp_v = ‘1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr’;
this.gridrowtemp_h = ‘1fr 1fr 1fr 1fr’;
this.gridcoltemp_v = ‘1fr 1fr’;
this.gridcoltemp_h = ‘1fr 1fr 1fr 1fr’;
}else{
this.equCount = 20;
this.gridrowtemp_v = ‘1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr’;
this.gridrowtemp_h = ‘1fr 1fr 1fr 1fr’;
this.gridcoltemp_v = ‘1fr 1fr’;
this.gridcoltemp_h = ‘1fr 1fr 1fr 1fr 1fr’;
}
for( let i=0;i<this.equCount;i++ ){
let bean = new Data_Div();
this.arrDataDiv.push(bean);
this.textInput.push(‘’);
this.inputSelect.push(false);
this.rltCorrect.push(false);
this.bDispRlt.push(false);
}
break;
case 7:
// 一元一次方程,生成数据
this.arrDataOneEqu = [];
if( this.deviceType==‘phone’ ){
this.equCount = 12;
this.gridrowtemp_v = ‘1fr 1fr 1fr 1fr 1fr 1fr’;
this.gridrowtemp_h = ‘1fr 1fr 1fr’;
this.gridcoltemp_v = ‘1fr 1fr’;
this.gridcoltemp_h = ‘1fr 1fr 1fr 1fr’;
}else{
this.equCount = 16;
this.gridrowtemp_v = ‘1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr’;
this.gridrowtemp_h = ‘1fr 1fr 1fr 1fr’;
this.gridcoltemp_v = ‘1fr 1fr’;
this.gridcoltemp_h = ‘1fr 1fr 1fr 1fr’;
}
for( let i=0;i<this.equCount;i++ ){
let bean = new Data_OneEqu(39,5);
this.arrDataOneEqu.push(bean);
this.textInput.push(‘’);
this.inputSelect.push(false);
this.rltCorrect.push(false);
this.bDispRlt.push(false);
}
break;
case 8:
// 四则混合运算
this.arrData4Cal = [];
if( this.deviceType==‘phone’ ){
this.equCount = 12;
this.gridrowtemp_v = ‘1fr 1fr 1fr 1fr 1fr 1fr’;
this.gridrowtemp_h = ‘1fr 1fr 1fr’;
this.gridcoltemp_v = ‘1fr 1fr’;
this.gridcoltemp_h = ‘1fr 1fr 1fr 1fr’;
}else{
this.equCount = 16;
this.gridrowtemp_v = ‘1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr’;
this.gridrowtemp_h = ‘1fr 1fr 1fr 1fr’;
this.gridcoltemp_v = ‘1fr 1fr’;
this.gridcoltemp_h = ‘1fr 1fr 1fr 1fr’;
}
for( let i=0;i<this.equCount;i++ ){
let bean = new Data_4Cal();
this.arrData4Cal.push(bean);
this.textInput.push(‘’);
this.inputSelect.push(false);
this.rltCorrect.push(false);
this.bDispRlt.push(false);
}
break;
case 9:
// 二元一次方程
this.arrDataTwoEqu = [];
if( this.deviceType==‘phone’ ){
this.equCount = 6;
this.gridrowtemp_v = ‘1fr 1fr 1fr’;
this.gridrowtemp_h = ‘1fr 1fr’;
this.gridcoltemp_v = ‘1fr 1fr’;
this.gridcoltemp_h = ‘1fr 1fr 1fr’;
}else{
this.equCount = 12;
this.gridrowtemp_v = ‘1fr 1fr 1fr 1fr 1fr 1fr’;
this.gridrowtemp_h = ‘1fr 1fr 1fr’;
this.gridcoltemp_v = ‘1fr 1fr’;
this.gridcoltemp_h = ‘1fr 1fr 1fr 1fr’;
}
for( let i=0;i<this.equCount;i++ ){
let bean = new Data_TwoEqu();
this.arrDataTwoEqu.push(bean);
this.textInput.push(‘’);
this.textInput.push(‘’);
this.inputSelect.push(false);
this.inputSelect.push(false);
this.rltCorrect.push(false);
this.bDispRlt.push(false);
}
break;
}
}
至此,完成了题目的生成。

©著作权归作者所有,如需转载,请注明出处,否则将追究法律责任
收藏
回复
举报
回复
    相关推荐