1.switch 语法用法
/ switch('周一'){
// case 1:
// break;
// case 2:
// break;
// case 3:
// break ;
// default:
// break
// }
// 张晓文 买彩票
// 中100万 买五菱宏光
// 中50万 雅迪
// 中10万 买吉安特
// 中10块 买个冰棍
// 其他 买个锤子
let money:number=10;
switch(money){
case 1000000:
console.log("买五菱宏光");
break;
case 500000:
console.log("雅迪");
break;
case 100000:
console.log("买吉安特");
break ;
case 10:
console.log("买个冰棍");
break;
default:
console.log("买个锤子");
}
2.剪刀石头布
2.1 页面
2.2 变量
@Entry
@Component
struct GameTest {
@State message: string = '剪刀石头布';
@State userCz:string='';//用户出招
@State comCz:string='';//电脑出招
@State res: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处理用户出招
.onClick(()=>{
//用户出招
if (this.userCz!=='') {
// Math.random() 产生0~1之间的一个小数
// 0 剪刀 1 石头 2 布
let num:number=parseInt(`${Math.random()*3}`)
// promptAction.showToast({message:`${num}`})
switch (num){
case 0:
this.comCz="剪刀"
break;
case 1:
this.comCz="石头"
break
case 2:
this.comCz="布"
break;
}
2.4电脑出招
Button("电脑出招")
.onClick(()=>{
//用户出招
if (this.userCz!=='') {
// Math.random() 产生0~1之间的一个小数
// 0 剪刀 1 石头 2 布
let num:number=parseInt(`${Math.random()*3}`)
// promptAction.showToast({message:`${num}`})
switch (num){
case 0:
this.comCz="剪刀"
break;
case 1:
this.comCz="石头"
break
case 2:
this.comCz="布"
break;
}
2.5数据统计
Text(`赢:${this.uY}`).fontStyle(40).fontColor(`red`).width('40%')
Text(`败:${this.cY}`).fontStyle(40).fontColor(`red`).width('40%')
Text(`平:${this.pY}`).fontStyle(40).fontColor(`red`).width('40%')
Text(`总:${this.uY+this.pY+this.pY}`).fontStyle(40).fontColor(`red`).width('40%')
Text(`胜率:${this.uY}/${this.uY+this.cY+this.pY}`).fontStyle(40).fontColor(`red`).width('40%')
}
2.6 重新开始
Button("重新开始")
.onClick(()=>{
//清零 重新初始化
this.userCz=''
this.comCz=''
this.res=''
this.uY=0;
this.cY=0
this.pY=0
//初始化单选的选项
this.isJ=false
this.isS=false
this.isB=false
})