如何在程序中设置适当的布尔条件

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在程序中设置适当的布尔条件相关的知识,希望对你有一定的参考价值。

我正在尝试制作一个小型电影院节目,根据用户年龄设置if语句,一个年龄为12 A,这意味着我必须询问他们是否有成人陪同

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 (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.");
        ...

在这里,无论我输入什么,它都将通过第一个条件并在那里结束,或者如果我在Console.ReadLine(); if语句上写else则它是无法访问的代码。

在此先感谢您的帮助。

答案

而不是评估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.");
  }
}
另一答案

你应该检查不是truefalse常数,而是实际的用户输入,比如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.");
  }
}
另一答案

您忘记了收到成人后续问题的价值。

试试这个:

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;
            }
        }
另一答案

如果你想要“是/否”,你应该将它存储在一个字符串变量中,根据这个变量评估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.");
另一答案

你正在阅读输入,但没有做任何事情:

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.");
}
另一答案

您似乎没有存储或测试“您是否由成人陪伴?”这一问题的答案。您需要存储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.");
              }
         }
     }

以上是关于如何在程序中设置适当的布尔条件的主要内容,如果未能解决你的问题,请参考以下文章

如何在android的片段中设置应用程序上下文?

有条件地在 JSF 组件标签中设置一个属性

如何在 typeorm 和 nest.js 中设置布尔验证

如何在选项卡式活动中设置片段从右到左开始

如何在 Xamarin Forms 中以适当的图像比例在 UWP 中设置 SpashScreen?

在 Android Kotlin 的约束布局中设置适当的约束