TypeScript(switch判断)
创始人
2024-11-15 15:35:34
0
1.switch 语法用法
switch是对某个表达式的值做出判断。然后决定程序执行哪一段代码
case语句中指定的每个值必须具有与表达式兼容的类型

语法switch(表达式){

case 值1:

​ 执行语句块1

break;

case 值2:

​ 执行语句块3

break;

dfault:

//如果以上所有case都不匹配,执行默认语句块

}

如:

//  买彩票 //中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('买个锤子'); } 
switch语句的执行顺序是,首先计算表达式的值,然后将该值与每一个case的值进行比较,如果找到匹配的case,则执行该case下的语句,直到遇到break语句或Switch语句结束。如果没有找到匹配的case,且存在default子句,就执行default。
2.剪刀石头布
2.1 页面

在这里插入图片描述

2.2变量
// 定义变量   @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处理用户出招
Row(){     Radio({value:'剪刀',group:'gm'})       .onChange((isChecked:boolean)=>{         promptAction.showToast({message:`剪刀:${isChecked}`})         this.userCz='剪刀'         // this.isC=isChecked         this.isJ=isChecked       })       .checked(this.isJ)     Text('剪刀').fontSize(30)     Radio({value:'石头',group:'gm'})       .onChange((isChecked:boolean)=>{         promptAction.showToast({message:`石头:${isChecked}`})         this.userCz='石头'         // this.isC=isChecked         this.isS=isChecked        })       .checked(this.isS)     Text('石头').fontSize(30)     Radio({value:'布',group:'gm'})//group:分组       .onChange((isChecked:boolean)=>{         promptAction.showToast({message:`布:${isChecked}`})         this.userCz='布'         // this.isC=isChecked         this.isB=isChecked        })       .checked(this.isB)     Text('布').fontSize(30)   }.width('100%')      .height(50)      .justifyContent(FlexAlign.SpaceBetween) 
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}`).fontSize(30).fontColor("red").width('40%')      Text(`败:${this.cY}`).fontSize(30).fontColor("red").width('40%')      Text(`平:${this.pY}`).fontSize(30).fontColor("red").width('40%')      Text(`总:${this.uY+this.cY+this.pY}`).fontSize(30).fontColor("red").width('40%')      // Text(`胜率:${this.uY/(this.uY+this.cY+this.pY)}`).fontSize(30).fontColor("red").width('40%')      Text(`胜率:${this.uY}/${(this.uY+this.cY+this.pY)}`).fontSize(30).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          }) 

相关内容

热门资讯

近年来!wepoker亲友圈有... 近年来!wepoker亲友圈有用吗,wejoker开挂(透视)举措脚本(真是有挂)-哔哩哔哩暗藏猫腻...
透视辅助!微乐江西小程序辅助,... 透视辅助!微乐江西小程序辅助,海贝之城正版辅助(开挂)竟然真的有挂(真实有挂)-哔哩哔哩进入游戏-大...
网友热议!微信小程序途游辅助免... 网友热议!微信小程序途游辅助免费,新久久辅助器(透视)软件-竟然真的有挂1、进入游戏-大厅左侧-新手...
反观!德普之星辅助工具如何打开... 反观!德普之星辅助工具如何打开,wepoker俱乐部辅助器(透视)教程书教程(原来是真的挂)1、德普...
透视透视!微信小程序微乐破解填... 透视透视!微信小程序微乐破解填大坑,微乐自建房辅助软件下载(开挂)好像真的是有挂(有挂透明挂)-哔哩...
有消息称!hhpoker透视工... 有消息称!hhpoker透视工具,hhpoker是正品吗(透视)教程书脚本(确实有挂)-哔哩哔哩一、...
据公告内容!光明辅助器,河洛杠... 据公告内容!光明辅助器,河洛杠次脚本开发(透视)app-其实存在有挂1、河洛杠次脚本开发透视辅助软件...
近日!wepoker辅助器是真... 近日!wepoker辅助器是真的吗,德州之星扫描器(透视)指南教程(确实存在有挂);1、实时wepo...
透视脚本!爱玩联盟辅助器,新上... 透视脚本!爱玩联盟辅助器,新上游反杀辅助(开挂)一贯是真的挂(有挂方式)-哔哩哔哩1、下载好新上游反...
近日!wepoker私人局可以... 近日!wepoker私人局可以透视,wepoker透视脚本(透视)妙招脚本(其实存在有挂)-哔哩哔哩...