Dart运算符、表达式、类型转换

2019-07-30  本文已影响0人  二猪哥

Dart运算符:

 +    -    *    /     ~/ (取整)     %(取余)
  int a=13;
  int b=5;

  print(a+b);   //加         18
  print(a-b);   //减          8
  print(a*b);   //乘         65
  print(a/b);   //除         2.6
  print(a%b);   //其余       3
  print(a~/b);  //取整       2

  var c=a*b;
  print('--------');
  print(c);
 ==    !=     >      <      >=      <=
  int a=5;
  int b=3;

  print(a==b);   //判断是否相等
  print(a!=b);   //判断是否不等
  print(a>b);   //判断是否大于
  print(a<b);   //判断是否小于
  print(a>=b);   //判断是否大于等于
  print(a<=b);   //判断是否小于等于


  if(a>b){
    print('a大于b');
  }else{
    print('a小于b');
  }

 !      &&       ||
  bool flag=false;
  print(!flag);   //取反

 /* && 并且:全部为true的话值为true 否则值为false */ 
  bool a=true;
  bool b=true;
  print(a && b);

/* ||或者:全为false的话值为false 否则值为true */ 
=    ?? =
   int a=10;
   int b=3;
   print(a);

   int c=a+b;   //从右向左     //13


   b??=23;  表示如果b为空的话把 23赋值给b
        
   int b=6;
   b??=23;
   print(b);     //6

      
   int b;
   b??=23;
   print(b);       //23

2、复合赋值运算符

+=     -=    *=     /=     %=    ~/=

 var a=12;

 a=a+10;

 print(a);



 var a=13;
 a+=10;   //表示a=a+10
 print(a);

 var a=4;
 a~/=3;   //a=a~/3;
 print(a);     //  1
  var score=41;
  if(score>90){
    print('优秀');
  }else if(score>70){
     print('良好');
  }else if(score>=60){
    print('及格');
  }else{
    print('不及格');
  }


  var sex="女";
  switch(sex){
    case "男":
      print('性别是男');
      break;
    case "女":
      print('性别是女');
      print('性别是女');
      break;
    default:
      print('传入参数错误');
      break;
  }

2、三目运算符

  bool flag=false;
  String c=flag?'我是true':'我是false';
  print(c);

3、 ?? 运算符

  var a;
  var b= a ?? 10;
  print(b);       //10

  var a = 22;
  var b= a ?? 10;

  print(b);    // 22
//自增
  var a=10;
  a++;   //a=a+1;
  print(a); 11

  var a=10;
  ++a;   //a=a+1;
  print(a); 11

//自减
 var a=10;
 a--;   //a=a-1;
 print(a) 9;

 var a=10;
 --a;   //a=a-1;
 print(a) 9;

//赋值运算 如果++ --写在后面 先赋值后运行运算
 var a=10;
 var b = a++;   //b = a=a+1;
 print(a); 11
 print(b); 10

 var a=10;
 var b = a--;   //b = a=a-1;
 print(a); 9
 print(b); 10

赋值运算 如果++ -- 写在前面 这时候先运算
 var a=10;
 var b=++a;
 print(a);  //11
 print(b);  //11

 var a=10;
 var b=--a;
 print(a);  //9
 print(b);  //9

类型转换

// String类型转成Number类型 int.parse()
 String str='123';
 var myNum=int.parse(str);
 print(myNum is int);     //true


 String str='123.1';
 var myNum=double.parse(str);
 print(myNum is double);   //true


 String price='12';
 var myNum=double.parse(price);
 print(myNum);
 print(myNum is double);  //true


/*报错*/
 String price='';
 var myNum=double.parse(price);
 print(myNum);
 print(myNum is double);

  异常处理:   try  ... catch
  String price='';
  try{
     var myNum=double.parse(price);
     print(myNum);
  }catch(err){
     print(0);
  } 


// Number类型转换成String类型 toString()
var myNum=12;
var str=myNum.toString();
print(str is String);

 var str='';
 if(str.isEmpty){
     print('str空');
 }else{
     print('str不为空');
 }


 var myNum=123;
 if(myNum==0){
    print('0');
 }else{
   print('非0');
 }


 var myNum=0/0;
 // print(myNum);
 if(myNum.isNaN){
    print('NaN');
 }
上一篇下一篇

猜你喜欢

热点阅读