dart 基础1(数据类型,string类型的插入,常量的定义,控制逻辑,条件表达式)

1 dart内置的数据类型有:
描述数字的 int double
字符串数据类型:String
布尔类型:bool类型
列表list以及具有映射关系的map。

注意:
所有的数据类型都是对象,所以他们的初始值都被定义为"null"。

void main(List<String> arguments) {

	// Numbers: int
	int score = 23;
	var count = 23;     // It is inferred as integer automatically by compiler
	int hexValue = 0xEADEBAEE;

	// Numbers: double
	double percentage = 93.4;
	var percent = 82.533;
	double exponents = 1.42e5; 

	// Strings
	String name = "Henry";
	var company = "Google";

	// Boolean
	bool isValid = true;
	var isAlive = false;

	print(score);
	print(exponents);

}

2 String类型以及string类型的插入。
注意:
String类型的插入使用 [“My name is $name”]这种方式,替代了 ["My name is " + name]。
当插入的String类型任然需要一定操作的时候可以使用大括号包裹的方式:
print(“The sum of $l and $b is ${l + b}”);

void main() {

	// Literals
	var isCool = true;
	int x = 2;
	"John";
	4.5;

	// Various ways to define String Literals in Dart
	String s1 = 'Single';
	String s2 = "Double";
	String s3 = 'It\'s easy';
	String s4 = "It's easy";

	String s5 = 'This is going to be a very long String. '
			'This is just a sample String demo in Dart Programming Language';
	String name = "Kevin";
	print("My name is $name");
	print("The number of characters in String Kevin is ${name.length}");
	int l = 20;
	int b = 10;
	print("The area of rectangle with length $l and breadth $b is ${l * b}");
}

3 常量的定义:
dart中有两种定义常量的方式:
一种是用 final, 另一种是用 const。
二者的区别在于:final 可以不用先赋值,const 声明时必须赋值,不然会报错。

void main() {

	// final
	final cityName = 'Mumbai';
	//	name = 'Peter';     // Throws an error
	final String countryName = 'India';
	// const
	const PI = 3.14;
	const double gravity = 9.8;
}

class Circle {
	final color = 'Red';
	static const PI = 3.14;
}

4 dart中的控制逻辑
if else 的使用与java一致

void main() {

	// IF and ELSE Statements
	var salary = 15000;

	if (salary > 20000) {
		print("You got promotion. Congratulations !");
	} else {
		print("You need to work hard !");
	}

	// IF ELSE IF Ladder statements
	var marks = 70;

	if (marks >= 90 && marks < 100) {
		print("A+ grade");
	} else if (marks >= 80 && marks < 90) {
		print("A grade");
	} else if (marks >= 70 && marks < 80) {
		print("B grade");
	} else if (marks >= 60 && marks < 70) {
		print("C grade");
	} else if (marks > 30 && marks < 60) {
		print("D grade");
	} else if (marks >= 0 && marks < 30) {
		print("You have failed");
	} else {
		print("Invalid Marks. Please try again !");
	}
}

5dart中的条件表达式:
1:condition ? exp1 : exp2(如条件正确返回exp1中的逻辑,否则返回exp2)

int a=2;
  int b=3;
  String c = a>b?"a is big":"b is big";
  print(c);

2:exp1 ?? exp2(如果exp1不为空返回exp1.否则返回exp2)

String d = null;
  String e = "hello";
  print(d??e);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值