出于某种原因,无论我输入哪种食物,我总是得到相同的输出(我得到的是熊的输出)。只有当我写披萨时,我才会得到一个懒惰动物的输出。
有人可以看看这个并告诉我我哪里错了吗?即使我选择了一些.IndexOf 没有找到的随机食物,我仍然会得到熊动物!
好吧,让我们试试看会发生什么:
用户输入“汉堡”
我将用直接值替换方法调用
string userInput = "Hamburger"; //Imagine they type Hamburger
int userInput1 = 0; //Hamburger found at 0 - userInput.IndexOf("Hamburger", 0);
int userInput2 = -1; //Pizza not found - userInput.IndexOf("Pizza", 0);
int userInput3 = -1; //Tuna salad not found - userInput.IndexOf("Tuna salad", 0);
bool hamUserInput1 = false; //0 converted to boolean is False - Convert.ToBoolean(userInput1);
bool pizzaUserInput2 = true //-1 converted to boolean is True -Convert.ToBoolean(userInput2);
bool tunaUserInput3 = true //-1 converted to boolean is True -Convert.ToBoolean(userInput3);
if (false /*hamUserInput1*/ == true) //NOT DONE, false is not equal to true
{
Console.WriteLine($"Since you chose {userInput} as your food, youre a bear!");
}
else if (true /*pizzaUserInput2*/ == true) //DONE
{
Console.WriteLine($"Since you chose {userInput} as your food, youre a sloth!");
}
我们不需要更进一步;我们发现当我们输入 Hamburger 时,你就是个树懒!
用户输入“披萨”
第 2 轮,我们开始!
string userInput = "Pizza"; //Imagine they type Pizza
int userInput1 = -1; //Hamburger not found - userInput.IndexOf("Hamburger", 0);
int userInput2 = 0; //Pizza found at 0 - userInput.IndexOf("Pizza", 0);
int userInput3 = -1; //Tuna salad not found - userInput.IndexOf("Tuna salad", 0);
bool hamUserInput1 = true; //-1 converted to boolean is True - Convert.ToBoolean(userInput1);
bool pizzaUserInput2 = true //0 converted to boolean is False -Convert.ToBoolean(userInput2);
bool tunaUserInput3 = true //-1 converted to boolean is True -Convert.ToBoolean(userInput3);
if (true /*hamUserInput1*/ == true) //DONE
{
Console.WriteLine($"Since you chose {userInput} as your food, youre a bear!");
}
我们不需要更进一步;我们发现当我们输入 Pizza 时,你就是一只熊!
用户类型“ASDF”
第三轮,我们开始!
string userInput = "ASDF"; //Imagine they type ASDF
int userInput1 = -1; //Hamburger not found - userInput.IndexOf("Hamburger", 0);
int userInput2 = -1; //Pizza not found - userInput.IndexOf("Pizza", 0);
int userInput3 = -1; //Tuna salad not found - userInput.IndexOf("Tuna salad", 0);
bool hamUserInput1 = true; //-1 converted to boolean is True - Convert.ToBoolean(userInput1);
bool pizzaUserInput2 = true //-1 converted to boolean is True-Convert.ToBoolean(userInput2);
bool tunaUserInput3 = true //-1 converted to boolean is True -Convert.ToBoolean(userInput3);
if (true /*hamUserInput1*/ == true) //DONE
{
Console.WriteLine($"Since you chose {userInput} as your food, youre a bear!");
}
我们不需要更进一步;我们发现当我们输入 ASDF 时,你就是一只熊!
这是您使用 VS 调试器进行的调试。你点击一行,你按 F9,你按 Play..
..然后代码在红点所在的那一行停止,您可以按 F10 或 F11 来推进它(分别跳过或进入方法 - 跳过仍然运行方法它只是没有'不要踏入它(如果你一直踏入它可能会非常乏味))
您可以指向代码中的任何范围内变量并随时查看它们的值
你会看到这个过程;输入一些东西,观察值
直截了当,IndexOf 的Convert.ToBoolean 是一个坏主意。
- 如果 IndexOf 返回 -1 表示“未找到”,则 ToBoolean 会将其设为
true,就好像它“已找到”一样
- 如果 IndexOf 返回 0 表示“在开始时找到”,Convert 会将此设为“未找到”
- 如果 IndexOf 返回 1+ 表示“在中间某处找到”,Convert 会将此设为“已找到”
所以它有时有效,但有时无效 - 这是可怕的、不可靠的代码。扔掉它
看看这多好:
string userInput = Console.ReadLine();
if (userInput.Contains("Hamburger")){
Console.WriteLine($"Since you chose Hamburger as your food, you're a bear!");
} else if (userInput.Contains("Pizza")){{
Console.WriteLine($"Since you chose Pizza as your food, you're a sloth!");
}
else if (userInput.Contains("Tuna salad")){
Console.WriteLine($"Since you chose Tuna salad as your food, you're a fish!");
}
else{
Console.WriteLine($"Since you chose {userInput} as your food, I can only say that you're a dinosaur!");
}