ATM机(高阶版)
规则:在数组里面存入多个用户,来使用判断多个用户的登录且只有三次机会
法1:
const re = require('readline-sync');
let arr = [
'1', '1', 100,
'2', '2', 200,
'3', '3', 300
];
//法1:
let username, pwd, f = true; //f控制业务选择的循环
for (let k = 3; k > 0; k--) {//控制登录次数
console.log('请输入用户名:');
username = re.question();
// let username = '3';
for (let index = 0; index < arr.length; index++) {
if (username == arr[index] && (index % 3 == 0 || index == 0)) {
console.log('请输入密码:');
pwd = re.question();
// let pwd = '2';
if (pwd == arr[index + 1]) {
console.log(`${username}登录成功,余额为${arr[index + 2]}`);
f=true;
while (f) {
console.log(`请选择操作:1:存钱,2:取钱,3:查询余额,0:退出:`);
let num = re.question() - 0;
switch (num) {
case 1:
console.log('请输入存钱金额:');
let jin = re.question() - 0;
arr[index+2]+=jin;
console.log(`存钱成功,余额为:${arr[index+2]}`);
break;
case 2:
console.log('请输入取钱金额:');
let save = re.question() - 0;
if (save <= arr[index+2]) {
arr[index+2] -= save;
console.log(`取钱成功,余额为:${arr[index+2]}`);
} else {
console.log(`余额不足`);
}
break;
case 3:
console.log(`余额为:${arr[index+2]}`);
break;
case 0:
f=false;
console.log("欢迎下次光临");
break;
default:
console.log('你输入的数字不是查询业务,请重新输入!!');
}
}
k-=3;
}
else {
if (k == 1) {
console.log('多次输入错误,请联系客服');
} else{
console.log(`密码错误,你还有${k}次机会`);
}
f=false;
}
} else {
index += 2;
}
}
}
法2:
const re = require('readline-sync');
let arr = [
'1', '1', 100,
'2', '2', 200,
'3', '3', 300
];
let username, pwd, f = true;
for (let k = 3; k > 0; k--) {//控制登录次数
console.log('请输入用户名:');
username = re.question();
// let username = '3';
for (let index = 0; index < arr.length; index++) {
if (username == arr[index] && (index % 3 == 0 || index == 0)) {
console.log('请输入密码:');
pwd = re.question();
// let pwd = '2';
if (pwd == arr[index + 1]) {
console.log(`${username}登录成功,余额为${arr[index + 2]}`);
while (f) {
console.log(`请选择操作:1:存钱,2:取钱,3:查询余额,0:退出:`);
let num = re.question() - 0;
switch (num) {
case 1:
console.log('请输入存钱金额:');
let jin = re.question() - 0;
arr[index + 2] += jin;
console.log(`存钱成功,余额为:${arr[index + 2]}`);
break;
case 2:
console.log('请输入取钱金额:');
let save = re.question() - 0;
if (save <= arr[index + 2]) {
arr[index + 2] -= save;
console.log(`取钱成功,余额为:${arr[index + 2]}`);
} else {
console.log(`余额不足`);
}
break;
case 3:
console.log(`余额为:${arr[index + 2]}`);
break;
case 0:
f = false;
console.log("欢迎下次光临");
index=arr.length;
break;
default:
console.log('你输入的数字不是查询业务,请重新输入!!');
}
}
k -= 3;
}
else {
if (k == 1) {
console.log('多次输入错误,请联系客服');
} else {
console.log(`密码错误,你还有${k}次机会`);
}
break;
}
} else if(index==arr.length-1){
if (k == 1) {
console.log('机会没了,请联系客服');
} else{
console.log(`账户输入错误,你还有${k}次机会`);
}
}
}
}
扩展内容(多维数组控制用户)
const readline = require('readline-sync');
let accArr = [
["1", "1", 1000],
["2", "2", 2000]
]
let state = true;
let count = 3;
while (count) {
console.log('请输入账号');
let inputname = readline.question();
console.log('请输入密码');
let inputpwd = readline.question();
for (let index = 0; index < accArr.length; index++) {
if (inputname == accArr[index][0]) {
if (inputpwd == accArr[index][1]) {
console.log(`登录成功!欢迎你光临xx银行`);
console.log(`余额为:${accArr[index][2]}`);
count-=3;
break;
}
} else if(index==accArr.length-1){
count--;
console.log('登陆失败,没找到用户名');
}
}
}