【发布时间】:2014-09-15 10:29:31
【问题描述】:
好的,所以我正在创建一个基于文本的探索类型游戏。我想知道如何循环 if/else 语句以及重置变量。我有它,所以你扮演的角色从他的卧室开始,你必须输入命令让你的角色跟随,例如环顾四周。如果您还可以帮助我用 java 识别所有类型的表单中的相同单词(在任何地方都使用大写字母)。
if (begin === 'look around')
{
confirm('To your right is the door.Left is the dresser with some pictures on it and a mirror on the wall. In front of you is the cabinets with a TV on top. Behind you is the bed')
}
else if (begin === 'look')
{
confirm('To your right is the door.Left is the dresser with some pictures on it and a mirror on the wall. In front of you is the cabinets with a TV on top. Behind you is the bed')
}
else if (begin === 'examine room')
{
confirm('To your right is the door.Left is the dresser with some pictures on it and a mirror on the wall. In front of you is the cabinets with a TV on top. Behind you is the bed')
}
else if (begin === 'examine the room')
{
confirm('To your right is the door.Left is the dresser with some pictures on it and a mirror on the wall. In front of you is the cabinets with a TV on top. Behind you is the bed')
}
else if (begin === '?')
{
confirm('If you have not already noticed, this game is completely Text-based. In order to progress through the game, you will need to type in commands that you think the character you are playing as should do.Example: look around.')
}
【问题讨论】:
-
用
while(true)包裹整个代码。或者改变真实,这样你就可以真正结束游戏了。 (while(stillPlaying)),并且在您的条件之一内,您可以将stillPlaying设置为 false。至于大写问题,只需将 begin.toLowerCase() 改为 begin.toLowerCase(),并确保您与之比较的字符串始终为小写。 -
我还会在您检查它们的值之前将您的字符串值转换为小写。这样,无论是看、看还是看都没有关系。如果这些值是从某种类型的命令行中提取的,我会在您检索它们后立即转换它们。
-
您可以简单地使用包含所有选项的数组和 indexOf 来检查数组中是否存在选项,而不是使用 if-else。
-
使用递归函数调用。
标签: javascript loops if-statement reset