Console属于静态类,可直接加载,详见静态成员解释
1、向控制台输出的几种方式
(1)Console.WriteLine(); 什么都不输出,相当于换行(即空一行)
(2)Console.WriteLine("要输出的内容"); 输出后换行
(3)Console.Write("要输出的内容"); 输出后不换行
![](https://img.haomeiwen.com/i16716627/84679177cd2fdda9.png)
2、格式化字符串输出——正确使用占位符
static void Main(string[] args)
{
string stuName="张晓晓";
int stuAge=20;
Console.WriteLine("姓名:"+stuName+"年龄:"+StuAge);
Console.ReadLine();
}
格式化字符串输出: Console.WriteLine("姓名:{0} 年龄{1}:",stuName,stuAge);
——与 Console.WriteLine("姓名:"+stuName+"年龄:"+StuAge); 等效
![](https://img.haomeiwen.com/i16716627/ce3aea2f99ecf1e9.png)
说明:{0}表示占位符,依次使用{0},{1},{2}……与变量列表中的变量对应,如上述语句中,{0}对应stuName,{1}对应stuAge
3、从控制台读入数据
(1)Console.ReadLine(); 读取一行数据并返回字符串类型——从控制台读取的类型默认都是字符串类型
(2)int age=int.Parse(Console.ReadLine()); 将读取的字符串转换为整型
(3)double num=double.Parse(Console.ReadLine()); 将读取的字符串转换为浮点型
说明:对控制台读入数据的数据转换必须保证输入数据的格式是转换目标类型的有效表示格式(不能把"ua20"作为int形式输出)
4、\t制表符的使用
![](https://img.haomeiwen.com/i16716627/cde2c9a6a9065d54.png)
网友评论