需求:用户可以选择存钱、取钱、查看余额和退出。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>ATM取款机</title>
</head>
<body>
<script>
//1. 开始循环
//3. 准备一个总的金额
let money = 100;
while(true)
{
//+号将string类型转为number
let re = +prompt(`
请你选择操作:
1. 存钱
2. 取钱
3. 查看余额
4. 退出
`);
//2. 如果用户输入的4,则退出循环,break
if(re === 4)
{
break;
}
//4. 根据输入做操作
switch(re)
{
case 1:
//存钱 注意:+号将string类型转为number类型
let save = +prompt('请输入存款金额:');
money = money + save;
break;
case 2:
//取钱
let out = +prompt('请输入取款金额:');
money = money - out;
break;
case 3:
//查看余额
alert(`您的银行卡余额为${money}`);
break;
}
}
</script>
</body>
</html>
运行结果: