【问题标题】:convert string array to string for if else condition将字符串数组转换为 if else 条件的字符串
【发布时间】:2019-05-06 01:53:24
【问题描述】:

我正在努力学习,(我是编码新手)

这是我写的

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(); 

我得到它的错误

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

感谢您的帮助!

【问题讨论】:

  • Console.ReadLine 返回单个字符串。您已将变量 Userclass 定义为字符串数组 ([])。赋值运算符右侧的事物类型(=)需要匹配(或兼容)赋值运算符左侧的变量类型。
  • 您应该清楚区分两件事:用户选择的类和用户拥有的选项。第一个是单个字符串。第二个是你的字符串数组。
  • 考虑到你正在学习编码,我也会给你这些建议。使用“string”而不是“String”,大写的用法是指方法和类,而您打算指的是一种类型。此外,在命名对象时,使用小写(例如“userclass”而不是“Userclass”),没有真正的理由这样做,除非它遵循一般命名约定。 :)
  • @DennisVanhout string is an alias for String...它们在功能上是相同的。有些人实际上建议使用String 来实现跨语言兼容性。
  • @KennethK。根据该逻辑,您还应该/可以使用 Boolean 和 Int32 而不是 bool 和 int。

标签: c# string conditional-statements


【解决方案1】:

好的,有几件事:

您的UserClassarray。它将保存项目,但除非您附加到它,否则您不能将用户输入写入它。 Here 是来自 MSDN 的 C# 中 Arrays 的引用。

您在string array 上使用.Contains() 而不是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 保持不变(我已将大写更改为驼峰式)。只要您正确检查array,将这些userClasss 存储在array 中就没有问题(我看到)。

而不是检查string[] userClass 数组是否包含这些项目,因为我们知道它会按照我们编写的方式执行并且如前所述,它总是会运行true。相反,我们检查user input 是否匹配数组中的某些内容。

我创建了string input = Console.ReadLine() 以允许用户输入,然后我检查inputstring[] 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;
}

【讨论】:

  • 感谢一百万!这按我的需要工作!我试图通过做这样愚蠢的事情来让自己学习语言变得有趣,所以我很感激你的帮助
  • @JamesSketchAndTheBeastSul ,没问题!编码愉快!
【解决方案2】:

给你,我写了 cmets 解释程序的每个部分,如果你有什么不明白的地方,请告诉我。

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
    }
}

【讨论】:

  • 哦,这是一种更清洁的方法,比我在 atm 上做的要先进一些。但是谢谢,我也不能适应这种情况。
  • 我添加了这个示例,以便您可以看到我编写的代码背后的逻辑并将其与您的代码进行比较,这样您就可以继续修改它(并添加了 cmets,以便您了解正在发生的事情)。我基本上使用了与您相同的概念和数据结构,但更改了逻辑,因此您可以看到严重依赖数据结构和语言结构(例如多个 if 语句)不是这种方式。但是由于您是初学者,我猜一切都是允许的,仍然很高兴我能提供帮助,或者我希望我能提供帮助:)
  • 绝对!是的,我同意你的方法,理想情况下,我宁愿让事情尽可能简单,因为我计划让这成为一个非常冗长的文字冒险,有一天会有实际的图像和场景,这样它就可以变得非常长!我仍然试图理解数组和东西,所以我试图在部分中以不同的方式实现不同的东西 (Array.IndexOf(charClasses, userclass.ToLower()) != -1) 那在做什么,什么是!=- 1 句话?
  • Array.IndexOf 是类 Array 的方法,所有像这样声明的对象 int[] or string[] 都实现了该类,方法 IndexOf 将数组作为第一个参数和值作为第二个,该方法通过查找匹配的第一个值在数组中查找该值。如果匹配,该方法返回一个大于或等于0的数字,否则返回-1,我所做的基本上是检查不是-1来执行if语句中的内容,否则它什么都不打印。尝试添加一个数组中不存在的类,它什么也不打印。
  • 不要当乞丐,但如果答案对你有帮助,我可以投赞成票吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-06-18
  • 2017-11-27
  • 2016-05-15
  • 2022-11-23
相关资源
最近更新 更多