21xrx.com
2024-09-17 04:10:38 Tuesday
登录
文章检索 我的文章 写文章
我的JavaScript简易ATM
2023-06-10 13:39:45 深夜i     --     --
JavaScript ATM 代码

最近我在学习JavaScript,于是我想写一个简单的ATM程序来练习一下自己的代码能力。这个ATM程序可以进行基本的存款、取款和查询余额操作。下面是我编写的JavaScript代码:

script

let balance = 1000;

function deposit(amount) {

 balance += amount;

 console.log(`You have deposited $${amount}. Your current balance is $${balance}.`);

}

function withdraw(amount) {

 if (balance >= amount) {

  balance -= amount;

  console.log(`You have withdrawn $${amount}. Your current balance is $${balance}.`);

 } else {

  console.log(`Insufficient funds. Your current balance is $${balance}.`);

 }

}

function checkBalance() {

 console.log(`Your current balance is $${balance}.`);

}

我定义了三个函数:`deposit`、`withdraw`和`checkBalance`。`deposit`函数用于存款,`withdraw`函数用于取款,`checkBalance`函数用于查询余额。在这个程序中,我使用了一个变量 `balance` 来存储用户的余额。初始余额为1000元。

让我们测试一下这个ATM程序。首先我要存入100元:

script

deposit(100);

输出: `You have deposited $100. Your current balance is $1100.`

接下来我要取出500元:

script

withdraw(500);

输出: `You have withdrawn $500. Your current balance is $600.`

最后,我要查询我的余额:

script

checkBalance();

输出: `Your current balance is $600.`

看起来我的ATM程序是正常运行的!当然,实际情况下一个真正的ATM程序会更加复杂,它需要与银行的数据库进行交互等等。但是,作为一个练习项目,这个简单的ATM程序已经足够好了。

通过这个项目,我巩固了一些JavaScript编程方面的知识,例如变量、函数和条件语句。下一步我将尝试编写更复杂的项目,以进一步提高我的编程能力。

  
  

评论区

{{item['qq_nickname']}}
()
回复
回复
    相似文章