Blogger Information
Blog 3
fans 0
comment 0
visits 1380
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
java script练习-- 简易取款机
玛琳
Original
437 people have browsed it

需求

  • 初始金额为100元
  • 如果存钱,就累加并提示余额
  • 如果取钱,就减去并返回余额
  • 如果查询,就返回余额
  • 如果退出,弹出退出信息并关闭

效果


代码部分

  1. <script>
  2. var money = 100; //初始余额
  3. var value = 0; //输入金额
  4. do {
  5. choice = prompt("--------欢迎光临-------\n请输入数字:\n1.存钱\n2.取钱\n3.显示余额\n4.退出"); // 弹出提示输入框
  6. if (choice == "1") { //存钱
  7. value = parseInt(prompt("请输入存款金额:"));
  8. if (isNaN(value) || value < 0) { //输入值不是数字或者小于0,就提示并退出当前循环
  9. alert("输入错误!请重新输入!");
  10. continue;
  11. } else {
  12. money += value; // 余额等于当前余额加上输入金额
  13. alert("可用余额为:" + money + "元!");
  14. continue;
  15. }
  16. } else if (choice == "2") { //取钱
  17. if (money <= 0) {
  18. alert("可用余额为0元!"); // 先检查可用余额,不足提示
  19. continue;
  20. } else {
  21. value = parseInt(prompt("请输入取款金额:"));
  22. if (isNaN(value) || value < 0) {
  23. alert("输入错误!请重新输入!");
  24. continue;
  25. } else if (value > money) { //取款金额大于当前余额,并提示
  26. alert("出错了:可用余额为:" + money + "元!")
  27. continue;
  28. } else {
  29. money -= value; // 余额等于当前余额加上输入金额
  30. alert("可用余额为:" + money + "元");
  31. continue;
  32. }
  33. }
  34. } else if (choice == "3") { //查询余额
  35. alert("可用余额为:" + money + "元");
  36. continue;
  37. } else if (choice != "4") { //退出
  38. alert("输入错误!请重新输入!");
  39. continue;
  40. }
  41. } while (choice != "4");
  42. alert("谢谢使用,再见!")
  43. </script>
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post