首席教师
首席教师
发布于 2024-04-09 / 25 阅读
0
0

关于翻译推理自动出题代码的简单分享

我们做翻译推理时,为了加强训练,需要多刷类似的题目,但真题有限,遂使用JS代码写一个简单的随机出题程序(其实是2021年写的代码)。

一、核心功能

首先,翻译推理常见的连接词如:

  • 如果……那么……
  • 若……则……
  • 只要……就……
  • 所有……都……
  • 为了……一定(必须)……
  • ……是……的充分条件

那么我们可以定义连接词的数组为:

let s1=["除非", "只有", "只要", "除非", "为了", "为了"]
let s2=["否则", "才", "就", "否则", "一定", "必须"]

然后需要语法完整,并定义一些人名:

let s0=["小明", "小红", "小张", "小李", "小郑", "小赵"]
let s3=["去看电影", "去吃饭", "去电影院", "去上课", "去考试", "去睡觉", "去消费", "买手机", "买电脑", "去上班"]

然后随机一些选项,从s3数组取,并加上“”即可。

那么随机题目的完整函数如下:

...
         setQues(){
				this.uNum++
				//1:推导 2:相同语句
				let qType = this.cswRandom(1,2)
				switch(qType){
					case 1:{
						let s0=["小明","小红","小张","小李","小郑","小赵"]
						let s1=["除非","只有","只要","除非","为了","为了"]
						let s2=["否则","才","就","否则","一定","必须"]
						let s3=["去看电影","去吃饭","去电影院","去上课","去考试","去睡觉","去消费","买手机","买电脑","去上班"]//10
						let n0 = this.cRand(0,5)
						let n1 = this.cRand(0,5)
						let n2s = this.cRand(0,9)//随机s3
						let n2e = this.cRand(0,9,n2s)//随机s3,排除n2s结果
						let all = [
							''+s3[n2s]+'→'+s3[n2e],'-'+s3[n2s]+'→'+s3[n2e],
							''+s3[n2s]+'→-'+s3[n2e],'-'+s3[n2s]+'→-'+s3[n2e],
							'-'+s3[n2e]+'→-'+s3[n2s],'-'+s3[n2e]+'→'+s3[n2s],
							''+s3[n2e]+'→-'+s3[n2s],''+s3[n2e]+'→'+s3[n2s]
						]//所有符合的选项
						this.question = {
							"stem":'<p>“'+s0[n0]+s1[n1]+s3[n2s]+',他'+s2[n1]+'会'+s3[n2e]+'”下列推导正确的是?</p>',
							"option":[
								"",
								"",
								""
							],
							"type":1,//题目类型
							"tips":[],
							"answer": 0,
							"analysis":"<p>暂无分析</p>",
						}
						let ans = this.cswRandom(0,2)//随机正确答案所在选项
						let n = this.cswRandom(0,1)//答案随机一种
						this.question.answer = ans
						switch(n1){
							case 0://除非.否则 -B->A|-A->B
								this.question.stem = '<p>“'+s0[n0]+s1[n1]+s3[n2s]+','+s2[n1]+'他会'+s3[n2e]+'”下列推导正确的是?</p>'
								for(let i in this.question.option){
									let n2 = this.cswRandom(0,1)
									let ls = this.cRand(0,3,1)
									this.question.option[i] = all[ls+n2*4]
								}
								this.question.option[ans] = all[1+n*4]
								break
							case 1://只有.才 B->A|-A->-B
								for(let i in this.question.option){
									let n2 = this.cswRandom(0,1)
									let ls = this.cRand(0,3,3)
									this.question.option[i] = all[ls+n2*4]
								}
								this.question.option[ans] = all[3+n*4]
								break
							case 2://只要.就
								for(let i in this.question.option){
									let n2 = this.cswRandom(0,1)
									let ls = this.cRand(0,3,0)
									this.question.option[i] = all[ls+n2*4]
								}
								this.question.option[ans] = all[0+n*4]
								break
							case 3://除非.否则不 B->A|-A->-B
								this.question.stem = '<p>“'+s0[n0]+s1[n1]+s3[n2s]+','+s2[n1]+'他不会'+s3[n2e]+'”下列推导正确的是?</p>'
								for(let i in this.question.option){
									let n2 = this.cswRandom(0,1)
									let ls = this.cRand(0,3,3)
									this.question.option[i] = all[ls+n2*4]
								}
								this.question.option[ans] = all[3+n*4]
								break
							case 4://和只要就一样
								for(let i in this.question.option){
									let n2 = this.cswRandom(0,1)
									let ls = this.cRand(0,3,0)
									this.question.option[i] = all[ls+n2*4]
								}
								this.question.option[ans] = all[0+n*4]
								break
							case 5://和只要就一样
								for(let i in this.question.option){
									let n2 = this.cswRandom(0,1)
									let ls = this.cRand(0,3,0)
									this.question.option[i] = all[ls+n2*4]
								}
								this.question.option[ans] = all[0+n*4]
								break
							default:
								break
						}
						for(let i=0;i<this.question.option.length;i++){
							this.opStyle[i] = 'background:#FDFDFD;'
						}
						break
					}
					case 2:{
						let s0=["小明","小红","小张","小李","小郑","小赵"]
						let s1=["除非","只有","只要","除非","为了","为了"]
						let s2=["否则","才","就","否则","一定","必须"]
						let s3=["去看电影","去吃饭","去电影院","去上课","去考试","去睡觉","去消费","买手机","买电脑","去上班"]//10
						let n0 = this.cRand(0,5)
						let n1 = this.cRand(0,5)
						let n2s = this.cRand(0,9)//随机s3
						let n2e = this.cRand(0,9,n2s)//随机s3,排除n2s结果
						let all = [
							''+s3[n2s]+'→'+s3[n2e],'-'+s3[n2s]+'→'+s3[n2e],
							''+s3[n2s]+'→-'+s3[n2e],'-'+s3[n2s]+'→-'+s3[n2e],
							'-'+s3[n2e]+'→-'+s3[n2s],'-'+s3[n2e]+'→'+s3[n2s],
							''+s3[n2e]+'→-'+s3[n2s],''+s3[n2e]+'→'+s3[n2s]
						]//所有符合的选项
						this.question = {
							"stem":'<p>“'+s0[n0]+s1[n1]+s3[n2s]+',他'+s2[n1]+'会'+s3[n2e]+'”与这句话意思相同的是?</p>',
							"option":[
								"",
								"",
								"",
								""
							],
							"type":1,//题目类型
							"tips":[],
							"answer": 0,
							"analysis":"<p>暂无分析</p>",
						}
						let n = this.cswRandom(0,1)//答案随机一种
						var arr = [0, 1, 2, 3]
						arr.sort(this.randomSort)//随机排序
						let correctAnswer
						switch(n1){
							case 0://除非.否则 -B->A|-A->B
								this.question.stem = '<p>“'+s0[n0]+s1[n1]+s3[n2s]+','+s2[n1]+'他会'+s3[n2e]+'”与这句话意思相同的是?</p>'
								correctAnswer = 1//正确答案
								for(let i in this.question.option){
									let n2 = this.cswRandom(0,1)
									if(arr[i] == correctAnswer){
										this.question.answer = Number(i)
										this.question.option[i] = this.translate(s0[n0],all[correctAnswer+n*4])
										this.question.analysis = '<p> <span style="color:#00F;">'+all[correctAnswer] +'</span> 或者 <span style="color:#00F;">'+all[correctAnswer+4]+'</span>,当选第'+(Number(i)+1)+'项。</p>'
									}else
										this.question.option[i] = this.translate(s0[n0],all[arr[i]+n2*4])
								}
								break
							case 1://只有.才 B->A|-A->-B
								correctAnswer = 3//正确答案
								for(let i in this.question.option){
									let n2 = this.cswRandom(0,1)
									if(arr[i] == correctAnswer){
										this.question.answer = Number(i)
										this.question.option[i] = this.translate(s0[n0],all[correctAnswer+n*4])
										this.question.analysis = '<p> <span style="color:#00F;">'+all[correctAnswer] +'</span> 或者 <span style="color:#00F;">'+all[correctAnswer+4]+'</span>,当选第'+(Number(i)+1)+'项。</p>'
									}else
										this.question.option[i] = this.translate(s0[n0],all[arr[i]+n2*4])
								}
								break
							case 2://只要.就
								correctAnswer = 0//正确答案
								for(let i in this.question.option){
									let n2 = this.cswRandom(0,1)
									if(arr[i] == correctAnswer){
										this.question.answer = Number(i)
										this.question.option[i] = this.translate(s0[n0],all[correctAnswer+n*4])
										this.question.analysis = '<p> <span style="color:#00F;">'+all[correctAnswer] +'</span> 或者 <span style="color:#00F;">'+all[correctAnswer+4]+'</span>,当选第'+(Number(i)+1)+'项。</p>'
									}else
										this.question.option[i] = this.translate(s0[n0],all[arr[i]+n2*4])
								}
								break
							case 3://除非.否则不 B->A|-A->-B
								this.question.stem = '<p>“'+s0[n0]+s1[n1]+s3[n2s]+','+s2[n1]+'他不会'+s3[n2e]+'”与这句话意思相同的是?</p>'
								correctAnswer = 3//正确答案
								for(let i in this.question.option){
									let n2 = this.cswRandom(0,1)
									if(arr[i] == correctAnswer){
										this.question.answer = Number(i)
										this.question.option[i] = this.translate(s0[n0],all[correctAnswer+n*4])
										this.question.analysis = '<p> <span style="color:#00F;">'+all[correctAnswer] +'</span> 或者 <span style="color:#00F;">'+all[correctAnswer+4]+'</span>,当选第'+(Number(i)+1)+'项。</p>'
									}else
										this.question.option[i] = this.translate(s0[n0],all[arr[i]+n2*4])
								}
								break
							case 4://和只要就一样
								correctAnswer = 0//正确答案
								for(let i in this.question.option){
									let n2 = this.cswRandom(0,1)
									if(arr[i] == correctAnswer){
										this.question.answer = Number(i)
										this.question.option[i] = this.translate(s0[n0],all[correctAnswer+n*4])
										this.question.analysis = '<p> <span style="color:#00F;">'+all[correctAnswer] +'</span> 或者 <span style="color:#00F;">'+all[correctAnswer+4]+'</span>,当选第'+(Number(i)+1)+'项。</p>'
									}else
										this.question.option[i] = this.translate(s0[n0],all[arr[i]+n2*4])
								}
								break
							case 5://和只要就一样
								correctAnswer = 0//正确答案
								for(let i in this.question.option){
									let n2 = this.cswRandom(0,1)
									if(arr[i] == correctAnswer){
										this.question.answer = Number(i)
										this.question.option[i] = this.translate(s0[n0],all[correctAnswer+n*4])
										this.question.analysis = '<p> <span style="color:#00F;">'+all[correctAnswer] +'</span> 或者 <span style="color:#00F;">'+all[correctAnswer+4]+'</span>,当选第'+(Number(i)+1)+'项。</p>'
									}else
										this.question.option[i] = this.translate(s0[n0],all[arr[i]+n2*4])
								}
								break
							default:
								break
						}
						for(let i=0;i<this.question.option.length;i++){
							this.opStyle[i] = 'background:#FDFDFD;'
						}
						break
					}
					default:{
						break
					}
				}
			},
			translate(name, t){
				//let sj = this.cRand(0,1)//多种情况,下次再加
				let tArr = t.split('→')
				if(tArr[0][0] == '-' && tArr[1][0] == '-'){
					//if(sj == 0)
					return name + '他不' + tArr[0].substr(1, tArr[0].length - 1) + ",他就不" + tArr[1].substr(1, tArr[1].length - 1)
					//else
					//return name + '只要' + tArr[1].substr(1, tArr[1].length - 1) + ",他就" + tArr[0].substr(1, tArr[0].length - 1)
				}
				if(tArr[0][0] == '-' && tArr[1][0] != '-'){
					return name + '他不' + tArr[0].substr(1, tArr[0].length - 1) + ",他就" + tArr[1]
				}
				if(tArr[0][0] != '-' && tArr[1][0] == '-'){
					return name + '只要' + tArr[0] + ",他就不" + tArr[1].substr(1, tArr[1].length - 1)
				}
				if(tArr[0][0] != '-' && tArr[1][0] != '-'){
					return name + '只要' + tArr[0] + ",他就" + tArr[1]
				}
			},

