第16篇:模拟考试系统——计时、评分与结果统计

img

一、引言

模拟考试是驾考应用的核心功能之一,它模拟真实考试环境,提供计时答题、自动评分、成绩统计等功能。DriverLicenseExam 项目的模拟考试系统设计精巧,本文将深入解析其实现。

二、模拟考试模式

2.1 模式触发

在 HomeView 中点击"模拟考试"按钮触发:

// HomeView.ets
Row() {
  Image($r('app.media.trophy')).width(24).height(24);
  Text('模拟考试').fontSize(14).fontColor('#FFFFFF');
  Text('仿真冲刺').fontSize(12).fontColor('#FFFFFF');
}
.onClick(() => {
  const param: ROUTE_PARAM = {
    title: '模拟考试',
    type: EXAM_MANAGER_TYPE.mock_exam,
    examManager: this.examService.getMockExamManager('模拟考试')
  };
  this.vm.navStack.pushPathByName('practiceView', param);
});

2.2 试卷生成

每次模拟考试都生成新的试卷:

// ExamService.getMockExamManager
getMockExamManager(name: string | Resource) {
  let examManager = new ExamManager(name, this.generateExamDetail());
  examManager.timeLimit = 1;  // 限时1分钟
  return examManager;
}

三、倒计时实现

3.1 倒计时逻辑

// Exam.ets
aboutToAppear(): void {
  this.currentIndex = this.examManager.currentQuestionId + 1;

  if (this.examManager.timeLimit !== 0) {
    this.remainTime = this.examManager.timeLimit * 60;
    let showDialog: boolean = true;
    
    setInterval(() => {
      if (showDialog && !this.isStop) {
        if (this.remainTime >= 1) {
          this.remainTime--;
        } else {
          // 时间到,自动交卷
          showDialog = false;
          this.isStop = true;
          this.examSubmitDialog.open();
        }
      }
    }, 1000);
  }
}

3.2 暂停/继续

// 暂停考试
this.isStop = true;

// 继续考试
this.isStop = false;

四、评分与成绩统计

4.1 分数计算

// 提交试卷时计算分数
submitExam() {
  const totalQuestions = this.examManager.examDetails.length;
  const correctCount = this.examManager.correctNumber;
  const score = Math.round(correctCount / totalQuestions * 100);
  
  // 记录模拟考试分数
  this.mockExamCall(score);
}

4.2 成绩统计

// ExamService 统计
@Trace mockExamCount: number = 0;     // 考试次数
@Trace mockExamScore: number[] = [];   // 每次分数

public addMockExamCount(score: number) {
  this.mockExamCount++;
  this.mockExamScore.push(score);
}

// 计算平均分
public calculateAverageScore(): number {
  if (this.mockExamCount === 0) return 0;
  return Math.ceil(this.mockExamScore.reduce((pre, next) => pre + next) / this.mockExamCount);
}

五、返回拦截与确认弹窗

5.1 返回拦截

// PracticeView.ets
.onBackPressed(() => {
  if (this.type === EXAM_MANAGER_TYPE.mock_exam) {
    this.examController.isShowMockExamDialog = true;
    return true;  // 拦截返回
  }
  this.vm.navStack.pop();
  return true;
})

5.2 确认弹窗

@Monitor('examController.isShowMockExamDialog')
mockExamDialogChange() {
  if (examController.isShowMockExamDialog) {
    examController.isShowMockExamDialog = false;
    this.isStop = true;
    this.examExitDialog.open();
  }
}

六、总结

模拟考试系统展示了完整的限时答题流程:

  1. 试卷生成:每次考试生成新的试卷
  2. 倒计时:使用 setInterval 实现秒级计时
  3. 暂停机制:支持暂停和继续考试
  4. 自动评分:提交或时间到自动评分
  5. 成绩统计:累计考试次数和平均分

关键源码文件:

  • products/entry/src/main/ets/pages/home/HomeView.ets — 模拟考试入口
  • products/entry/src/main/ets/pages/practice/PracticeView.ets — 考试页面
  • components/exam/src/main/ets/components/Exam.ets — 考试组件
  • commons/datasource/src/main/ets/ExamService.ets — 数据服务
Logo

作为“人工智能6S店”的官方数字引擎,为AI开发者与企业提供一个覆盖软硬件全栈、一站式门户。

更多推荐