本文整理汇总了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;
}
}
示例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();
}
}
输出:
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("---");
}
}
输出:
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
//
// >
示例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");
}
}