我正在尝试制作一个小型电影院节目,根据用户年龄设置if语句,一个年龄为12 A,这意味着我必须询问他们是否有成人陪伴
ms.endOfStream()
前两个选项i 一切顺利。
4: DEMUXER_ERROR_COULD_NOT_OPEN
在这里,无论我输入什么,它都将通过第一个条件并在那里结束,或者如果我在Console.WriteLine(" Enter the number of the film you wish to see :");
int selection = int.Parse(Console.ReadLine());
Console.WriteLine("Enter your age:");
int age = int.Parse(Console.ReadLine());
bool yes = true;
bool no = false;
if语句上写if (selection == 3)
{
if (age >= 12)
{
Console.WriteLine("You may enter");
}
else
{
{
Console.WriteLine("Are you accompanied by an adult? Answer yes or no" );
Console.ReadLine();
if (true)
{
Console.WriteLine("You may pass.");
}
else if (false)
{
Console.WriteLine("You are not allowed.");
...
则它是无法访问的代码。
提前感谢您的帮助。
答案 0 :(得分:2)
而不是评估true或false,它总是给出相同的答案,你需要通过将它存储在变量中来检查用户写回的内容。
if (age >= 12)
{
Console.WriteLine("You may enter");
}
else
{
string response = null;
while(response != "yes" || response != "no"){
response = Console.ReadLine("Are you accompanied by an adult? Answer yes or no" );
}
if (response == "yes")
{
Console.WriteLine("You may pass.");
}
//Only other way to get here is if they answered, "no" so don't need to check response
else{
Console.WriteLine("You are not allowed.");
}
}
答案 1 :(得分:2)
您应该检查true
或false
常数,而不是实际用户输入,例如bool accompanied
:
if (selection == 3) {
if (age >= 12)
Console.WriteLine("You may enter")
else {
Console.WriteLine("Are you accompanied by an adult? Answer yes or no" );
// Trim() - let's be nice and allow user to leave leading/trailing spaces
string input = Console.ReadLine().Trim();
// accompanied if user's input "y" or "yes" (case insensitive)
bool accompanied = "yes".Equals(input, StringComparison.OrdinalIgnoreCase) ||
"y".Equals(input, StringComparison.OrdinalIgnoreCase);
if (accompanied)
Console.WriteLine("You may pass.");
else
Console.WriteLine("You are not allowed.");
}
}
答案 2 :(得分:1)
您忘记了收到成人跟进问题的价值。
试试这个:
private static void Main(string[] args)
{
Console.WriteLine(" Enter the number of the film you wish to see :");
int selection = int.Parse(Console.ReadLine());
Console.WriteLine("Enter your age:");
int age = int.Parse(Console.ReadLine());
if (selection == 3)
{
if (age < 12)
{
Console.WriteLine("Are you accompanied by an adult? Answer yes or no");
string isAccompanied = Console.ReadLine();
if (isAccompanied.ToUpper().Equals("NO"))
{
Console.WriteLine("You are not allowed.");
return;
}
Console.WriteLine("You may pass.");
return;
}
Console.WriteLine("You may enter");
return;
}
}
答案 3 :(得分:0)
如果您想要“是/否”,则应将其存储在字符串变量中,根据此变量评估if
条件。
if (selection == 3)
{
if (age >= 12)
{
Console.WriteLine("You may enter");
}
else
{
{
Console.WriteLine("Are you accompanied by an adult? Answer yes or no" );
string res = Console.ReadLine();
if (res == "yes")
{
Console.WriteLine("You may pass.");
}
else if (res == "no")
{
Console.WriteLine("You are not allowed.");
答案 4 :(得分:0)
您正在阅读输入,但没有对其进行任何操作:
Console.ReadLine();
您正在尝试评估一个永远不会改变的硬编码布尔文字:
if (true)
相反,检查实际输入的是什么。例如:
var userInput = Console.ReadLine();
if (userInput.Equals("yes", StringComparison.InvariantCultureIgnoreCase))
{
Console.WriteLine("You may pass.");
}
else
{
Console.WriteLine("You are not allowed.");
}
答案 5 :(得分:0)
看起来你没有存储或测试“你是否有成年人陪伴?”这个问题的答案。您需要存储readline方法的答案,并测试答案=是或否。
IE:修改文件的顶部以包含一个变量来检查年龄: -
Console.WriteLine(" Enter the number of the film you wish to see :");
int selection = int.Parse(Console.ReadLine());
Console.WriteLine("Enter your age:");
int age = int.Parse(Console.ReadLine());
bool isUserOldEnough = false;
然后将代码修改为: -
if (selection == 3)
{
if (age >= 12)
{
Console.WriteLine("You may enter");
}
else
{
Console.WriteLine("Are you accompanied by an adult? Answer yes or no" );
if (Console.ReadLine().ToLower() == "yes") isUserOldEnough = true;
if (isUserOldEnough == true)
{
Console.WriteLine("You may pass.");
}
else
{
Console.WriteLine("You are not allowed.");
}
}
}