简体   繁体   English

C#初学者帮助

[英]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方法将返回整行。

  • Use Main instead of Mian使用 Main 而不是 Mian
  • Use Console.ReadLine instead of Read.使用Console.ReadLine而不是 Read。

If you can't see anything it could be because of either of these reasons:如果您看不到任何内容,可能是由于以下原因之一:

  • (Unlikely) You're not a console app, so there's no console to see. (不太可能)您不是控制台应用程序,因此看不到控制台。 Try Debug.WriteLine instead.尝试 Debug.WriteLine 代替。
  • It's a console app so it's running so fast you don't even get to see what it does.这是一个控制台应用程序,所以它运行得如此之快,你甚至看不到它的作用。

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()方法的区别在于,

  1. with Read() you only read the next Character (char) you type in使用Read()您只能读取您输入的下一个字符 (char)
  2. with ReadLine() you can Read the whole next Line .使用ReadLine()您可以阅读整个下一行

I hope I could help :)我希望我能帮上忙:)

C# Hello World Program C#你好世界程序

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DemoApplication
{
 class Program
 { 
  static void Main(string[] args) 
  {
   Console.Write("Hello World");
   Console.ReadKey();
  }
 }
}

C# Expert C#专家

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM