C# 输出
为了在C#中输出内容,我们可以使用
System.Console.WriteLine() OR System.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 on New line Prints on Same line
使用WriteLine()和Write()打印变量和字面量
WriteLine()
和Write()
方法可用于打印变量和字面量。这是一个示例。
示例 3:打印变量和字面量
using System;
namespace Sample
{
class Test
{
public static void Main(string[] args)
{
int value = 10;
// Variable
Console.WriteLine(value);
// Literal
Console.WriteLine(50.05);
}
}
}
当我们运行程序时,输出将是:
10 50.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 World Value = 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 World You 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... x Input 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: 101 You entered 101 Enter double value: 59.412 You entered 59.412
Convert类的ToInt32()
和ToDouble()
方法分别将字符串输入转换为整数和双精度类型。同样,我们可以将输入转换为其他类型。这是Convert类可用方法的完整列表。
还有其他方法可以从用户那里获取数字输入。要了解更多信息,请访问从用户输入中读取整数。