我的搜索结果为空,或者我只是不明白我找到的结果。我试图提示用户输入一个 boolean 值,但我希望答案是"is"或“否”。如果您需要查看更多代码,请告诉我,但我有这个作为提示。
public static bool getBoolInputValue(string inputType)
{
bool valid = false;
bool value = true;
string inputString = string.Empty;
do
{
inputString = GetInput(inputType);
if (!(String.IsNullOrEmpty(inputString)))
{
valid = bool.TryParse(inputString, out value);
}
if (!valid)
Console.WriteLine("Invalid " + inputType + " try again!");
} while (!valid);
return value;
}
这是我的 boolean 值的参数。也许这需要更具体?
public bool Nitrus
{
set { nitrus = value; }
get { return nitrus; }
}
谢谢你的帮助。我对编程相当陌生,但无法弄清楚这一点。它确实提示成功,但无论我在框中输入什么答案,它都会告诉我这不是正确的格式。
最佳答案
如果我理解正确,您希望用户输入“yes”,这意味着您有一个 True 值。如果这是正确的,请跳过所有 string.IsNullOrEmpty
和 bool.TryParse
内容并执行类似这样的操作。
//make it ToLower so that it will still match if the user inputs "yEs"
inputString = GetInput(inputType);
if (inputString.ToLower() == "yes")
{
value = true;
}
else
{
value = false;
}
//note that the if/else code is the same as directly applying
// the value from the comparison itself:
// value = inputString.ToLower() == "yes";
// this allows the user to type in "yes" or "y":
// value = inputString.ToLower() == "yes" || inputString.ToLower() == "y";
关于C# 提示输入 boolean 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9354152/