TS选择结构

TS选择结构

if(表达式){成立后执行的内容}

let ages:number=19
if(ages>=18){
    console.log("已成年");
}
if(ages<18){
    console.log("未成年");
}

if(){}else{}

let widths:number=120;
if(widths%2===0){
    console.log("偶数");
}else{
    console.log("奇数");
}

if(){}else if(){}else{}

let times:number=21;
if(times<=10){
    console.log("进国家队");
}else if(times<=11 && times>10){
    console.log("进省队");
}else if(times<=13){
    console.log("进市队");
}else{
    console.log("进村");
}

计算器

import { promptAction } from '@kit.ArkUI';

@Entry
@Component
struct JiSuanQiPage {
  @State message: string = 'Hello World';
  @State num1:number=0;//数字1
  @State num2:number=0;//数字2
  @State num3:number=0;//结果
  @State fuHao:string='+'
  build() {
    Column() {
      Text(`${this.num1} ${this.fuHao} ${this.num2}=${this.num3}`)
      Row({space:25}){
        TextInput({text:'0'}).width(100)
          .onChange(val=>{
            this.num1=parseFloat(val)
          })
        TextInput({text:'*'}).width(100)
          .onChange(val=>{
            this.fuHao=val
          })
        TextInput({text:'0'}).width(100)
          .onChange(val=>{
            this.num2=parseFloat(val)
          })
      }
      .width('100%')
      .height(100)

      Button('计算').width('80%')
        // 单击运行
        .onClick(()=>{
          if(this.fuHao==='+'){
            this.num3=this.num1+this.num2
          }else if(this.fuHao==='-'){
            this.num3=this.num1-this.num2
          }else if(this.fuHao==='*'){
            this.num3=this.num1*this.num2
          }else if(this.fuHao==='/'){
            this.num3=this.num1/this.num2
          }else if(this.fuHao==='%'){
            this.num3=this.num1%this.num2
          }else{
            // 弹出一个提示框
            promptAction.showToast({message:"输入错误"})
          }
        })
    }
    .height('100%')
    .width('100%')
  }
}
@Entry
@Component
struct Test01 {
  @State message: string = '输入一个数字,判断是不是偶数';
  @State num1:number=0;//输入框默认值
  @State res:string='';
  //@State 同步刷新值
  // 调用build之外的变量要使用this
  build() {
    Column(){
      // TextInput({placeholder:'请输入数字'})
      //   .type(InputType.Number)//只能输入整数
      //   // onChange当值发生改变时会执行下边的代码
      //   .onChange(val=>{  //val 默认值,输入的值
      //     this.num1=parseInt(val);
      //   })
      // Text(`${this.num1}`)
      Text(`${this.message}`).fontSize(25)
      Text(`${this.num1}:${this.res}`)
        .fontSize(30)
        .backgroundColor(Color.Pink)
        .width('80%')
      TextInput({placeholder:'请输入一个数字'})
        .onChange(val=>{
          this.num1=parseFloat(val)
        })
      Button("判断").width('80%')
        .onClick(()=>{
          if(this.num1%2===0){
            this.res='是偶数'
          }else{
            this.res='不是偶数'
          }
        })
    }
    .width('100%')
    .height('100%')
  }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值