C# 我的";{0}";占位符不是';不起作用。有没有办法解决它们 使用系统; 名称空间控制台AP3 { 班级计划 { 静态void Main(字符串[]参数) { 控制台。WriteLine(“嗨!你叫什么名字?”); 字符串名称=Console.ReadLine(); WriteLine(“嗨{0}你好吗?”,name); 字符串howAre=Console.ReadLine(); 如果(howAre==“好”) { 控制台。WriteLine(“太棒了!”); 控制台。写线(“”); Console.ReadKey(); } 否则如果(howAre==“坏”) { WriteLine(“不要{0}担心每个人都会有糟糕的一天:)”+name); 控制台。写线(“”); Console.ReadKey(); } 其他的 { 控制台。写线(“”); 控制台。写线(“”); Console.ReadKey(); 返回; } } } }

C# 我的";{0}";占位符不是';不起作用。有没有办法解决它们 使用系统; 名称空间控制台AP3 { 班级计划 { 静态void Main(字符串[]参数) { 控制台。WriteLine(“嗨!你叫什么名字?”); 字符串名称=Console.ReadLine(); WriteLine(“嗨{0}你好吗?”,name); 字符串howAre=Console.ReadLine(); 如果(howAre==“好”) { 控制台。WriteLine(“太棒了!”); 控制台。写线(“”); Console.ReadKey(); } 否则如果(howAre==“坏”) { WriteLine(“不要{0}担心每个人都会有糟糕的一天:)”+name); 控制台。写线(“”); Console.ReadKey(); } 其他的 { 控制台。写线(“”); 控制台。写线(“”); Console.ReadKey(); 返回; } } } },c#,C#,您需要更改行 using System; namespace ConsoleApp3 { class Program { static void Main(string[] args) { Console.WriteLine("Hi! What is your name?"); string name = Console.ReadLine(); Console.WriteLi

您需要更改行

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;
            }
        }     
    }
}


您需要更改此行:

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);
为什么?

因为您使用的是
+
符号,它只在
控制台后面打印
字符串名

如果您将其更改为
,name
,它将在
{0}

的位置打印出您的
字符串名称,在C#6以后的版本中,您可以使用字符串插值,它更具可读性和经济性:

Console.WriteLine("Don't {0} worry everyone always had a bad day :) ", name);

只需以另一种方式添加一些输入,而无需{0}即可使用字符串插值:

例如:

Console.WriteLine($"Don't worry {name} everyone always had a bad day :)");

“不工作”不是一个有用的错误描述!到底发生了什么,你期望的是什么?在最后一个
else
块中,您没有提供任何应替换
{0}
的参数。您可能是指
Console.WriteLine(“不要{0}担心,每个人都会有糟糕的一天:)”,name),而不是
+名称)
。在
中有一个
+
而不是
,否则如果(howAre==“Bad”)
发布好问题的一部分是将代码缩减到显示失败的尽可能小的部分,因为这样一来,查看代码的人就不会有太大的混淆空间。(作为奖励,它通常会帮助你自己识别问题,这永远是一个胜利。)第一个确实有效。一些额外的输入:我很高兴你提到了美元符号。我怎么从来没有听说过这一点,直到现在?这改变了一切!
Console.WriteLine($"Don't worry {name} everyone always had a bad day :)");
Console.WriteLine($"Don't {name} worry everyone always had a bad day");