响应名称为

时间:2019-05-27 15:38:05

标签: c#

我还没有在C#上使用变量,所以我不知道如何使用下一个Console。Writeline回答说:“ Hello X,欢迎光临!”。将用户输入添加到X。

Console.WriteLine("What is your name?");
String answer = Console.ReadLine(); 

if (answer == string) 
    Console.WriteLine("Okay , welcome!");
}

4 个答案:

答案 0 :(得分:1)

public static void Main()
{
    Console.WriteLine("Do you want to play?"); 
    String answer = Console.ReadLine();

    Console.WriteLine(string.Format( @"Hello {0}, welcome", answer));
}

答案 1 :(得分:0)

使用插值字符串$"Hello {x} , welcome!"

Console.WriteLine("What is your name?");
string x = Console.ReadLine();// reading the name from the console
Console.WriteLine($"Hello {x} , welcome!");//displaying the name in the console window

此外,您可以将Console.ReadLine()放在插值字符串中,这样就不必创建字符串变量:

Console.WriteLine("What is your name?");
Console.WriteLine($"Hello {Console.ReadLine()} , welcome!");

您可以找到有关插值字符串here的更多信息。

答案 2 :(得分:0)

    Console.WriteLine("Please type your name?");
    var cName = Console.ReadLine();
    Console.WriteLine($"Hay there { cName }, how are you?");

Screenshot, working example

答案 3 :(得分:-3)

您可以轻松地做到这一点:

Console.WriteLine("What is your name?");
string name = Console.ReadLine(); 

if (answer == string) 
    Console.WriteLine($"Okay , welcome {name}!");