【问题标题】:Cannot activate bools or deactivate them in a while loop C#无法在 while 循环 C# 中激活布尔值或停用它们
【发布时间】:2018-05-22 05:01:34
【问题描述】:

我正在解决 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

【问题讨论】:

  • 是因为你想要continue;,还是else if

标签: c# boolean


【解决方案1】:

问题在于您对 hide 的检查是在 而不是在之后进行扫描的 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;
                     }
          }
     }
 }}

【讨论】:

  • 非常感谢。这很好用。我不知道如何实现这一点(该站点并没有真正解释得很好,而是把它扔给你,但这就是它的乐趣!:D)。但再次非常感谢您花时间向我展示它是如何完成的。简单,我永远不会想到它!再次感谢。
【解决方案2】:


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

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 为真时这个 if 块不会被执行。并将您的 if(answer == 'hide'){} 块移动到扫描检查之后。

最终代码:

使用系统;

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;
                }
            }
     }
 }}

希望这会有所帮助:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-10-25
    • 2011-08-01
    • 1970-01-01
    • 2017-11-20
    • 2017-09-11
    • 2015-10-08
    • 2020-12-03
    • 1970-01-01
    相关资源
    最近更新 更多