无法激活bool或在while循环中停用它们C#

时间:2018-05-22 05:01:35

标签: c# boolean

我正在解决Codeeasy.net上的一个问题而我无法终身解决问题。如果有人可以帮助我。这就是应该做的。

class AntivirusScan
{
    static void Main(string[] args)         
    {
        int fileIndex     = 0;
        bool hideMode     = false;
        bool heDoesntKnow = true;

        while (heDoesntKnow)
        {
            string answer = Console.ReadLine();

            if (answer == "scan")
            {
                fileIndex = fileIndex + 1;
                Console.WriteLine($"scanning file {fileIndex}");
            }

            if (hideMode == true)
                 Console.WriteLine($"can't scan files for viruses");
            if (answer == "hide")
            {
                Console.WriteLine($"can't scan files for viruses");
                hideMode = true;
            }
            if (answer == "unhide")
            {
                fileIndex=fileIndex+1;
                Console.WriteLine($"scanning file {fileIndex}");
                hideMode=false;
            }

            if (answer == "game over")
            {
                Console.WriteLine($"run");
                heDoesntKnow = false;
             }
         }
     }
 }

这是我的代码。如果它被隐藏,它应该返回它不能扫描。相反,它只是通过它。它出现一次,然后继续不做它应该做的事情。

应该从提示符

中读取
scan
scan
hide
scan
scan
unhide
scan
game over

这是它输出的内容

scanning file 1
scanning file 2
can't scan files for viruses

scanning file 3
can't scan files for viruses

scanning file 4
can't scan files for viruses
can't scan files for viruses

scanning file 5
scanning file 6
run

这是它应该输出的内容

scanning file 1
scanning file 2
can't scan files for viruses
can't scan files for viruses
scanning file 3
run

2 个答案:

答案 0 :(得分:0)


根据您的预期输出,我猜输入是

  

1)扫描
  2)扫描
  3)隐藏
    4)扫描
    5)取消隐藏
    6)游戏结束

因此,根据您的代码,我认为您必须在第一个if条件中添加一个约束:

 if (answer == "scan")
 {
   fileIndex=fileIndex+1;
   Console.WriteLine($"scanning file {fileIndex}");
  }

这应该改为

if (answer == "scan" && !hideMode)
 {
   fileIndex=fileIndex+1;
   Console.WriteLine($"scanning file {fileIndex}");
 }

这样当hideMode为true时,if块不会被执行。并将您的if(answer ==' hide'){}块移动到扫描检查后。

最终代码:

使用System;

namespace WhileLoop
{
    class AntivirusScan
    {
         static void Main(string[] args)
        {
int fileIndex = 0;
bool hideMode = false;
bool heDoesntKnow = true;

          while (heDoesntKnow)
            {
                string answer = Console.ReadLine();
                if (answer == "scan" && !hideMode)
                {
                    fileIndex = fileIndex + 1;
                    Console.WriteLine($"scanning file {fileIndex}");
                }

                if (answer == "unhide")
                {
                    fileIndex = fileIndex + 1;
                    Console.WriteLine($"scanning file {fileIndex}");
                    hideMode = false;
                }

                if (hideMode == true)
                {
                    Console.WriteLine($"can't scan files for viruses");
                }
                if (answer == "hide")
                {
                    Console.WriteLine($"can't scan files for viruses");
                    hideMode = true;
                }


                if (answer == "game over")
                {
                    Console.WriteLine("run");
                    heDoesntKnow = false;
                }
            }
     }
 }}

希望这会有所帮助:)

答案 1 :(得分:0)

问题在于,检查隐藏是在之后,而不是在之内,if语句用于扫描。我还使你的代码更具可读性并简化了几个陈述。

using System;

namespace WhileLoop
{
    class AntivirusScan
    {
         static void Main(string[] args)
         {
           int fileIndex = 0;
           bool hideMode = false;
           bool heDoesntKnow = true;

           while (heDoesntKnow)
            {
                     string answer = Console.ReadLine();
                     if (answer == "scan") {
                        if (hideMode) {
                            Console.WriteLine($"can't scan files for viruses");
                         } else {
                            fileIndex++;
                            Console.WriteLine($"scanning file {fileIndex}");
                         }
                     }

                     if(answer == "hide")
                     {
                        Console.WriteLine($"can't scan files for viruses");
                        hideMode=true;
                     }

                     if (answer == "unhide")
                     {
                        fileIndex=fileIndex+1;
                        Console.WriteLine($"scanning file {fileIndex}");
                        hideMode=false;
                     }

                     if (answer == "game over")
                     {
                        Console.WriteLine($"run");
                        heDoesntKnow = false;
                     }
          }
     }
 }}