JavaScript 简易ATM

利用循环完成ATM相关功能
<!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>Document</title>
</head>
<body>
    <script>
        var cunkuan = 0,
            cun = 0,
            qu = 0;
    do{
     
       var a = prompt('1.存钱\n2.取钱\n3.显示余额\n4.退出');
       switch (a) {
     
        case '1':
            cun = parseInt(prompt('请输入金额'));
            while (cun <= 0) {
     
            alert('请输入正整数:');
            cun = parseInt(prompt('请输入金额'));
            }
            cunkuan += cun;
            alert('你存了' + cun +'元'+ ',' + '总共有' + cunkuan+'元');
            break;
        case '2':
            qu = prompt('请输入金额');
            while (qu > cunkuan) {
     
            alert('余额不足,重新输入');
            qu = parseInt(prompt('请输入金额'));
        }
            cunkuan -= qu;
            alert('已取走' + qu + ',' + '还剩' + cunkuan+'元');
        break;
    case '3':
        alert('余额:' + cunkuan+'元')
        break;
        }
    }
    
    while (a != '4') {
     
        alert('正在退出');
    }
    </script>
</body>
</html>

也可利用其它循环实现

你可能感兴趣的:(javascript)