程序中需要的字符串是“未使用”的 C#
本文关键字:未使用 字符串 程序 | 更新日期: 2023-09-27 18:34:00
我一直在用C#制作文本冒险游戏(以了解有关字符串等的更多信息(。我有大约 3 个随机场景,在您开始的 10 个基本场景之后触发。其中之一是猴庙。"menu"类有两个参数,MainArgs 和 takenDump。在这种情况下,您可以选择进行转储,如果 takenDump 为真(bool(,则会说"你旁边有便便"。如果使用无法识别的命令,则 MainArgs 用于返回到同一方案。
public void ConScen1()
{
string MainArgs;
bool takenDump; // Available commands are: "hit monkey talk to monkey suicide go inside"
string conchoice1;
Console.WriteLine("You arrive at a temple dedicated to the monkey god.");
Console.WriteLine("Outside is a monkey, presumably guarding the place.");
Console.WriteLine("What do you do?");
Console.Write(">");
conchoice1 = Console.ReadLine();
if (conchoice1.Contains("hit monkey"))
{
Console.WriteLine("You hit the monkey. He draws a knife.");
Console.WriteLine("He stabs you in the eye. You bleed to death.");
Console.WriteLine(" -- Game Over --");
Console.WriteLine("Press any key to start over..");
Console.ReadKey();
takenDump = false;
MainArgs = "null";
TextAdventure1.AdventureTime.Menu(MainArgs, takenDump);
}
}
这里的问题是"字符串 MainArgs;"行。我需要它用"null"调用 Menu(( 来重新开始。但是,工作室说它未使用(即使它在 if 语句中使用(。有没有办法禁用警告或解决问题?如果我删除该行,它会给我一个关于如何不声明 MainArgs 的错误(在 if 语句中(。
这里的问题是"字符串 MainArgs;"行。我需要它用"null"调用 Menu(( 来重新开始。
不,你真的没有。你也不需要takenDump
。您只需将呼叫更改为:
TextAdventure1.AdventureTime.Menu("null", false);
不过,需要传入一个带有值"null"
的字符串是很奇怪的......
我还强烈建议您在首次使用时声明变量,因此请在此处声明conchoice1
:
string conchoice1 = Console.ReadLine();
(我也会重命名它...以及您的方法。你到处都是奇怪的名字。命名既重要又困难。
请注意,您的程序在"重新启动"的方式上也很奇怪 - 它通过再次调用自身来实现(我假设Menu
是某种顶级方法,最终可以调用ConScen1
。想想用户多次失败后,您的执行堆栈会是什么样子......您不想以这种方式递归。目前还不清楚你的程序控制流总体上是什么样的,但你应该改变游戏的状态,并注意到更高的位置,而不是这种方法。