我一直在用C#制作一个文本冒险游戏(了解更多关于字符串等)。我有大约3个随机场景,这些场景是在您开始的10个基本场景之后触发的。其中一个是猴庙。 “menu”类有两个参数,MainArgs和takenDump。在场景中你可以选择进行转储,如果takeDump为true(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语句中)。
答案 0 :(得分:1)
不,你真的没有。您也不需要这里的问题是“字符串MainArgs;”线。我需要它以“null”调用Menu()来重新开始。
takenDump
。您只需将呼叫更改为:即可
TextAdventure1.AdventureTime.Menu("null", false);
虽然......
需要传入值为"null"
的字符串,但这很奇怪
我还强烈建议您在首次使用时声明变量,因此请在此处声明conchoice1
:
string conchoice1 = Console.ReadLine();
(我也将它重命名为......以及你的方法。你到处都有奇怪的名字。命名很重要而且很难。)
请注意,你的程序在“重启”的方式上也很奇怪 - 它是通过再次调用自己来实现的(我假设Menu
是一些顶级方法,最终可能最终调用ConScen1
。想想在用户多次失败后你的执行堆栈会是什么样子......你不想以这种方式递归。目前还不清楚你的程序控制流程是什么样的,但你应该正在改变游戏的状态,并注意到更高的,而不是这种方法。