1.switch 语法用法
switch结构
switch 只能做等值判断 区间范围无法使用
let week: number = 3;
switch (week) {
case 0:
console.log("周日睡觉");
break;
case 1:
console.log("周1睡觉");
break;
case 2:
console.log("周2睡觉");
break;
case 3:
console.log("周3睡觉");
break;
case 4:
console.log("周4睡觉");
break;
case 5:
console.log("周5睡觉");
break;
case 6:
console.log("周6睡觉");
break;
default:
console.log("输入错误")
}
2.剪刀石头布
2.1 页面
2.2 变量
@State message: string = '剪刀石头布';
@State userCz: string = ''; //用户出招
@State comCz: string = ''; //电脑出招
@State rec: string = ''; //结果
@State uy:number=0//用户赢的次数
@State cy:number=0//电脑赢的次数
@State py:number=0//平局的次数
@State isj:boolean=false//选中状态
@State iss:boolean=false//选中状态
@State isb:boolean=false//选中状态
2.3处理用户出招
Radio({ value: "剪刀", group: "gm" })
.onChange((isChecked: boolean) => {
promptAction.showToast({ message: `剪刀:${isChecked}` })
this.userCz = '剪刀'
this.isj=isChecked
})
.checked(this.isj)
2.4电脑出招
if(this.userCz!==""){
// Math.random() 产生0~1之间的小数
//0 剪刀 1石头 2布
let num: number = parseInt(`${Math.random() * 3}`)
switch (num) {
case 0:
this.comCz = "剪刀"
break;
case 1:
this.comCz = "石头"
break;
case 2:
this.comCz = "布"
break;
}
2.5数据统计
if((this.comCz=="剪刀"&&this.userCz=="布")||(this.comCz=="石头"&&this.userCz=="剪刀")|| (this.comCz=="布"&&this.userCz=="石头")
){
this.rec='电脑'
this.cy++
}else if((this.userCz=="剪刀"&&this.comCz=="布")|| (this.userCz=="石头"&&this.comCz=="剪刀")|| (this.userCz=="布"&&this.comCz=="石头")
){
this.rec="用户"
this.uy++
}else {
this.rec="平局"
this.py++
}
}else {
promptAction.showToast({message:'请用户请出招'})
}
})
}.width('80%').justifyContent(FlexAlign.SpaceAround)
Text(`赢:${this.uy}`).fontSize(30).fontColor('red')
Text(`败:${this.cy}`).fontSize(30).fontColor('red')
Text(`平:${this.py}`).fontSize(30).fontColor('red')
Text(`总局数:${this.uy+this.cy+this.py}`).fontSize(30).fontColor('red')
Text(`胜率:${this.uy/(this.uy+this.cy+this.py)}`).fontSize(30).fontColor('red')
2.6 重新开始
Button("重新开始")
.onClick(()=>{
this.userCz=''
this.comCz=''
this.rec=''
this.uy=0
this.cy=0
this.py=0
this.isj=false
this.iss=false
this.isb=false
})