C#Array.Contains()不工作 [英] C# Array.Contains() Not Working

查看:95
本文介绍了C#Array.Contains()不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好。我的问题是带有contains的if语句没有响应。它只是转到else语句。有没有办法修复此代码或以不同的方式来做到这一点?提前谢谢。

Hello. My issue is that the if statement with the contains is not responding. It simply goes to the else statement. Is there a way to fix this code or a different way to do this? Thank you in advance.

using System.Linq;



*我有这个使用选项。这是因为这种方式是找到数组中的项目不起作用吗?


*I do have this using option. Is this the reason that this way is of finding an item in the Array is not working?

#region class rooms
class Room
{
    public string Title;
    public string Description;
    public string map_location;
    public Room Close;
    public Room Open;
    public Room Warn;
    public Room Dwarn;
    public Room Swarn;
    public Room Awarn;
    public Items[] roomitems = new Items[10000];
    public Challenge RoomChallange;
    public Challenge RoomChallange2;
    public Challenge RoomChallange3;

    public Room(string title, string description, string maplocation)
    {
        Title = title;
        Description = description;
        map_location = maplocation;
    }
}



*我把数组放在这里。我已经看到其他人使用Arrays的例子将Array放在Main(string [] args)中。我尝试将它放在Main(string [] args)中,但这对于这种情况没有任何帮助。下一部分是在Main(string [] args)


*I placed the array in here. I have seen other people with examples of Arrays have put the Array in the Main(string[] args). I tried putting it in the Main(string[] args) but that did nothing to help the situation. This next part is in the Main(string[] args)

Items Orcics_200 = new Items("Orcics_200", "A pile of two hundred Orcics.", 0, .2, 0, 200);
<pre>     
Room administrativeoffice21 = new Room("Administrative Office", "The shops are to Dwarn, the entrance to Orcinium is to the Warn, the inns inside the wall are to the Awarn, and the entrance back into Aldoria is to the Swarn. This is the administrative office. Hundreds of Orcs and adventures are lined behind you. You can feel hope, joy, and tension of these people. The administrative officers are franticly filling in the administrative papers of the new immigrants; as soon as your papers are filled up you may as well be invisible.", "Your location is (2,1)");
<pre>            
administrativeoffice21.roomitems[1] = Orcics_200;



*每当我启动此代码时,我都会得到else部分。这是为什么?


*Whenever I fire up this code I get the else section. Why is this?

case "take two hundred orcics":
    if (currentRoom.roomitems.Contains(Orcics_200))
    {
        Console.WriteLine("You have taken {0}.", Orcics_200.Name);
        playeritems.Add(Orcics_200);
        playerwieght = playerwieght + Orcics_200.Wieght;
        currentRoom.Description += " You have taken the 200 Orcics.";
    }
    else
    {
        Console.WriteLine("The Orcics are not here / you have already taken it.");
        goto fix;
    }
    break;

*提前感谢您的所有答案。

*Thank you in advance for all of your answers.

推荐答案

尝试以下代码:



try following code:

case "take two hundred orcics":
                            //ToUpper() added
    if (currentRoom.roomitems.ToUpper().Contains(Orcics_200.ToUpper()))
    {
        //i add .ToUpper() or you can use .ToLower() 
        //Condition getting false cause of LetterCase.
        Console.WriteLine("You have taken {0}.", Orcics_200.Name);
        playeritems.Add(Orcics_200);
        playerwieght = playerwieght + Orcics_200.Wieght;
        currentRoom.Description += " You have taken the 200 Orcics.";
    }
    else
    {
        Console.WriteLine("The Orcics are not here / you have already taken it.");
        goto fix;
    }
    break;





if b关注让我知道。



谢谢



if any concern let me know.

thanks


这篇关于C#Array.Contains()不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