【问题标题】:The >= and <= does not seem to work well with double digital? Javascript>= 和 <= 似乎不适用于双数字? Javascript
【发布时间】:2020-11-24 18:45:18
【问题描述】:

发生的情况是,我注意到我正在使用 if 语句并检查我是否有足够的积分来获得 >= 的内容,但遇到了一个问题,即 100 或 200 之类的三位数数字很好,但是作为当我尝试做50时,它似乎无法检查它,这导致即使你有很多积分也无法购买。我确实注意到当我将最大点增加到 500 或更高时它会起作用,但我不想这样做。我最近发现它的另一种解决方案是像 050 一样,但是说 50 很尴尬,你们知道为什么支票很难用两位数或正确的方法吗?

冲突/错误是在 Sword 中建立的,就在最后一个“测试”之前,也可以通过只有长句的评论找到它。


function equipment() {

    clear();

    console.log(point);

    $('<p class=\'texts\'> Here is your equipment screen, please select what you want and if make a mistake or dislike your choice, please click on reset button, otherwise here is your ' + point + ' points to spent on here.</p>').insertBefore('#placeholder');

    $('<button class=\'command_button e-buttons\' value=\'pistol\'>Pistol</button>').insertBefore('#placeholder_choice');

    $('<button class=\'command_button e-buttons\' value=\'rifle\'>Rifle</button>').insertBefore('#placeholder_choice');

    $('<button class=\'command_button e-buttons\' value=\'sword\'>Sword</button>').insertBefore('#placeholder_choice');

    $('<button class=\'command_button e-buttons\' value=\'test\'>Test</button>').insertBefore('#placeholder_choice');

};

$(document).on('click', '.e-buttons', function() {

    var button = $(this).val();
    console.log('The Button is ' + button);
    $('#console').scrollTop($('#console')[0].scrollHeight);
    
    if (point <= 0) {

        alert("Sorry, you have no more point left, please either continue to next step or remove other choices.");

        equipment();

    }

    if (button == 'pistol') {

        if (point >= '100') {

            console.log("Your equipment menu is working. " + button);
            point = point - '100';
            console.log("the point remaining is " + point + ".");
            equipment();

        }

        else {

            alert("Sorry not enough money for pistol.");
            equipment();

        }

    }

    else if (button == 'rifle') {

        if (point >= '200') {

            console.log("Your equipment menu is working. " + button)
            point = point - '200';
            console.log("the point remaining is " + point + ".")
            equipment();

        }

        else {

            alert("Sorry not enough money for rifle.");
            equipment();

        }

    }

    else if (button == 'sword') {

        if (point >= '50') {

            console.log("Your equipment menu is working. " + button)
            point = point - '50';
            console.log("the point remaining is " + point + ".")
            equipment();

        }

        else { // Fix this, something is causing it to be error when it at 50, but if it 100 or more, it work fine. Also notice that if you reduced total to below 400, it will cause same error but are fine if over 400... So something is clearly prevent point check of sword from see the point. Maybe if change to switch statement will correct this?

            alert("Sorry not enough money for sword.");
            equipment();

        }

    }

    else if (button == 'test') {

        if (point >= '500') {

            console.log("Your equipment menu is working. " + button)
            point = point - '500';
            console.log("the point remaining is " + point + ".")
            equipment();

        }

        else {

            alert("Sorry not enough money for test.");
            equipment();

        }

    }

    else {

        console.log("Your equipment menu is not working. " + button)
        alert("Your equipment menu is not working. " + button)

    };

});```

【问题讨论】:

  • 你应该使用数字而不是字符串。
  • 啊,谢谢。我只是注意到了这一点并回过头来说明这一点,但你已经回答了这个问题。

标签: javascript jquery math operators digits


【解决方案1】:

在您的代码中,您在任何地方都使用点作为字符串值,但您正在对其执行操作,例如“-”。

if (point >= '500') {

point = point - '500';

这是故意的吗? 也许将点更改为整数会有所帮助。

point = point - 500;

【讨论】:

  • 不,我只是注意到你不应该在字符串中做数字,而是自己做。这解决了我的很多问题。谢谢。
【解决方案2】:

问题是您正在比较字符串。例如,当您比较'200''50' 时,它实际上会逐个字符地比较,从左到右。所以我们有来自 2 个字符串的前 2 个字符:'2' &lt; '5',这将导致 '200' &gt;= '50' 返回 false

要解决这个问题,只需在比较之前将它们转换为数字。 parseInt()Number() 可以。

【讨论】:

  • 感谢您提供的信息,如果我需要将数字放入字符串中,然后将它们转换为整数而不是先进行整数,我会记住这一点。
猜你喜欢
  • 1970-01-01
  • 2017-04-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-11
  • 2012-09-21
  • 2021-11-18
  • 2020-04-04
相关资源
最近更新 更多