C# Console.ReadLine()没有';t将字符串设置为用户输入
我不确定为什么UserInput为null,以及为什么看似简单的用户输入不起作用。我对c#很陌生,所以代码不好,请提前道歉 您的C# Console.ReadLine()没有';t将字符串设置为用户输入,c#,C#,我不确定为什么UserInput为null,以及为什么看似简单的用户输入不起作用。我对c#很陌生,所以代码不好,请提前道歉 您的UserInput不是空的,是打印问题。。。 打印时,可以通过两种方式进行: public static void GetCommand(string room, string[] items) { Console.WriteLine("what would you like to do?"); string UserInput = Console.Read
UserInput
不是空的,是打印问题。。。
打印时,可以通过两种方式进行:
public static void GetCommand(string room, string[] items)
{
Console.WriteLine("what would you like to do?");
string UserInput = Console.ReadLine();
Console.WriteLine("UserInput1: ", UserInput);
UserInput = UserInput.ToLower();
string[] uIn = UserInput.Split();
Console.WriteLine("UserInput2: ",uIn);
if (uIn[0] == "get")
{
get(room, items, uIn);
GetCommand(room,items);
}
if (uIn[0] == "search")
{
search(room, uIn);
}
if (uIn[0]== "north" ^ uIn[0] == "south" ^ uIn[0] == "east" ^ uIn[0] == "west")
{
Console.WriteLine(":::", uIn[0]);
move(room, uIn[0]);
}
if (uIn[0] == "test")
{
test();
}
if (uIn[0] == "clear")
{
Console.Clear();
}
}
请注意,当您给出参数时,使用的是,
您遇到的问题是,您提供了参数,但没有要求打印它(没有使用
{0}
)您的用户输入不为空,这是打印问题。。。
打印时,可以通过两种方式进行:
public static void GetCommand(string room, string[] items)
{
Console.WriteLine("what would you like to do?");
string UserInput = Console.ReadLine();
Console.WriteLine("UserInput1: ", UserInput);
UserInput = UserInput.ToLower();
string[] uIn = UserInput.Split();
Console.WriteLine("UserInput2: ",uIn);
if (uIn[0] == "get")
{
get(room, items, uIn);
GetCommand(room,items);
}
if (uIn[0] == "search")
{
search(room, uIn);
}
if (uIn[0]== "north" ^ uIn[0] == "south" ^ uIn[0] == "east" ^ uIn[0] == "west")
{
Console.WriteLine(":::", uIn[0]);
move(room, uIn[0]);
}
if (uIn[0] == "test")
{
test();
}
if (uIn[0] == "clear")
{
Console.Clear();
}
}
请注意,当您给出参数时,使用的是,
您遇到的问题是,您提供了参数,但没有说要打印它(没有使用{0}
)您可以发布一个简短的、自包含的、正确的(可编译的)示例吗?您没有正确使用控制台。WriteLine
。它应该是Console.WriteLine(“UserInput1:{0}”,UserInput)
The{0}
告诉它在该位置将第一个参数插入格式字符串。或者,您也可以执行Console.WriteLine(“UserInput1:+UserInput”)
甚至Console.WriteLine($“UserInput1:{UserInput}”)代码>使用调试器;它会告诉你UserInput
还可以。另外,最好用|
而不是^
来表示“或”而不是“排他或”,因为它更典型、排他,或者没有真正的用处,因为只有一个比较可能是真的。你能发布一篇简短、自包含、正确(可编译)的文章吗,示例?您没有正确使用控制台。WriteLine
。它应该是Console.WriteLine(“UserInput1:{0}”,UserInput)
The{0}
告诉它在该位置将第一个参数插入格式字符串。或者,您也可以执行Console.WriteLine(“UserInput1:+UserInput”)
甚至Console.WriteLine($“UserInput1:{UserInput}”)代码>使用调试器;它会告诉你UserInput
是可以的。另外,最好用|
而不是^
来表示“或”而不是“排他或”,因为它更典型、排他或实际上没有用,因为只有一个比较可能是真的。
Console.WriteLine("UserInput1 : {0}" , UserInput); //inside the {} u type the position of the parameter {0} is first and {1} is second