【问题标题】:Prompt does not notice if statement change [closed]如果语句更改,提示不会通知[关闭]
【发布时间】:2014-07-02 23:11:41
【问题描述】:

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

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

【问题讨论】:

  • 请更具描述性,可能发布整个代码,甚至可能发布一个显示问题的 jsfiddle。

标签: javascript if-statement prompt


【解决方案1】:

更改命令的顺序,以便在选择它之后打印正文部分。现在您正在打印倒数第二个正文部分。

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

prompt(bodyPart)

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

【讨论】:

    【解决方案2】:

    也许您应该使用两个变量。一种称为randomNumber,一种称为bodyPart。那么您将不会更改类型,并且您还应该对两者有更清晰的区分。

    【讨论】:

      猜你喜欢
      • 2016-07-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-21
      • 2015-08-17
      • 2021-09-25
      • 1970-01-01
      • 2013-08-31
      相关资源
      最近更新 更多