如何使用超过2个字符串的if语句 [英] How to have an if statement with more than 2 strings
本文介绍了如何使用超过2个字符串的if语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
基本上我知道这可能很简单....但是我想要一个if语句来检查输入的单词是否与数组中的3个值相匹配...如果它们被添加到列表中......
下面是我正在谈论的代码以及其他类的代码..(不是整个项目,太大了)
public static string PlayerSelectsItem( string word,对象 [] args)
{
if (word == torch, shield, sword)
{
GameEngine.Instance.RemoveI temFromRoom(字);
Inventory.AddItem(word);
Inventory.PrintItems();
Console.WriteLine();
return MOVE_OR_PICK ; // 不要更改操作
}
else
{
Console.WriteLine( 该项目确实如此不存在);
}
return 跨度>;
}
< pre lang = cs> public class RoomThree:Room
{
// 属性
public static string [] _availableItems = new string [] {& quot; torch& quot;,& quot; shield& quot;,& quot; sword& quot; };
public static ChatLine [] _lines = new ChatLine []
{
// 新的ChatLine(& quot; START& quot;,& quot;欢迎来到3号房间。#DIRECTIONS#。& quot;,& quot; move +?& quot;,GameEngine.Instance .PlayerMove),
new ChatLine(& quot; START& quot;,& quot; Welcome to Room 3 。 一张带有武器和电话铃声的桌子。& quot; +
& quot ;我建议你回答它& quot;,& quot; answer,answer phone& quot;,AnswerPhone),
new ChatLine(& quot) ; TABLE& quot;,& quot;表上有项目& quot;,& quot; look,lo ok table& quot;,LookTable),
new ChatLine(& quot; FOUND_ITEMS& quot;,& quot;你看着桌子那里#AVAILABLE_ITEMS#。& quot;,& quot;& quot;,PlayerSelectsItem),
new ChatLine(& quot; MOVE_OR_PICK& quot; ,& quot;向北门移动,或选择另一个项目,#AVAILABLE_ITEMS#& quot;,& quot; pick +?& quot;,PlayerSelectsItem),
新 ChatLine(& quot; MOVE_OR_PICK& quot;,& quot;向北方doorm移动或选择另一个项目,#AVAILABLE_ITEMS#& quot;,& quot; move +?& quot; ;,GameEngine.Instance.PlayerMove),
new ChatLine(& quot; MOVE_OR_PICK& quot;,& quot;向北方道舍移动或选择另一个项目,#AVAILABLE_ITEMS#& quot;,& quot; drop +?& quot;,PlayerDropsItem),
new ChatLine(& quot;& quot;,& quot;& quot;,& quot; pick + ?, select + ?, select +?& quot; ;,PlayerSelectsItem),
new ChatLine(& quot;& quot;,& quot;& quot;,& quot; drop +? & quot;,PlayerDropsItem),
new ChatLine(& quot;& quot;,& quot;& quot;,& quot; ; move +?& quot;,GameEngine.Instance.PlayerMove)
};
// 构造函数
public RoomThree()
: base (& quot; ROOM_THREE& quot;,_lines,& quot ; START& quot;,_ availableItems)
{
}
// 方法
私人 静态 < span class =code-keyword> string AnswerPhone( string word, object [] args)
{
Console.WriteLine();
Console.WriteLine(& quot; Hello human。我的名字是 Sauron。我是的领导者比赛。您已成为问题 me& quot; +
& quot;和 我的种族。但是我在中看到潜在的。我在 >你..所以我会为你提供这个交易....& quot; +
& quot;如果你能得到给我,我将为你提供一个指挥官角色,带领我的军队对抗恶魔。但这将是艰难的,你& quot; +
& quot;将不得不面对非常有经验的战斗兽人,你必须找到你的方式..但如果你生存。& quot; +
& quot;我会让你成为我的第二个在命令中。现在你面前的 表上有武器,另一个房间里有一个orc 。 QUOT; +
& quot;祝好运。你需要它。& quot;);
Console.WriteLine();
return & quot; TABLE& quot ;;
}
private static string LookTable( string word, object [] args)
{
Console.WriteLine();
return & quot; FOUND_ITEMS& quot ;;
}
public static string PlayerSelectsItem( string word, object [] args)
{
if (word ==& quot; torch& quot;,& quot; shield& quot;,& quot; sword& quot; )
{
GameEngine.Instance.RemoveItemFromRoom(word);
Inventory.AddItem(word);
Inventory.PrintItems();
Console.WriteLine();
返回& quot; MOVE_OR_PICK& quot ;; // 不要改变行动
}
else
{
Console.WriteLine(& quot;该项目不存在& quot;);
}
return & quot;& quot ;;
}
public static string PlayerDropsItem( string word, object [] args)
{
if (Inventory.HasItem(word))
{
Inventory.RemoveItem(word);
GameEngine.Instance.DropItemInRoom(word);
Console.WriteLine(& quot; Item已被删除& quot;);
}
else
{
Console.WriteLine(& quot;你没有&#39; t)项&安培; QUOT);
}
return & quot;& quot ;; // 不要改变行动
}
} < / pre >
解决方案
if ((word.equals(< span class =code-string> torch))||(word.equals( shield))||(word.equals( 剑)))
你想过这个吗?
< blockquote>你可能会改变
Quote:if(word ==torch,shield,剑)
{
to
< span class =code-keyword> string [] goodwords = { torch, shield, sword};
if (Array.IndexOf(goodwords,word)!= -1)
{
if (word == torch&& word == shield&& word == sword)
或
if (word == torch || word == shield || word == sword)
Basically I know this is probably so simple.... But I want to have an if statement that checks if the entered word matches 3 values in an array... if they are then they get added to a list..
Below is the code that i'm talking about and also the code for the rest of the class.. (not the entire project, too big)
public static string PlayerSelectsItem(string word, object[] args)
{
if (word == "torch", "shield", "sword" )
{
GameEngine.Instance.RemoveItemFromRoom(word);
Inventory.AddItem(word);
Inventory.PrintItems();
Console.WriteLine();
return "MOVE_OR_PICK"; // Don't change action
}
else
{
Console.WriteLine("The item does not exist");
}
return "";
}
<pre lang="cs">public class RoomThree:Room
{
// Attributes
public static string[] _availableItems = new string[] { "torch", "shield", "sword" };
public static ChatLine[] _lines = new ChatLine[]
{
// new ChatLine("START", "Welcome to Room 3.. There are doors to the #DIRECTIONS#.", "move+?", GameEngine.Instance.PlayerMove),
new ChatLine("START", "Welcome to Room 3. There is a table with weapons and a phone ringing on it. " +
"I would suggest you answer it", "answer, answer phone", AnswerPhone),
new ChatLine("TABLE", "There are items on the table", "look, look table", LookTable),
new ChatLine("FOUND_ITEMS", "You looked at the table and there #AVAILABLE_ITEMS#.", "", PlayerSelectsItem),
new ChatLine("MOVE_OR_PICK", "Move towards the North door, or pick another item, there #AVAILABLE_ITEMS#", "pick+?", PlayerSelectsItem),
new ChatLine("MOVE_OR_PICK", "Move towards the North doorm or pick another item, there #AVAILABLE_ITEMS#", "move+?", GameEngine.Instance.PlayerMove),
new ChatLine("MOVE_OR_PICK", "Move towards the North doorm or pick another item, there #AVAILABLE_ITEMS#", "drop+?", PlayerDropsItem),
new ChatLine("", "", "pick+?, select+?, choose+?", PlayerSelectsItem),
new ChatLine("", "", "drop+?", PlayerDropsItem),
new ChatLine("", "", "move+?", GameEngine.Instance.PlayerMove)
};
// Constructors
public RoomThree()
: base("ROOM_THREE", _lines, "START", _availableItems)
{
}
// Methods
private static string AnswerPhone(string word, object[] args)
{
Console.WriteLine();
Console.WriteLine("Hello human. My name is Sauron. I am the leader of this race. You have became a problem for me," +
" and for my race. But I see potential in you. I see a great warrior in you.. So I will offer you this deal...." +
"If you can get to me, I will offer you a commander role leading my army against the demons. But it will be tough, you" +
" will have to face very experienced fighting orcs and you will have to find your way to me.. But IF you SURVIVE." +
" I will make you my second in command. Now there are weapons on the table in front of you, and an orc in the other room." +
" Good luck. You will need it.");
Console.WriteLine();
return "TABLE";
}
private static string LookTable(string word, object[] args)
{
Console.WriteLine();
return "FOUND_ITEMS";
}
public static string PlayerSelectsItem(string word, object[] args)
{
if (word == "torch", "shield", "sword" )
{
GameEngine.Instance.RemoveItemFromRoom(word);
Inventory.AddItem(word);
Inventory.PrintItems();
Console.WriteLine();
return "MOVE_OR_PICK"; // Don't change action
}
else
{
Console.WriteLine("The item does not exist");
}
return "";
}
public static string PlayerDropsItem(string word, object[] args)
{
if (Inventory.HasItem(word))
{
Inventory.RemoveItem(word);
GameEngine.Instance.DropItemInRoom(word);
Console.WriteLine("Item has been dropped");
}
else
{
Console.WriteLine("You don't have that item");
}
return ""; // Don't change action
}
}</pre>
解决方案
if((word.equals("torch"))||(word.equals("shield"))||(word.equals("sword")))
U thought about this ?
You might change from
Quote:if (word == "torch", "shield", "sword" )
{
to
string [] goodwords = { "torch", "shield", "sword" }; if (Array.IndexOf(goodwords, word) != -1) {
if (word == "torch" && word== "shield" && word == "sword" )
or
if (word == "torch" || word== "shield" || word == "sword" )
这篇关于如何使用超过2个字符串的if语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文