我正在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发布于 2018-05-22 05:33:23
问题是,您的隐藏检查是在,而不是在中,这是用于扫描的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;
}
}
}
}}发布于 2018-05-22 05:26:53
根据您的预期输出,我猜输入是
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(应答== '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;
}
}
}
}}希望这会有所帮助:)
https://stackoverflow.com/questions/50460103
复制相似问题