@[TOC](JavaScript 逻辑实训题(ATM逻辑处理))
var users = [
{
card: '1001',
pass: '1001',
money: 100
}, {
card: '1002',
pass: '1002',
money: 1000
}, {
card: '1003',
pass: '1003',
money: 500
}
];
var isLogin = false;
var id = null;
var m;
function loginCount(n) {
n = n || 3
var errCount = 0;
for (var i = 0; i < n; i++) {
login()
if (isLogin) {
break;
} else {
errCount++;
alert('你还剩余' + (n - errCount) + '次机会')
}
}
}
function login() {
var card = prompt('请输入账号');
var pwd = prompt('qingshurumima');
for (i = 0; i < users.length; i++) {
if (users[i].card === card && users[i].pass === pwd) {
isLogin = true;
id = i;
menu();
return;
}
}
alert('账号密码错误');
isLogin = false;
}
function menu() {
var a = prompt('请选择您的操作:\n 1-查询余额\n 2-存钱\n 3-取款\n 4-车专账\n 5-退出')
switch (a) {
case '1':
return cha();
case '2':
return cun();
case '3':
return qu();
case '4':
return zhuan();
case '5':
return quit();
default:
alert('1~5')
menu();
}
}
function cha() {
alert(users[id].money)
menu();
}
function cun() {
var card = parseInt(prompt('请输入要存的金额'));
users[id].money += card;
alert('存钱成功');
return menu();
}
function qu() {
isMore();
users[id].money -= m;
alert('取钱成功');
return menu();
}
function isMore() {
m = parseInt(prompt('请输入金额'));
if (m > users[id].money) {
alert('超额了');
isMore();
}
return
}
function zhuan() {
var card = prompt('请输入要转账的账号');
for (var i = 0; i < users.length; i++) {
if (card === users[i].card && card !== users[id].card) {
isMore();
users[id].money -= m;
users[i].money += m;
alert('转账成功');
return menu();
}
}
alert('请输入正确的账号');
zhuan();
}
function quit() {
isLogin = false;
id = null;
loginCount();
}
login();
个人测试暂无发现BUG 欢迎指正 水仙花数 重点运用-函数
|