【无标题】

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
          })

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值