JS simple ATM case

Insert picture description here
Reference article

The code is different and optimized. The infinite loop can only be realized when the input is less than or equal to. When a value greater than 100 is entered, it prompts that the input is incorrect and no longer loops.

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>简易ATM</title>
		<script>
			var count = 100;
			var step = 100;
			for (; step <= 100 ;) {
    
    
				step = prompt('此网页显示' + '\n' + '请输入您要的操作:' + '\n' + '1.存钱' + '\n' + '2.取钱' + '\n' + '3.显示余额' + '\n' +
					'4.退出');
				step = parseInt(step);
				switch (step) {
    
    
					case 1:
						var s = prompt('请输入您需要存的钱');
						count += parseInt(s);
						alert('您的余额为' + count + '元');
					case 2:
						var q = prompt('请输入您需要取的钱');
						count -= q;
						alert('您的余额为' + count + '元');
						break;
					case 3:
						alert('您的余额为' + count + '元');
						break;
					case 4:
						alert('退出成功');
						break;
					default:
						alert('输入错误');
				}	
			}
		</script>
	</head>
	<body>
	</body>
</html>


For the second optimization, you should write an infinite loop. The step setting is less than 100 meaningless.
But if you want to enter 4, exit successfully and jump out of the dead loop.
Just another thought, the bank ATM machine exits the scene, and it seems to return to the initial page.
In this way, my design meets the requirements,
but because of the technical realization,
I still want to meet a certain condition in the loop statement to break out of the loop.
This problem is left to the future.

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>简易ATM</title>
		<script>
			var count = 100;
			for (;;) {
    
    
				var step = prompt('此网页显示' + '\n' + '请输入您要的操作:' + '\n' + '1.存钱' + '\n' + '2.取钱' + '\n' + '3.显示余额' + '\n' +
					'4.退出');
				step = parseInt(step);
				switch (step) {
    
    
					case 1:
						var s = prompt('请输入您需要存的钱');
						count += parseInt(s);
						alert('您的余额为' + count + '元');
						break;
					case 2:
						var q = prompt('请输入您需要取的钱');
						count -= q;
						alert('您的余额为' + count + '元');
						break;
					case 3:
						alert('您的余额为' + count + '元');
						break;
					case 4:
						alert('退出成功');
						break;
					default:
						alert('输入错误');
				}
			}
		</script>
	</head>
	<body>
	</body>
</html>

Guess you like

Origin blog.csdn.net/qq_41685741/article/details/114680538