using System;
namespace ConsoleApp3
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hi! What is your name?");
string name = Console.ReadLine();
Console.WriteLine("Hi {0} how are you? <Please write 'Good' or 'Bad'>", name);
string howAre = Console.ReadLine();
if (howAre == "Good")
{
Console.WriteLine("Excellent!");
Console.WriteLine("<Press Any Key To Continue>");
Console.ReadKey();
}
else if (howAre == "Bad")
{
Console.WriteLine("Don't {0} worry everyone always had a bad day :) " + name);
Console.WriteLine("<Press Any Key To Continue>");
Console.ReadKey();
}
else
{
Console.WriteLine("<Please write 'Good' or 'Bad'> ");
Console.WriteLine("<Press Any Key To Return>");
Console.ReadKey();
return;
}
}
}
}
答案 0 :(得分:3)
您需要更改行
Console.WriteLine("Don't {0} worry everyone always had a bad day :) " + name)
要
Console.WriteLine("Don't {0} worry everyone always had a bad day :) ", name)
答案 1 :(得分:3)
在C#6之后,您可以使用字符串插值,这更具可读性和经济性:
Console.WriteLine($"Don't worry {name} everyone always had a bad day :)");
答案 2 :(得分:2)
您需要更改此行:
Console.WriteLine("Don't {0} worry everyone always had a bad day :) " + name);
To This:
Console.WriteLine("Don't {0} worry everyone always had a bad day :) ", name);
<强>为什么吗
因为您使用+
符号并且只打印string name
后面的Console.writeline
。
如果您将其更改为, name
,则会在string name
{0}
答案 3 :(得分:1)
在没有{0}的情况下,只需添加一些输入就可以使用字符串插值:https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/tokens/interpolated
示例:
Console.WriteLine($"Don't {name} worry everyone always had a bad day");