【发布时间】: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