Dart运算符
算术运算符
+ - * / ~/ (取整) %(取余)
int a=13;
int b=5;
print(a+b); //加
print(a-b); //减
print(a*b); //乘
print(a/b); //除
print(a%b); //其余
print(a~/b); //取整
var c=a*b;
print('--------');
print(c);
关系运算符
== != > < >= <=
void main(){
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 */
bool a = false;
bool b = false;
print(a || b);
//如果一个人的年龄是20 并且 sex是女的话我们打印这个人
int age = 20;
String sex = "女";
if (age == 20 && sex == "女") {
print("$age --- $sex");
} else {
print("不打印");
}
//如果一个人的年龄是20 或者 sex是女的话我们打印这个人
int age = 23;
String sex = "女";
if (age == 20 || sex == "女") {
print("$age --- $sex");
} else {
print("不打印");
}
}
赋值运算符
基础赋值运算符 = ??=
复合赋值运算符 += -= *= /= %= ~/=
void main() {
int a = 10;
int b = 3;
print(a);
int c = a + b; //从右向左
// b??=23; 表示如果b为空的话把 23赋值给b
int b = 6;
b ??= 23;
print(b);
int b;
b ??= 23;
print(b);
//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);
}
条件表达式
if else
switch case
void main() {
//1、if else switch case
bool flag = true;
if (flag) {
print('true');
} else {
print('false');
}
//判断一个人的成绩 如果大于60 显示及格 如果大于 70显示良好 如果大于90显示优秀
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、三目运算符
var falg = true;
var c;
if (falg) {
c = '我是true';
} else {
c = "我是false";
}
print(c);
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);
}
类型转换
Number
与String
类型之间的转换
其他类型转换成Booleans
类型
void main() {
//1、Number与String类型之间的转换
// Number类型转换成String类型 toString()
// String类型转成Number类型 int.parse()
String str = '123';
var myNum = int.parse(str);
print(myNum is int);
String str = '123.1';
var myNum = double.parse(str);
print(myNum is double);
String price = '12';
var myNum = double.parse(price);
print(myNum);
print(myNum is double);
//报错
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);
}
var myNum = 12;
var str = myNum.toString();
print(str is String);
// 2、其他类型转换成Booleans类型
// isEmpty:判断字符串是否为空
var str = '';
if (str.isEmpty) {
print('str空');
} else {
print('str不为空');
}
var myNum = 123;
if (myNum == 0) {
print('0');
} else {
print('非0');
}
var myNum;
if (myNum == 0) {
print('0');
} else {
print('非0');
}
var myNum;
if (myNum == null) {
print('空');
} else {
print('非空');
}
var myNum = 0 / 0;
print(myNum);
if (myNum.isNaN) {
print('NaN');
}
}
网友评论