二、完整代码展示

现在随机出题完毕,需要让用户作答训练,且都喜欢使用手机训练,于是这里采用Uniapp(基于Vue)开发,可以编译成WEB端网页、微信小程序、app等。

(一)View代码

<template>
	<view>
		<view style="flex-direction:row;justify-content:space-between;padding:6rpx 10rpx 0rpx 10rpx;font-weight:bold;color:#29abe2;">
			<view style="flex-direction:row;">
				<text style="font-size:27rpx;">第</text>
				<text style="font-size:27rpx;margin-left:5rpx;">{{uNum}}</text>
				<text style="font-size:27rpx;margin-left:5rpx;">题(本地随机出题)</text>
			</view>
			<view style="flex-direction:row;">
				<text style="font-size:27rpx;">本题用时</text>
				<text style="font-size:27rpx;margin-left:10rpx;">{{$u.timeFormat(uTime, 'MM:ss')}}</text>
			</view>
		</view>
		<!-- 题干 -->
		<view class="quesTop">
			<text class="quesTopTxt">问</text>
		</view>
		<view class="stem">
			<view style="color:orange;float:right;height:20rpx;font-size:20rpx;margin-top:-18rpx;" v-for="num of question.starLevel" :key="num">
				<span :id="'s'+num" class="icon iconfont icon-star" style="font-size:20rpx;"></span>
			</view>
			<view style="margin-top:32rpx;" v-html="question.stem"></view>
			<view v-if="special != null" style="margin-top:10rpx;">
				<view style="text-align:right;color:#999;margin-right:50rpx;">
					<u-parse :content="special"></u-parse>
				</view>
			</view>
		</view>
		<!-- 选项/填空位 -->
		<template v-if="question.type == 1 || question.type == 2 || question.type == 3">
			<view style="font-size:24rpx;" v-for="(item,index) in question.option" :key="index">
				<text class="option" :style="opStyle[index]?opStyle[index]:'background:#FDFDFD;'" v-on:click="ask(index,false)">
					{{index+1}}.{{item}}
				</text>
			</view>
		</template>
		<template v-else>
			<view style="margin-left:20rpx;">
				<text style="font-size:24rpx;font-weight:bold;color:#999;">答题区:</text>
			</view>
			<view class="ask" @click="show = true">
				<view style="flex-direction:row;">
					<text style="font-size:30rpx;font-weight:bold;overflow:hidden;-webkit-line-clamp:4;text-overflow:ellipsis;display:-webkit-box;-webkit-box-orient:vertical;">{{value}}</text>
					<text :style="{'font-size':'30rpx','font-weight':'bold','color':curColor}">{{cursor}}</text>
				</view>
				<view style="justify-content:center;padding-left:10rpx;">
					<text style="font-size:30rpx;font-weight:bold;color:#007AFF;" @click="value = ''">清空</text>
				</view>
			</view>
		</template>
		<!-- 解析/答案 -->
		<view class="quesAs" v-if="isShowAn">
			<view v-html="askStatus" style="float:left;margin-right:10rpx;"></view>
			<template v-if="question.type != 4">
				<text>正确答案:第{{question.answer+1}}项</text>
			</template>
			<template v-else>
				<text>正确答案:{{question.answer}}</text>
			</template>
			<view v-html="question.analysis"></view>
		</view>
		<button class="btn" type="primary" v-on:click="ref(<!-- #ifdef MP-WEIXIN -->'wx'<!-- #endif --><!-- #ifdef MP-QQ -->'qq'<!-- #endif -->)">{{btnName}}</button>
		<view style="text-align:center;margin-bottom:26rpx;">
			<text style="font-size:28rpx;color:#999;">本地工具,不记录任何信息,不保存历史答题记录</text>
		</view>
		<view v-if="show" style="height:600rpx;"></view>
		<u-keyboard cancelText="我再想想" confirmText="提交答案" :mode="mode" :show="show" :overlay="false" @change="valChange" @backspace="backspace" @confirm="confirm" @cancel="cancel"></u-keyboard>
	</view>
