集合了一些关於Console的资料以及自己练习范例在以下
C#中的console表示控制台。 Console 是一个类别 主要用於控制台应用程序的输入和输岀操作。
class Program
{
static void Main(string[] args)
{
/*输出讯息(把数据输出到控制台并显示出来)*/
Console.Write(""); //表示向控制台直接写入字串,不进行换行,可继续接着前面的字写入。
Console.WriteLine(); //表示向控制台写入字串後 换行。
/*输入讯息(取得用户输入的资料)*/
Console.Read(); //表示从控制台读取字串,不换行。
Console.ReadLine(); //表示从控制台读取字串後进行换行。
Console.ReadKey(); //取得键盘按下的下一个字符或功能键,按下的键显示在控制台画面中。
/*其他功能*/
Console.Beep(); //通过控制台播放提示音。
Console.Clear(); //清除控制台缓冲区和相应的控制台画面的显示讯息。
}
}
范例
//Console.Write(格式化字串, 输出项, 输出项2);
//索引从0开始
class Program
{
static void Main(string[] args)
{
//请实作 输入姓名和学校,并在输出时组成一句话“xx 同学在xx 上课”
Console.WriteLine("请输入学生姓名:");
string name = Console.ReadLine();
Console.WriteLine("请输入所在学校:");
string school = Console.ReadLine();
Console.WriteLine($"{name}同学在{school}上课");
//也可写作Console.WriteLine("{0}同学在{1}上课", name, school);
}
}
显示结果如下:
常见格式
一些输出范例
Console.WriteLine("{0, 8 :C}", 2); // $2.00
Console.WriteLine("{0, 8 :C3}", 2); // $2.000
Console.WriteLine("{0 :D3}", 2); // 002
Console.WriteLine("{0 :E}", 2); // 2.000000E+000
Console.WriteLine("{0 :G}", 2); // 2
Console.WriteLine("{0 :N}", 2500000.00); // 2,500,00.00
Console.WriteLine("{0 :x4}", 12); // 000c
Console.WriteLine("{0, 2 :x}", 12); // c
Console.WriteLine("{0 :000.000}", 12.23); // 012.230
Console.WriteLine("{0 :r}", 15.62);// 15.62
Console.WriteLine("{0 :d}", System.DateTime.Now); // 2022-3-17
Console.WriteLine("{0 :D}", System.DateTime.Now); // 2022年3月17日
Console.WriteLine("{0 :t}", System.DateTime.Now); // 11:43
Console.WriteLine("{0 :T}", System.DateTime.Now); // 11:43:34
Console.WriteLine("{0 :f}", System.DateTime.Now); // 2022年3月27日11:43
Console.WriteLine("{0 :F}", System.DateTime.Now); // 2022年3月27日11:43:34
Console.WriteLine("{0 :g}", System.DateTime.Now); // 2022-3-17 11:43
Console.WriteLine("{0 :G}", System.DateTime.Now); // 2022-3-17 11:43:34
Console.WriteLine("{0 :M}", System.DateTime.Now); // 3月17日
Console.WriteLine("{0 :r}", System.DateTime.Now);// Thu, 17 Mar 2022 11:43:34 GMT
Console.WriteLine("{0 :s}", System.DateTime.Now); // 2022-03-17T11:43:34
Console.WriteLine("{0 :u}", System.DateTime.Now); // 2022-03-17 11:43:34Z
Console.WriteLine("{0 :U}", System.DateTime.Now); // 2022年3月17日3:43:34
Console.WriteLine("{0 :Y}", System.DateTime.Now); // 2022年3月
Console.WriteLine("{0 :dd}", System.DateTime.Now); // 27
Console.WriteLine("{0 :ddd}", System.DateTime.Now); // 四
Console.WriteLine("{0 :dddd}", System.DateTime.Now); // 星期四
Console.WriteLine("{0 :f}", System.DateTime.Now); // 2022年3月17日11:46
Console.WriteLine("{0 :ff}", System.DateTime.Now); // 18
Console.WriteLine("{0 :fff}", System.DateTime.Now); // 187
Console.WriteLine("{0 :ffff}", System.DateTime.Now); // 1875
Console.WriteLine("{0 :fffff}", System.DateTime.Now); // 18750
Console.WriteLine("{0 :gg}", System.DateTime.Now); // 公元
Console.WriteLine("{0 :ggg}", System.DateTime.Now); // 公元
Console.WriteLine("{0 :gggg}", System.DateTime.Now); // 公元
Console.WriteLine("{0 :ggggg}", System.DateTime.Now); // 公元
Console.WriteLine("{0 :gggggg}", System.DateTime.Now);// 公元
Console.WriteLine("{0 :hh}", System.DateTime.Now); // 11
Console.WriteLine("{0 :HH}", System.DateTime.Now); // 11
Console.WriteLine("{0 :mm}", System.DateTime.Now); // 50
Console.WriteLine("{0 :MM}", System.DateTime.Now); // 03
Console.WriteLine("{0 :MMM}", System.DateTime.Now); // 三月
Console.WriteLine("{0 :MMMM}", System.DateTime.Now); // 三月
Console.WriteLine("{0 :ss}", System.DateTime.Now); // 43
Console.WriteLine("{0 :tt}", System.DateTime.Now); // 上午
Console.WriteLine("{0 :yy}", System.DateTime.Now); // 12
Console.WriteLine("{0 :yyyy}", System.DateTime.Now); // 2022
Console.WriteLine("{0 :zz}", System.DateTime.Now); // +08
Console.WriteLine("{0 :zzz}", System.DateTime.Now); // +08:00
Console.WriteLine("{0 :hh:mm:ss}", System.DateTime.Now); // 11:43:34
Console.WriteLine("{0 :dd/MM/yyyy}", System.DateTime.Now); // 17-03-2022
注意:
Console.ReadLine()和Console.Read()的输入结果完全不同,不能混用。
Console.Read(), 返回值为首字的ASCII码
Console.ReadLine(), 返回值为字串。
也就是说read方法只能读取第一个字符,而ReadLine能读多个字符也可以换行读取
Console.ReadKey()的作用:
using System;
using System.Collections.Generic;
using System.Linq;using System.Text;
using System.Threading.Tasks;namespace ConsoleTest
{ class Program
{ static void Main(string[] args)
{
Console.WriteLine("输入用户姓名和ID");
string name = Console.ReadLine();
int id = int.Parse(Console.ReadLine());
Console.WriteLine("User Name is {0} \nThe id is {1}",name, id);
Console.ReadKey();
}
}
read是从控制台读取,key表示按下键盘,那麽组合在一起的意思就是取得用户按下功能键显示在视窗中,用在前面的语法达到视窗暂停的功能,在运行程序状态下,只有按下任意键後视窗才会关闭。
参考来源:
C#中的console是什麽意思
百度知道 搜寻
C# Console.WriteLine()函数中{}输出格式详解
>>: Visual studio 2019 使用AddressSanitizer检查程序码
大家好,我是长风青云。今天是铁人赛的第八天。 阵列我原本以为应该挺好讲的,可是我却发现我讲的嗑嗑碰碰...
今天看了彭彭老师的影片,他使用了程序码做举例,这篇我会使用他举例的程序,再另外加上自己笔记让印象加深...
今天的介绍的也是属於Node的基础。 process.nextTick 依照Node官网的解说,事实...
提到 Web 前端的效能优化,有许多的技巧是聚焦在如何减少页面的「载入时间 Loading Tim...
轻松快速道路 今日会使用CDK建立一样的架构,如下图: 在CDK可能有很多种不同的建立方式,这边会介...