我正在尝试创建一个小游戏,但我希望用户输入他的名字,这样游戏就更加真实了!怎么样???? 我试图这样做,如果你输入3(案例3)应用程序刚关闭! 这是代码!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
namespace Game
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Does anyone copy?");
// Thread.Sleep(3000);
Console.WriteLine("Is there anybody listening?");
// Thread.Sleep(3000);
Console.WriteLine("Please i need help!!!");
//Thread.Sleep(3000);
Console.WriteLine("1. Who is this?");
Console.WriteLine("2. Yes i copy!");
Console.WriteLine("3. Ignore the message!");
String choice = Console.ReadLine();
switch (choice)
{
case "1":
//Thread.Sleep(2000);
Console.WriteLine("I'm Emma!");
//Thread.Sleep(6000);
Console.WriteLine("Oh my God i tought nobody was going to answer!");
//Thread.Sleep(2000);
Console.WriteLine("Who are you?");
break;
case "2":
//Thread.Sleep(6000);
Console.WriteLine("Oh my God i tought nobody was going to answer!");
//Thread.Sleep(2000);
Console.WriteLine("I'm Emma by the way!");
//Thread.Sleep(2000);
Console.WriteLine("Who are you?");
break;
case "3":
break;
}
Console.ReadLine();
string yourName;
yourName = Console.ReadLine();
//Thread.Sleep(10000);
Console.WriteLine("Hey {0}... Sorry i got you so worried... i was just scared someone was in my room!" yourName );
//Thread.Sleep(20000);
}
}
}
答案 0 :(得分:0)
在您的代码中,您尝试使用string.Format,正确的语法是:
string yourName;
yourName = Console.ReadLine();
//Thread.Sleep(10000);
Console.WriteLine(String.Format("Hey {0}... Sorry i got you so worried... i was just scared someone was in my room!", yourName));
//Thread.Sleep(20000);
您缺少的重要部分是Console.WriteLine(String.Format(
您还需要使用逗号分隔变量和文本,因为它们是String.Format()
方法中的不同参数。
除此之外,你还可以像这样使用字符串插值
Console.WriteLine($"Hey {yourName}... Sorry i got you so worried... i was just scared someone was in my room!");
这与String.Format
的工作方式类似,但通过在字符串前添加$
来使用它。
正如您所看到的,这样做的好处是,变量名称位于大括号内{yourName}
,而不是放置0.这比写入的代码少String.Format