</template>

(二)Script代码

<script>
	export default {
		data() {
			return{
				question:{},
				isShowAn:false,
				opStyle:[],
				btnName:'查看答案',
				askStatus:'<label style="color:#FF6600;">未作答</label>',
				uTime:0,//使用时间
				uNum:0,//第多少题
				startTime:0,//开始时间戳
				isStart:true,
				show:false,
				value:"",
				mode:"number",
				special:null,
				cursor:"|",
				curColor:'#000'
			}
		},
		onLoad() {
			this.setQues()
			this.startTime = Date.now()
			this.timeAdd()
		},
		onReady() {
			//this.show = true
			//this.doCursor()
		},
		methods: {
			ref(d){
				if(this.btnName == '查看答案'){
					this.curColor = '#FFF'
					if(this.value != ""){
						this.confirm()
						return
					}
					this.show = false
					this.isShowAn = true
					this.btnName = '继续随机出题'
					this.isStart = false
					this.askStatus = '<label style="color:#FF6600;">未作答</label>'
				}
				else{
					this.curColor = '#000'
					this.value = ""
					this.special = null
					this.isShowAn = false
					this.btnName = '查看答案'
					this.setQues()
					this.isStart = true
					this.startTime = Date.now()
					this.timeAdd()
				}
			},
			doCursor(){
				setTimeout(()=>{
					this.cursor = '|'
				setTimeout(()=>{
					this.cursor = ''
					this.doCursor()
				},500)
				},500)
			},
			cRand(s, e, flag = -1){
				if(flag < 0) return this.cswRandom(s,e)
				let ls = this.cswRandom(s,e - 1)
				if(ls >= flag) return ls+1
				return ls
			},
			randomSort(a, b){//通过随机产生0到1的数,然后判断是否大于0.5从而影响排序,产生随机性的效果
			   return Math.random()>.5 ? -1 : 1
			},
			setQues(){
				this.uNum++
				//1:推导 2:相同语句
				let qType = this.cswRandom(1,2)
				switch(qType){
					case 1:{
						let s0=["小明","小红","小张","小李","小郑","小赵"]
						let s1=["除非","只有","只要","除非","为了","为了"]
						let s2=["否则","才","就","否则","一定","必须"]
						let s3=["去看电影","去吃饭","去电影院","去上课","去考试","去睡觉","去消费","买手机","买电脑","去上班"]//10
						let n0 = this.cRand(0,5)
						let n1 = this.cRand(0,5)
						let n2s = this.cRand(0,9)//随机s3
						let n2e = this.cRand(0,9,n2s)//随机s3,排除n2s结果
						let all = [
							''+s3[n2s]+'→'+s3[n2e],'-'+s3[n2s]+'→'+s3[n2e],
							''+s3[n2s]+'→-'+s3[n2e],'-'+s3[n2s]+'→-'+s3[n2e],
							'-'+s3[n2e]+'→-'+s3[n2s],'-'+s3[n2e]+'→'+s3[n2s],
							''+s3[n2e]+'→-'+s3[n2s],''+s3[n2e]+'→'+s3[n2s]
						]//所有符合的选项
						this.question = {
							"stem":'<p>“'+s0[n0]+s1[n1]+s3[n2s]+',他'+s2[n1]+'会'+s3[n2e]+'”下列推导正确的是?</p>',
							"option":[
								"",
								"",
								""
							],
							"type":1,//题目类型
							"tips":[],
							"answer": 0,
							"analysis":"<p>暂无分析</p>",
						}
						let ans = this.cswRandom(0,2)//随机正确答案所在选项
						let n = this.cswRandom(0,1)//答案随机一种
						this.question.answer = ans
						switch(n1){
							case 0://除非.否则 -B->A|-A->B
								this.question.stem = '<p>“'+s0[n0]+s1[n1]+s3[n2s]+','+s2[n1]+'他会'+s3[n2e]+'”下列推导正确的是?</p>'
								for(let i in this.question.option){
									let n2 = this.cswRandom(0,1)
									let ls = this.cRand(0,3,1)
									this.question.option[i] = all[ls+n2*4]
								}
								this.question.option[ans] = all[1+n*4]
								break
							case 1://只有.才 B->A|-A->-B
								for(let i in this.question.option){
									let n2 = this.cswRandom(0,1)
									let ls = this.cRand(0,3,3)
									this.question.option[i] = all[ls+n2*4]
								}
								this.question.option[ans] = all[3+n*4]
								break
							case 2://只要.就
								for(let i in this.question.option){
									let n2 = this.cswRandom(0,1)
									let ls = this.cRand(0,3,0)
									this.question.option[i] = all[ls+n2*4]
								}
								this.question.option[ans] = all[0+n*4]
								break
							case 3://除非.否则不 B->A|-A->-B
								this.question.stem = '<p>“'+s0[n0]+s1[n1]+s3[n2s]+','+s2[n1]+'他不会'+s3[n2e]+'”下列推导正确的是?</p>'
								for(let i in this.question.option){
									let n2 = this.cswRandom(0,1)
									let ls = this.cRand(0,3,3)
									this.question.option[i] = all[ls+n2*4]
								}
								this.question.option[ans] = all[3+n*4]
								break
							case 4://和只要就一样
								for(let i in this.question.option){
									let n2 = this.cswRandom(0,1)
									let ls = this.cRand(0,3,0)
									this.question.option[i] = all[ls+n2*4]
								}
								this.question.option[ans] = all[0+n*4]
								break
							case 5://和只要就一样
								for(let i in this.question.option){
									let n2 = this.cswRandom(0,1)
									let ls = this.cRand(0,3,0)
									this.question.option[i] = all[ls+n2*4]
								}
								this.question.option[ans] = all[0+n*4]
								break
							default:
								break
						}
						for(let i=0;i<this.question.option.length;i++){
							this.opStyle[i] = 'background:#FDFDFD;'
						}
						break
					}
					case 2:{
						let s0=["小明","小红","小张","小李","小郑","小赵"]
						let s1=["除非","只有","只要","除非","为了","为了"]
						let s2=["否则","才","就","否则","一定","必须"]
						let s3=["去看电影","去吃饭","去电影院","去上课","去考试","去睡觉","去消费","买手机","买电脑","去上班"]//10
						let n0 = this.cRand(0,5)
						let n1 = this.cRand(0,5)
						let n2s = this.cRand(0,9)//随机s3
						let n2e = this.cRand(0,9,n2s)//随机s3,排除n2s结果
						let all = [
							''+s3[n2s]+'→'+s3[n2e],'-'+s3[n2s]+'→'+s3[n2e],
							''+s3[n2s]+'→-'+s3[n2e],'-'+s3[n2s]+'→-'+s3[n2e],
							'-'+s3[n2e]+'→-'+s3[n2s],'-'+s3[n2e]+'→'+s3[n2s],
							''+s3[n2e]+'→-'+s3[n2s],''+s3[n2e]+'→'+s3[n2s]
						]//所有符合的选项
						this.question = {
							"stem":'<p>“'+s0[n0]+s1[n1]+s3[n2s]+',他'+s2[n1]+'会'+s3[n2e]+'”与这句话意思相同的是?</p>',
							"option":[
								"",
								"",
								"",
								""
							],
							"type":1,//题目类型
							"tips":[],
							"answer": 0,
							"analysis":"<p>暂无分析</p>",
						}
						let n = this.cswRandom(0,1)//答案随机一种
						var arr = [0, 1, 2, 3]
						arr.sort(this.randomSort)//随机排序
						let correctAnswer
						switch(n1){
							case 0://除非.否则 -B->A|-A->B
								this.question.stem = '<p>“'+s0[n0]+s1[n1]+s3[n2s]+','+s2[n1]+'他会'+s3[n2e]+'”与这句话意思相同的是?</p>'
								correctAnswer = 1//正确答案
								for(let i in this.question.option){
									let n2 = this.cswRandom(0,1)
									if(arr[i] == correctAnswer){
										this.question.answer = Number(i)
										this.question.option[i] = this.translate(s0[n0],all[correctAnswer+n*4])
										this.question.analysis = '<p> <span style="color:#00F;">'+all[correctAnswer] +'</span> 或者 <span style="color:#00F;">'+all[correctAnswer+4]+'</span>,当选第'+(Number(i)+1)+'项。</p>'
									}else
										this.question.option[i] = this.translate(s0[n0],all[arr[i]+n2*4])
								}
								break
							case 1://只有.才 B->A|-A->-B
								correctAnswer = 3//正确答案
								for(let i in this.question.option){
									let n2 = this.cswRandom(0,1)
									if(arr[i] == correctAnswer){
										this.question.answer = Number(i)
										this.question.option[i] = this.translate(s0[n0],all[correctAnswer+n*4])
										this.question.analysis = '<p> <span style="color:#00F;">'+all[correctAnswer] +'</span> 或者 <span style="color:#00F;">'+all[correctAnswer+4]+'</span>,当选第'+(Number(i)+1)+'项。</p>'
									}else
										this.question.option[i] = this.translate(s0[n0],all[arr[i]+n2*4])
								}
								break
							case 2://只要.就
								correctAnswer = 0//正确答案
								for(let i in this.question.option){
									let n2 = this.cswRandom(0,1)
									if(arr[i] == correctAnswer){
										this.question.answer = Number(i)
										this.question.option[i] = this.translate(s0[n0],all[correctAnswer+n*4])
										this.question.analysis = '<p> <span style="color:#00F;">'+all[correctAnswer] +'</span> 或者 <span style="color:#00F;">'+all[correctAnswer+4]+'</span>,当选第'+(Number(i)+1)+'项。</p>'
									}else
										this.question.option[i] = this.translate(s0[n0],all[arr[i]+n2*4])
								}
								break
							case 3://除非.否则不 B->A|-A->-B
								this.question.stem = '<p>“'+s0[n0]+s1[n1]+s3[n2s]+','+s2[n1]+'他不会'+s3[n2e]+'”与这句话意思相同的是?</p>'
								correctAnswer = 3//正确答案
								for(let i in this.question.option){
									let n2 = this.cswRandom(0,1)
									if(arr[i] == correctAnswer){
										this.question.answer = Number(i)
										this.question.option[i] = this.translate(s0[n0],all[correctAnswer+n*4])
										this.question.analysis = '<p> <span style="color:#00F;">'+all[correctAnswer] +'</span> 或者 <span style="color:#00F;">'+all[correctAnswer+4]+'</span>,当选第'+(Number(i)+1)+'项。</p>'
									}else
										this.question.option[i] = this.translate(s0[n0],all[arr[i]+n2*4])
								}
								break
							case 4://和只要就一样
								correctAnswer = 0//正确答案
								for(let i in this.question.option){
									let n2 = this.cswRandom(0,1)
									if(arr[i] == correctAnswer){
										this.question.answer = Number(i)
										this.question.option[i] = this.translate(s0[n0],all[correctAnswer+n*4])
										this.question.analysis = '<p> <span style="color:#00F;">'+all[correctAnswer] +'</span> 或者 <span style="color:#00F;">'+all[correctAnswer+4]+'</span>,当选第'+(Number(i)+1)+'项。</p>'
									}else
										this.question.option[i] = this.translate(s0[n0],all[arr[i]+n2*4])
								}
								break
							case 5://和只要就一样
								correctAnswer = 0//正确答案
								for(let i in this.question.option){
									let n2 = this.cswRandom(0,1)
									if(arr[i] == correctAnswer){
										this.question.answer = Number(i)
										this.question.option[i] = this.translate(s0[n0],all[correctAnswer+n*4])
										this.question.analysis = '<p> <span style="color:#00F;">'+all[correctAnswer] +'</span> 或者 <span style="color:#00F;">'+all[correctAnswer+4]+'</span>,当选第'+(Number(i)+1)+'项。</p>'
									}else
										this.question.option[i] = this.translate(s0[n0],all[arr[i]+n2*4])
								}
								break
							default:
								break
						}
						for(let i=0;i<this.question.option.length;i++){
							this.opStyle[i] = 'background:#FDFDFD;'
						}
						break
					}
					default:{
						break
					}
				}
			},
			translate(name, t){
				//let sj = this.cRand(0,1)//多种情况,下次再加
				let tArr = t.split('→')
				if(tArr[0][0] == '-' && tArr[1][0] == '-'){
					//if(sj == 0)
					return name + '他不' + tArr[0].substr(1, tArr[0].length - 1) + ",他就不" + tArr[1].substr(1, tArr[1].length - 1)
					//else
					//return name + '只要' + tArr[1].substr(1, tArr[1].length - 1) + ",他就" + tArr[0].substr(1, tArr[0].length - 1)
				}
				if(tArr[0][0] == '-' && tArr[1][0] != '-'){
					return name + '他不' + tArr[0].substr(1, tArr[0].length - 1) + ",他就" + tArr[1]
				}
				if(tArr[0][0] != '-' && tArr[1][0] == '-'){
					return name + '只要' + tArr[0] + ",他就不" + tArr[1].substr(1, tArr[1].length - 1)
				}
				if(tArr[0][0] != '-' && tArr[1][0] != '-'){
					return name + '只要' + tArr[0] + ",他就" + tArr[1]
				}
			},
			ask(index,flag){//仅选择题
				if(this.isShowAn == true) return
				if(flag){
					this.opStyle[this.question.answer] = 'background:#deffe0;'
					this.askStatus = '<label style="color:#FF6600;">未作答</label>'
				}
				else{
					if(this.question.answer == index){
						this.askStatus = '<label style="color:#00FF00;">回答正确</label>'
						this.opStyle[this.question.answer] = 'background:#deffe0;'
					}
					else{
						this.askStatus = '<label style="color:#FF0000;">回答错误</label>'
						this.opStyle[this.question.answer] = 'background:#deffe0;'
						this.opStyle[index] = 'background:#ffdede;'
					}
				}
				this.isStart = false
				this.isShowAn = true
				this.btnName = '继续出题'
			},
			reset(){
				this.startTime = Date.now()
			},
			timeAdd(){
				setTimeout(() => {
					this.uTime = Date.now() - this.startTime - 28800000
					if(this.isStart) this.timeAdd()
				},50)
			},
			valChange(val) {//按键被点击(点击退格键不会触发此事件)
				this.value += val
			},
			backspace() {//删除value的最后一个字符
				if(this.value.length)
					this.value = this.value.substr(0, this.value.length - 1)
			},
			confirm(){
				this.show = false
				if(this.value == this.question.answer){
					this.askStatus = '<label style="color:#00FF00;">回答正确</label>'
				}else{
					this.askStatus = '<label style="color:#FF0000;">回答错误</label>'
				}
				this.isStart = false
				this.isShowAn = true
				this.btnName = '继续出题'
			},
			cancel(){
				this.show = false
			}
		}
	}
