[英]C# beginner help
Can you help in this example?你能在这个例子中提供帮助吗? I am a beginner.
我是初学者。 I am trying to display name from keyboard in console windows.
我试图在控制台窗口中从键盘显示名称。
class Program
{
public static void Main(string[] args)
{
Console.Write("Your Name?: ");
string name = Console.Read();
Console.WriteLine("Welcome {0}", name);
}
}
Amongst other things, you probably want to change the middle line of the Main method to除其他事项外,您可能希望将 Main 方法的中间行更改为
string name = Console.ReadLine();
The Read
method returns a single character, whereas the ReadLine
method will return the whole line. Read
方法返回单个字符,而ReadLine
方法将返回整行。
Console.ReadLine
instead of Read.Console.ReadLine
而不是 Read。If you can't see anything it could be because of either of these reasons:如果您看不到任何内容,可能是由于以下原因之一:
You may want to insert Console.ReadLine();
您可能想要插入
Console.ReadLine();
as a last statement, otherwise your console will disappear immediately.作为最后的声明,否则您的控制台将立即消失。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace C_sharptesting
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Your name is. Please fill in your name.");
string name = Console.ReadLine();
Console.WriteLine("Your name is " + name + ":)");
Console.ReadLine();
}
}
}
class Program
{
public static void Main(string[] args)
{
Console.Write("Your Name?: ");
string name = Console.ReadLine();
Console.WriteLine($"Welcome {name}");
}
}
You are using the below code您正在使用以下代码
string name = Console.Read();
Try to use Console.ReadLine();尝试使用 Console.ReadLine();
string name = Console.ReadLine();
The difference between Read() and the ReadLine() Method is, that Read()和ReadLine()方法的区别在于,
I hope I could help :)我希望我能帮上忙:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.