1.switch 语法用法

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("输入错误")

}

第二种形式

let www: number = 1;
switch (www) {
    case 1:
    case 3:
    case 5:
        console.log("吃土豆丝");
        break;

    case 2:
    case 4:
    case 6:
        console.log("吃屎");
        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

          })
        this.uy=0
        this.cy=0
        this.py=0
        this.isj=false
        this.iss=false
        this.isb=false

      })

  • 9
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值