</script>

(三)CSS代码

<style lang="scss">
	page{background-color: #F6F6F6;}
	checkbox-group label{font-size:30rpx;}
	.mainTop{text-align:center;box-shadow: 0px 0px 2px #CCC;border-radius:10rpx;background:#FBFBFB;font-size:32rpx;line-height:50rpx;height:50rpx;margin:10rpx;}
	.quesTop{position:relative;text-align:center;width:100%;z-index:1;}
	.quesTopTxt{box-shadow: 0px 0px 2px #CCC;margin:20rpx 335rpx;width:80rpx;height:80rpx;border-radius:40rpx;background:#FBFBFB;line-height:80rpx;font-size:38rpx;font-weight:bold;}
	.stem{box-shadow: 0px 0px 2px #CCC;font-size:34rpx;border-radius:16rpx;padding:20rpx;background:#FDFDFD;margin:-60rpx 20rpx 40rpx 20rpx;font-weight:bold;}
	.option{opacity:1;box-shadow: 0px 0px 2px #CCC;color:#000;background:#FDFDFD;margin:15rpx 20rpx;padding:20rpx;text-decoration:none;display:block;text-align:left;font-size:30rpx;border-radius:16rpx;font-weight:bold;}
	.ask{opacity:1;box-shadow: 0px 0px 2px #CCC;color:#000;background:#FDFDFD;margin:15rpx 20rpx;padding:20rpx;flex-direction:row;justify-content:space-between;}
	.quesAs{box-shadow: 0px 0px 2px #CCC;color:#000;background:#FDFDFD;margin:40rpx 20rpx;padding:20rpx;text-decoration:none;display:block;text-align:left;font-size:28rpx;border-radius:16rpx;font-weight:bold;}
	.btn{margin:20rpx;font-size:30rpx;}
</style>

三、最终效果

pdtl-1.jpg


评论