2013-02-11 117 views
-1

我正在创建基于文本的RPG。你能帮我创建一个方法吗?我想学习如何创建一个方法,所以我不需要将"help"case复制到每个循环中。创建方法

这是我想要的方法做:

string command; 
while (command != "exit game") 
{ 
    command=Console.ReadLine(); 

    switch(command){ 
    case (help): 
     Console.WriteLine("List of useableverbs and nouns"); 
     break; 
    default: 
     Console.WriteLine("Invalidcommand"); 
     break; 
     } 
    } 

另外,我怎么可以设置它,以便“退出游戏”退出游戏?

我开始编程在几个星期前,所以任何帮助,将不胜感激

+4

什么问题? – Pete 2013-02-11 14:01:51

+1

以后请格外小心。清理起来很麻烦! – PhonicUK 2013-02-11 14:04:23

+0

我更新了帖子,使问题更加清晰。您向我们展示代码的事实告诉我们您尝试了什么,并允许我们为您提供帮助。在您展示您尝试过的内容并解释问题所在之处后,无需解释“您可以在哪里进行自己的研究”。一定要问更直接的问题以避免混淆。 – 2013-02-11 14:18:41

回答

1

这里有一个方法:

void HandleCommand(string command) 
{ 
    switch (command) 
    { 
     case (help): 
      Console.WriteLine("List of useable verbs and nouns"); 
      break; 
     default: 
      Console.WriteLine("Invalid command"); 
      break; 
    } 
} 

,并使用它:

while (command != "exit game") 
{ 
    command=Console.ReadLine(); 
    HandleCommand(command); 
} 
0

除非你有一个名为help的变量,你想引用它。 IE:

case("help"): 

书面,键入“退出游戏”应该打破循环,如果你把它放在你的情况下关闭控制台窗口。它可能更干净,就像使用常量而不是硬编码的字符串。

0

在C#中,您可以像创建方法一样创建方法。比方说,你想展示你的帮助文本,那么任何其他方法(可能是你的主要方法)之外,你会写的线沿线的东西:

static void ShowHelp() { 
    Console.WriteLine("This is some text. Enter some command!"); 
    var command = Console.ReadLine(); 
    //Do other things 
} 

然后每当你想要的文字显示出来,你可以用ShowHelp()来调用它。

1

您可以在method中将其设置为do。方法创建可重用的代码。这避免了需要复制和粘贴相同的逻辑。而不是复制粘贴代码,您只需通过调用方法(在这种情况下):

CheckCommand();

该方法可以看起来像...

private static void CheckCommand() 
{ 
    string command; 
    do 
    { 
     command = Console.ReadLine(); 
     switch (command) 
     { 
      case ("help"): 
       Console.WriteLine("List of useable verbs and nouns"); 
       break; 
      default: 
       Console.WriteLine("Invalid command"); 
       break; 
     } 
    } 
    while (command != "exit game"); 

} 

这是设置,所以如果用户类型在“退出游戏”中,循环将退出。在另一个说明中,扩展这种逻辑的一个好方法是做一个case insensitive比较。

0

您可以使用一个布尔变量来将游戏结束通知给循环。该命令必须在循环中查询,并且由于在循环开始时不知道该命令,因此我会将循环条件放在循环结尾。

bool doExit = false; 
do { 
    string command = Console.ReadLine().ToLower(); 
    switch (command) { 
     case "exit": 
     case "quit": 
      doExit = true; 
      break; 
     case "otherCommand": 
      HandleOtherCommand(); 
      break; 
     case "?": 
     case "h": 
     case "help": 
      PrintHelp(); 
      break; 
     default: 
      Console.WriteLine("Invalid command!"); 
      PrintHelp(); 
      break; 
    } 
} while (!doExit); 

使用布尔变量的优点是,当满足其他条件时可以轻松终止游戏。例如当玩家赢得或失败时。


现在的方法。您可以将方法放置在相同的源代码(Program.cs)中或创建新的类。在计划内。CS你会写类似

private static void PrintHelp() 
{ 
    Console.WriteLine("Valid commands:"); 
    Console.WriteLine("help, h, ?: Print this help."); 
    Console.WriteLine("exit, quit: End this game."); 
    Console.WriteLine("..."); 
} 

注意,由于Main方法是静态的,这同一个类中的其他方法必须是静态的了。如果您为命令创建其他类,则可以选择是否要创建静态类。静态类只是放置方法的地方。

static class GameCommands 
{ 
    // The `public` modifier makes it visible to other classes. 
    public static void PrintHelp() 
    { 
     ... 
    } 

    public static void SomeOtherCommand() 
    { 
     ... 
    } 
} 

您可以使用className.MethodName()语法调用此类方法。

GameCommands.PrintHelp(); 

如果您必须存储不同的状态(例如分数)。创建非静态类是合适的。您可以创建类称为类实例或对象的类的副本。这实际上只复制类的状态,而不是方法代码。非静态方法可以作用于这种状态(字段,属性)。

class Player 
{ 
    // Constructor. Initializes an instance of the class (an object). 
    public Player (string name) 
    { 
     Name = name; 
    } 

    // Property 
    public int Score { get; private set; } 

    // Property 
    public string Name { get; private set; } 

    // Instance method (non-static method) having a parameter. 
    public void IncreaseScoreBy(int points) 
    { 
     Score += points; 
    } 

    public void PrintWinner() 
    { 
     Console.WriteLine("The winner is {0} with a score of {2} points." Name, Score); 
    } 
} 

可以使用这样一类这样的

Player player1 = new Player("John"); // Pass an argument to the constructor. 
Player player2 = new Player("Sue"); 

player1.IncreaseScoreBy(5); 
player2.IncreaseScoreBy(100); 

if (player1.Score > player2.Score) { 
    player1.PrintWinner(); 
} else if (player2.Score > player1.Score) 
    player2.PrintWinner(); 
} else { 
    Console.WriteLine("The score is even!"); 
} 

我们使用没有返回值的方法到现在为止。这通过替换返回类型的void关键字来表示。

void MethodWithNoReturnValue() { ... } 

如果你有一个返回值(即你的方法是一个函数),你必须指定返回类型。 return语句终止函数并指定返回值。

double Reciprocal(double x) 
{ 
    return 1.0/x; 
} 

你将同一类

double y = Reciprocal(x); 

内或与对象名称或类名前面加上它,如果它是静态的称呼它。