作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个银行脚本。当用户存钱时我想确保它是一个正整数。如果不是的话,我想把他们踢出去。
这是我的代码:
<section id="pigBox">
<img src="images/pig.png" />
<label>Balance: </label><input type="text" id="balance" />
<button id="deposit"> Deposit </button>
<button id="withdraw"> Withdraw </button>
</section><!-- end of pigBox-->
document.getElementById('balance').value = "1000"
var balance = document.getElementById('balance').value;
var deposit = document.getElementById('deposit');
var withdraw = document.getElementById('withdraw');
deposit.addEventListener('click', depositCash);
withdraw.addEventListener('click', withdrawCash);
function depositCash() {
var depositAmt = prompt('How much would you like to deposit?');
if(depositAmt != Number(depositAmt) && depositAmt) {
return alert('Please enter a valid integer.');
}
balance = Number(balance) + Number(depositAmt);
document.getElementById('balance').value = balance;
}
function withdrawCash() {
var withdrawAmt = prompt('How much you you like to withdraw?');
if(withdrawAmt != Number(withdrawAmt)) {
return alert('Please enter a valid integer.');
}
balance = Number(balance) - Number(withdrawAmt);
document.getElementById('balance').value = balance;
}
我尝试使用..
else if(Number(depositAmt) < 0) {
return alert('please enter a valid integer.');
}
但这行不通。我做错了什么?
谢谢大家!
最佳答案
只需检查
if (depositAmt <= 0) {
return alert('Please enter a positive integer.');
}
将 if
循环重写为 if
- else if
- else
以避免检查所有条件。
if (depositAmt != Number(depositAmt) && depositAmt) {
return alert('Please enter a valid integer.');
} else if (depositAmt <= 0) {
return alert('Please enter a positive integer.');
} else {
balance = Number(balance) + Number(depositAmt);
document.getElementById('balance').value = balance;
}
document.getElementById('balance').value = "1000"
var balance, deposit, withdraw;
balance = document.getElementById('balance').value;
deposit = document.getElementById('deposit');
withdraw = document.getElementById('withdraw');
deposit.addEventListener('click', depositCash);
withdraw.addEventListener('click', withdrawCash);
function depositCash() {
var depositAmt;
depositAmt = prompt('How much would you like to deposit?');
if (depositAmt != Number(depositAmt) && depositAmt) {
return alert('Please enter a valid integer.');
} else if (depositAmt <= 0) {
return alert('Please enter a positive integer.');
} else {
balance = Number(balance) + Number(depositAmt);
document.getElementById('balance').value = balance;
}
}
function withdrawCash() {
var withdrawAmt;
withdrawAmt = prompt('How much you you like to withdraw?');
if (withdrawAmt != Number(withdrawAmt)) {
return alert('Please enter a valid integer.');
}
balance = Number(balance) - Number(withdrawAmt);
document.getElementById('balance').value = balance;
}
<section id="pigBox">
<img src="images/pig.png" />
<label>Balance: </label><input type="text" id="balance" />
<button id="deposit"> Deposit </button>
<button id="withdraw"> Withdraw </button>
</section>
<!-- end of pigBox-->
关于javascript - 检查负数的 If 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43362216/
我是一名优秀的程序员,十分优秀!