c# 控制台输入输出

1.System.Console.Write()方法不会在显示文本之后添加一个换行符而是将当前位置保持在同一行上。而System.Console.WriteLine()方法会换行。
2.System.Console.ReadLine()方法将暂停程序执行,以便用户输入字符。一旦用户按回车键创建新的一行。程序就会继续执行。

 

  string firstName, lastName;
            System.Console.WriteLine("Hey You!");
            System.Console.Write("Enter your first name:");
            firstName = System.Console.ReadLine();
            System.Console.Write("Enter your last name:");
            lastName = System.Console.ReadLine();
            System.Console.WriteLine("Your full name is {0} {1}.",
                firstName,lastName
                );


3.System.Console.Read()方法返回的数据类型是与读取的字符值对应的一个整数,如果没有更多的字符可用,就返回-1。为了获取实际的字符,需要首先将整数转型为一个字符。

  int readValue;
            char character;
            readValue = System.Console.Read();
            character = (char)readValue;
            System.Console.WriteLine(character);
            System.Console.ReadLine();


4。System. Console.ReadKey()方法返回的是用户的一次按键之后的输入,它使开发人员拦截用户的按键操作,并执行相应的行到,比如限制只能按回车键等
5. System.Console.WriteLine("Your full name is {0} {1}.", firstName,lastName);{0}为交换索引占位符,firstName为对应的变量。

 

 

 if (System.Console.ReadKey(true).Key == ConsoleKey.Enter)
            {
                System.Console.ReadKey(true);
            }
            else
            {
                System.Console.WriteLine("输入错误!");
            }
posted on 2010-06-21 23:15  二地主  阅读(474)  评论(0编辑  收藏  举报