Lect02类型与语句[V3]答题.pptx

第2讲 类型、表达式、语句;知识回顾:程序的基本结构;功能:标准输入流、输出流和错误流 命名空间: System 常用方法 Read::从标准输入流读取 ReadKey:获取用户按下的下一个字符或功能键 ReadLine:从标准输入流读取下一行字符 Write: 写入标准输出流 WriteLine:写入标准输出流,并换行;基本格式 Console.Write(“格式串”, 对象1,对象2,…) 例 {0,-8} 第一个参数,宽度为8,左对齐 {1,8} 第二个参数,宽度为8,右对齐 {1: D8}输出整数,域宽为7,用0补齐 {0:E4}输出指数,4位小数 {0:F5} 输出浮点数,5位小数 ;int a = double b = 1234.12543; //0描述:占位符,如果可能,填充位 Console.Write({0:000000},a); // 001234 Console.Write({0:000000},b); // 004321 //.描述:小数点 Console.Write({0:000.000},a); //1234.000 Console.Write({0:000.000},b); //4321.125;static?void?Main(string[]?args) ????????{?? double r = 1000.0, theta = 45.0; Console.WriteLine(r ={0:E} ;theta={1:F} , r, theta); theta *= Math.PI / 180; double x = r * Math.Sin(theta); double y = r * Math.Cos(theta); Console.WriteLine(x={0:0.000};y={1:0.000}, x, y); Console.ReadKey(); ????????};习题:已知两点和P2的坐标(自己任意给定),编程计算它们之间距离 ;一、类型 二、运算符与表达式 三、控制语句 《教材》第2章、第3章;类型 类型转换 ;1. 类型 ;double x = 12.3; double y = x; string s1 =“www”; string s2 = Hello; string s3; string s4 =s2; ;预定义类型;double pi= 3.1415; int a=12345; dicimal d=12.34M; ;string类型;string s1 = 小鸟; string s2 = s1 + 飞翔; string s3 = s2.Replace(飞翔, 自由); s1 = string.Format(小鸟飞了{0}公里, 9); s3 = s2.Substring(1, 3);枚举:一组已命名的数字常量 定义方式: [modifiers] enum identifier {enumerator-list};;enum Color { Red=2, Green=4, Blue=8 } static void Chose(string[] args) { Color myc = Color.Green; switch (myc) { case Color.Red: Console.WriteLine(red ={0}, myc); break; case Color.Green: Console.WriteLine(green={0}, myc); break; } } ;结构:几个数据组合在一起而形成的数据结构 定义方式 修饰符 struct 结构名 { //结构体 修饰符 类型 字段; //数据成员 修饰符 类型 方法(){…} //方法 } ;struct Point { public int x, y; public Point(int x, int y) { this.x=x;

您可能关注的文档

文档评论(0)

1亿VIP精品文档

相关文档