如果声明更改,提示不会注意到

时间:2014-07-02 23:11:41

标签: javascript if-statement prompt

请帮忙。当使用console.log语句运行代码时,它会在提示符中输入数字,然后单独记录答案,返回语句显然无效。

prompt("You begin to move but as you evade most of the bullets some of them hit your " +          bodyPart + " you begin to feel the blood shushing out, As people realize what happened there begins to be chaos. You look at the direction of the bullets to find 3 guys with heavy weaponry. They are almost done reloading. will you take COVER behind a table, RUN out of the place, or... suddenly you hear the girl telling you 'Take cover'... FIGHT them?").toUpperCase()

var bodyPart = Math.random()
                            if (bodyPart > .75){
                                bodyPart = "Right Arm"
                            }
                            else if (bodyPart > .50){
                                bodyPart= "Left Arm"
                            }
                            else if (bodyPart >.25){
                               bodyPart = "Right Leg"
                            }
                            else{
                              bodyPart = "Left Leg"
                            }

2 个答案:

答案 0 :(得分:0)

更改命令的顺序,以便在选择之后打印身体部位。现在你正在打印倒数第二个身体部位。

var bodyPartRoll = Math.Random();
var bodyPart;
if (bodyPartRoll > 0.75.){
   bodyPart = "Right Arm";
}else if(...){
   ...
}

prompt(bodyPart)

我还创建了一个单独的varialbe来存储Math.Random()的结果。这不是必需的,但我认为将一个变量用于两件事令人困惑。此外,如果您不知道Javascript中的自动分号插入是如何工作的,我建议您在行尾添加分号。

答案 1 :(得分:0)

也许你应该使用两个变量。一个叫randomNumber,一个叫bodyPart。然后你不会改变类型,你也应该更清楚地区分两者。