当前位置: 首页>>代码示例>>C#>>正文


C# Console.ReadLine方法代码示例

本文整理汇总了C#中System.Console.ReadLine方法的典型用法代码示例。如果您正苦于以下问题:C# Console.ReadLine方法的具体用法?C# Console.ReadLine怎么用?C# Console.ReadLine使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Console的用法示例。


在下文中一共展示了Console.ReadLine方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Main

//引入命名空间
using System;
using System.IO;

public class InsertTabs
{
    private const int tabSize = 4;
    private const string usageText = "Usage: INSERTTABS inputfile.txt outputfile.txt";
    public static int Main(string[] args)
    {
        if (args.Length < 2)
        {
            Console.WriteLine(usageText);
            return 1;
        }

        try
        {
            // Attempt to open output file.
            using (var writer = new StreamWriter(args[1]))
            {
                using (var reader = new StreamReader(args[0]))
                {
                    // Redirect standard output from the console to the output file.
                    Console.SetOut(writer);
                    // Redirect standard input from the console to the input file.
                    Console.SetIn(reader);
                    string line;
                    while ((line = Console.ReadLine()) != null)
                    {
                        string newLine = line.Replace(("").PadRight(tabSize, ' '), "\t");
                        Console.WriteLine(newLine);
                    }
                }
            }
        }
        catch(IOException e)
        {
            TextWriter errorWriter = Console.Error;
            errorWriter.WriteLine(e.Message);
            errorWriter.WriteLine(usageText);
            return 1;
        }

        // Recover the standard output stream so that a 
        // completion message can be displayed.
        var standardOutput = new StreamWriter(Console.OpenStandardOutput());
        standardOutput.AutoFlush = true;
        Console.SetOut(standardOutput);
        Console.WriteLine($"INSERTTABS has completed the processing of {args[0]}.");
        return 0;
    }
}
开发者ID:.NET开发者,项目名称:System,代码行数:53,代码来源:Console.ReadLine

示例2: Main

//引入命名空间
using System;

public class Example
{
   public static void Main()
   {
      Console.Clear();

      DateTime dat = DateTime.Now;

      Console.WriteLine("\nToday is {0:d} at {0:T}.", dat);
      Console.Write("\nPress any key to continue... ");
      Console.ReadLine();
   }
}
开发者ID:.NET开发者,项目名称:System,代码行数:16,代码来源:Console.ReadLine

输出:

Today is 10/26/2015 at 12:22:22 PM.

Press any key to continue...

示例3: Main

//引入命名空间
using System;

public class Example
{
   public static void Main()
   {
      if (! Console.IsInputRedirected) {
         Console.WriteLine("This example requires that input be redirected from a file.");
         return; 
      }

      Console.WriteLine("About to call Console.ReadLine in a loop.");
      Console.WriteLine("----");
      String s;
      int ctr = 0;
      do {
         ctr++;
         s = Console.ReadLine();
         Console.WriteLine("Line {0}: {1}", ctr, s);
      } while (s != null);
      Console.WriteLine("---");
   }
}
开发者ID:.NET开发者,项目名称:System,代码行数:24,代码来源:Console.ReadLine

输出:

About to call Console.ReadLine in a loop.
----
Line 1: This is the first line.
Line 2: This is the second line.
Line 3: This is the third line.
Line 4: This is the fourth line.
Line 5:
---

示例4: Main

//引入命名空间
using System;

public class Example
{
   public static void Main()
   {
      string line;
      Console.WriteLine("Enter one or more lines of text (press CTRL+Z to exit):");
      Console.WriteLine();
      do { 
         Console.Write("   ");
         line = Console.ReadLine();
         if (line != null) 
            Console.WriteLine("      " + line);
      } while (line != null);   
   }
}
// The following displays possible output from this example:
//       Enter one or more lines of text (press CTRL+Z to exit):
//       
//          This is line #1.
//             This is line #1.
//          This is line #2
//             This is line #2
//          ^Z
//       
//       >
开发者ID:.NET开发者,项目名称:System,代码行数:28,代码来源:Console.ReadLine

示例5: Console.ReadLine()

//引入命名空间
using System;
using System.IO;

class MainClass
{
  public static void Main(string[] args)
  {
    string ans;
    do
    {
      Console.Write("Are you done? [yes] [no] : ");
      ans = Console.ReadLine();
    }while(ans != "yes");
  }
}
开发者ID:C#程序员,项目名称:System,代码行数:16,代码来源:Console.ReadLine


注:本文中的System.Console.ReadLine方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。