-3

I haven't used variables on C# yet so I don't know how I would go about having the next Console.Writeline respond saying, "Hello X, welcome!". Adding the users input to the X.

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

if (answer == string) 
    Console.WriteLine("Okay , welcome!");
}
user247702
  • 23,641
  • 15
  • 110
  • 157
  • 1
    Seems like that would be built into `WriteLine()`. Is the **[documentation](https://learn.microsoft.com/en-us/dotnet/api/system.console.writeline?view=netframework-4.8)** any help? – Ňɏssa Pøngjǣrdenlarp May 27 '19 at 15:40
  • 4
    It's in step 3 of the new "Try .NET" tutorial site, just follow along: https://dotnet.microsoft.com/learn/dotnet/in-browser-tutorial/1 – Peter B May 27 '19 at 15:43

4 Answers4

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

    Console.WriteLine(string.Format( @"Hello {0}, welcome", answer));
}
demo
  • 6,038
  • 19
  • 75
  • 149
Christian L.
  • 290
  • 3
  • 9
  • 1
    I was a TA, and in my experience giving beginners code without any description or explanation is harmful because it teaches them programming is just memorizing snippets, and that they don't actually need to research or learn anything. – Dour High Arch May 27 '19 at 15:51
  • @Dour High Arch I agree, but the case was so simple that I believed not necessary any comment. In the question he also specified that he never used variable, so the first thing, in my honest opinion, was showing him that. If he feel comfortable he can follow learning trying some more complex example. – Christian L. May 27 '19 at 16:08
0

Use interpolated strings $"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

Also you can put the Console.ReadLine() inside the interpolated string so you don't have to create a string variable:

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

You can find out more about interpolated strings here.

Slaven Tojić
  • 2,945
  • 2
  • 14
  • 33
0
    Console.WriteLine("Please type your name?");
    var cName = Console.ReadLine();
    Console.WriteLine($"Hay there { cName }, how are you?");

Screenshot, working example

-3

You can do it in an easy way:

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

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