如果条件成立,则将字符串数组转换为字符串

时间:2018-12-04 15:31:16

标签: c# string conditional-statements

我正在尝试学习,(这是编码的新手)

我写了这个

static void Main(string[] args)
{
    string username = "James";
    string[] Userclass = new string[3] { "Mage", "Warrior", "Assasin" };

    Console.WriteLine("What class will you be? You can choose from Mage, Warrior, Or Assasin: ");


    if (Userclass.Contains("Mage"))
    {
        String Message = "You are a strong Willed Mage " + username;
        Console.WriteLine(Message);
    }
     if (Userclass.Contains("Warrior"))
    {
        String Message = "Your are a valiant Warrior " + username;
        Console.WriteLine(Message);
    }
     if (Userclass.Contains("Assasin"))
    {
        String Message = "You are a vigilant Assasin " + username;
        Console.WriteLine(Message);

    }
    Console.ReadLine();
}

但是如果我定义:

string Userclass = Console.Readline(); 

我得到一个错误

  

无法将字符串[]转换为字符串

感谢您的帮助!

2 个答案:

答案 0 :(得分:3)

好的,几件事:

您的UserClassarray。它可以容纳项目,但是除非您要附加它,否则您不能在其中写入用户输入。 Here是MSDN中C#中Arrays的引用。

您在.Contains()而不是string array上使用string。从逻辑上讲,是的,string array 确实包含这些值。虽然,如果实际上运行/编译过,所有if statements都会运行 true 而不是从用户输入中选择。

这使我想到了我的下一件事情-您要求user input,但实际上并没有在所示的Main()方法中允许这样做。 (我见过的)最常见的方式是这样的:

string input = Console.ReadLine();

哪个,我看到您正在尝试实现,所以这不是问题:)


此代码应该适用(我将细化更改):

static void Main(string[] args)
{
    string userName = "James";
    string[] userClass = new string[3] { "mage", "warrior", "assassin" };

    Console.WriteLine("What class will you be? You can choose from Mage, Warrior or Assassin:");
    string input = Console.ReadLine();

    if (input.ToLower() == userClass[0])
    {
        string Message = "You are a strong Willed Mage " + userName;
        Console.WriteLine(Message);
    }
    else if (input.ToLower() == userClass[1])
    {
        string Message = "Your are a valiant Warrior " + userName;
        Console.WriteLine(Message);
    }
    else if (input.ToLower() == userClass[2])
    {
        string Message = "You are a vigilant Assassin " + userName;
        Console.WriteLine(Message);
    }
    else
        Console.WriteLine("No proper class selected...");

    Console.ReadLine();
}

string userName与您的string[] userClass相同(我将大写字母更改为骆驼装)。只要将userClass正确地检查一下,将这些array存储在array中就没有问题。

不是检查string[] userClass数组是否包含这些项目,因为我们知道它已经按照我们的描述进行了写入,并且如上所述,它将始终运行 true 。相反,我们检查user input是否匹配数组中的某些内容。

我创建了string input = Console.ReadLine()以允许用户输入,然后我根据input的值检查string[] userClass。我添加了.ToLower()并将数组值更改为全部小写。这样,如果用户输入mAgE而不是Mage,就不会抛出错误。


要避免使用array的另一种方法是使用switch

switch (input.ToLower())
{
    case "mage":
        Console.WriteLine($"You are a strong Willed Mage {userName}");
        //assign user class
        break;
    case "warrior":
        Console.WriteLine($"Your are a valiant Warrior {userName}");
        //assign user class
        break;
    case "assassin":
        Console.WriteLine($"You are a vigilant Assasin {userName}");
        //assign user class
        break;
}

答案 1 :(得分:1)

在这里,我写了注释来解释程序的每个部分,如果您听不懂,请告诉我。

static void Main(string[] args)
{
    Console.Write("Enter Username: "); // we ask for the username here, you can do it with Console.WriteLine() as well
    string username  = Console.ReadLine();  // get the username
    Console.Write("What class will you be? You can choose from Mage, Warrior, Or Assasin: "); // ask for the class, also same as above, you can use Console.WriteLine()
    string userclass = Console.ReadLine();   // get the class

    // Possible character classes (we use this as a reference to check the class the user gives us later)
    // also set the string representing each class as lower case characters to avoid looping or unnecessary string concatenation later
    string[] charClasses = new string[3] { "mage", "warrior", "assasin" }; // make sure the class given is within our possible character classes

    // here we check that the class given is within the bound of our possible classes and convert
    // the userclass to its lower case equivalent to match it with those in the array
    if (Array.IndexOf(charClasses, userclass.ToLower()) != -1)   
    {
         // if the class is acceptable then attach it to the message along with the username via string concatenation
        String Message = "You are a strong Willed "+userclass + " " + username; 
        Console.WriteLine(Message);  // print the message
    }
}