原文: https://www.programiz.com/csharp-programming/basic-input-output
在本教程中,我们将学习如何使用各种方法从用户那里获取输入并在 C# 中显示输出
C# 输出
为了在 C# 中输出内容,我们可以使用
System.Console.WriteLine() ORSystem.Console.Write()
此处,System是命名空间,Console是命名空间System中的类,WriteLine和Write是类Console的方法。
让我们看一个简单的示例,该示例将字符串输出到输出屏幕。
示例 1:使用WriteLine()打印字符串
using System;namespace Sample{class Test{public static void Main(string[] args){Console.WriteLine("C# is cool");}}}
当我们运行程序时,输出将是
C# is cool
WriteLine()和Write()方法之间的区别
WriteLine()和Write()之间的主要区别在于Write()方法仅打印提供给它的字符串,而WriteLine()方法则打印字符串并移至下一行的开头。
让我们看下面的示例,以了解这些方法之间的区别。
示例 2:如何使用WriteLine()和Write()方法?
using System;namespace Sample{class Test{public static void Main(string[] args){Console.WriteLine("Prints on ");Console.WriteLine("New line");Console.Write("Prints on ");Console.Write("Same line");}}}
当我们运行程序时,输出将是:
Prints onNew linePrints on Same line
使用WriteLine()和Write()打印变量和字面值
WriteLine()和Write()方法可用于打印变量和字面值。 这是一个例子。
示例 3:打印变量和字面值
using System;namespace Sample{class Test{public static void Main(string[] args){int value = 10;// VariableConsole.WriteLine(value);// LiteralConsole.WriteLine(50.05);}}}
当我们运行程序时,输出将是:
1050.05
使用+运算符组合(连接)两个字符串并打印它们
在打印时,可以使用+操作符对字符串进行合并/连接。
示例 4:打印使用+运算符连接的字符串
using System;namespace Sample{class Test{public static void Main(string[] args){int val = 55;Console.WriteLine("Hello " + "World");Console.WriteLine("Value = " + val);}}}
当我们运行程序时,输出将是:
Hello WorldValue = 55
使用字符串格式化打印连接的字符串【更好的选择】
打印连接字符串的更好的选择是使用格式化字符串。 格式化字符串允许程序员使用占位符作为变量。 例如,
下一行,
Console.WriteLine("Value = " + val);
可以替换为
Console.WriteLine("Value = {0}", val);
{0}是变量val的占位符,它将由val的值替换。 由于仅使用一个变量,因此只有一个占位符。
格式化的字符串中可以使用多个变量。 我们将在下面的示例中看到这一点。
示例 5:使用字符串格式化打印连接的字符串
using System;namespace Sample{class Test{public static void Main(string[] args){int firstNumber = 5, secondNumber = 10, result;result = firstNumber + secondNumber;Console.WriteLine("{0} + {1} = {2}", firstNumber, secondNumber, result);}}}
当我们运行程序时,输出将是:
5 + 10 = 15
在此,将{0}替换为firstNumber,将{1}替换为secondNumber,将{2}替换为result。 与使用+运算符相比,这种打印输出的方法更具可读性,并且不易出错。
要了解有关字符串格式的更多信息,请访问 C# 字符串格式。
C# 输入
在 C# 中,从用户获取输入的最简单方法是使用Console类的ReadLine()方法。 但是,Read()和ReadKey()也可用于从用户那里获取输入。 它们也包含在Console类中。
示例 6:从用户获取字符串输入
using System;namespace Sample{class Test{public static void Main(string[] args){string testString;Console.Write("Enter a string - ");testString = Console.ReadLine();Console.WriteLine("You entered '{0}'", testString);}}}
当我们运行程序时,输出将是:
Enter a string - Hello WorldYou entered 'Hello World'
ReadLine(),Read()和ReadKey()方法之间的区别:
ReadLine(),Read()和ReadKey()方法之间的区别是:
ReadLine():ReadLine()方法从标准输入流读取下一行输入。 它返回相同的字符串。Read():Read()方法从标准输入流中读取下一个字符。 它返回字符的 ascii 值。ReadKey():ReadKey()方法获取用户按下的下一个键。 此方法通常用于按住屏幕,直到用户按下某个键为止。
如果您想了解更多有关这些方法的信息,请参见以下关于 StackOverflow 的有趣讨论: Console.Read()和Console.ReadLine()之间的区别? 。
示例 7:Read()和ReadKey()方法之间的区别
using System;namespace Sample{class Test{public static void Main(string[] args){int userInput;Console.WriteLine("Press any key to continue...");Console.ReadKey();Console.WriteLine();Console.Write("Input using Read() - ");userInput = Console.Read();Console.WriteLine("Ascii Value = {0}",userInput);}}}
当我们运行程序时,输出将是:
Press any key to continue...xInput using Read() - Learning C#Ascii Value = 76
从此示例中,必须清楚ReadKey()和Read()方法的工作方式。 在使用ReadKey()时,一旦按下该键,它就会显示在屏幕上。
使用Read()时,它占一行,但仅返回第一个字符的 ASCII 值。 因此,将打印76(L的 ASCII 值)。
读取数值(整数和浮点类型)
在 C# 中,读取字符或字符串非常简单。 您需要做的就是根据需要调用相应的方法。
但是,在 C# 中读取数字值可能会有些棘手。 我们仍将使用与获取字符串值相同的ReadLine()方法。 但是由于ReadLine()方法将输入作为字符串接收,因此需要将其转换为整数或浮点类型。
转换输入的一种简单方法是使用Convert类的方法。
示例 8:使用Convert类从用户读取数值
using System;namespace UserInput{class MyClass{public static void Main(string[] args){string userInput;int intVal;double doubleVal;Console.Write("Enter integer value: ");userInput = Console.ReadLine();/* Converts to integer type */intVal = Convert.ToInt32(userInput);Console.WriteLine("You entered {0}",intVal);Console.Write("Enter double value: ");userInput = Console.ReadLine();/* Converts to double type */doubleVal = Convert.ToDouble(userInput);Console.WriteLine("You entered {0}",doubleVal);}}}
当我们运行程序时,输出将是:
Enter integer value: 101You entered 101Enter double value: 59.412You entered 59.412
Convert类的ToInt32()和ToDouble()方法分别将输入的字符串转换为整数和双精度类型。 同样,我们可以将输入转换为其他类型。 这是Convert类的可用方法的完整列表.aspx)。
还有其他方法可以从用户那里获取数字输入。 要了解更多信息,请访问从用户输入读取整数。
