本文實例為大家分享了java實現atm取款機程序的具體代碼,供大家參考,具體內容如下
對象說明:
功能:該程序的功能為實現模擬銀行atm自動取款機取款,存款,查詢余額,轉賬等功能,只是完成了基本的功能。
思路:第一、登錄判斷,密碼限制三次,使用for循環。第二、成功登錄,選擇相應的功能,使用switch語句。第四、實現功能的反復循環操作,因為次數不確定,使用while結構。第五、對每個功能模塊進行填充完善。
代碼展示:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
|
import java.util.scanner; public class bankatm { public static void main(string[] args) { scanner input = new scanner(system.in); string cardnum = "6228123123" ; // 卡號 int pwd = 888888 ; // 密碼 boolean flag = true ; // 聲明布爾類型變量 double surplus = 1000 ; // 余額 // 界面 system.out.println( "---------歡迎使用工商銀行atm機---------" ); /** 用于進行登錄的次數限制止 **/ for ( int i = 1 ; i <= 3 ; i++) { system.out.println( "請插入您的銀行卡:" ); string inputcard = input.next(); system.out.println( "請輸入您的密碼:" ); int inputpwd = input.nextint(); // 驗證賬號和密碼 if (inputcard.equals(cardnum) && inputpwd == pwd) { flag = true ; break ; } else { if (i <= 2 ) { system.out.println( "對不起,密碼輸入不正確,你還有" + ( 3 - i) + "次機會!" ); } else { system.out.println( "對不起,您的卡已被鎖定!" ); break ; } flag = false ; } } /** 登錄成功后選擇功能 */ if (flag) { char answer = 'y' ; while (answer == 'y' ) { system.out.println( "請選擇功能:1.取款 2.存款 3.查詢余額 4.轉賬 5.退出" ); int choice = input.nextint(); switch (choice) { case 1 : // 執行取款操作 system.out.println( "--->取款" ); system.out.println( "請輸入取款金額:" ); double getmoney = input.nextdouble(); if (getmoney > 0 ) { if (getmoney <= surplus) { if (getmoney % 100 == 0 ) { system.out.println( "請取走您的鈔票!余額為¥" + (surplus - getmoney)); } else { system.out.println( "對不起,不能取零錢!" ); } } else { system.out.println( "對不起,余額不足!" ); } } else { system.out.println( "請輸入正確的金額:" ); } break ; case 2 : // 執行存款操作 system.out.println( "--->存款" ); system.out.println( "請把鈔票整理后放入存鈔口:" ); double savemoney = input.nextdouble(); if (savemoney > 0 && savemoney <= 10000 ) { if (savemoney % 100 == 0 ) { surplus += savemoney; system.out.println( "存款成功!余額為¥" + surplus); } else { double backmoney = savemoney % 100 ; surplus = savemoney + surplus - backmoney; system.out.println( "存款成功!余額為¥" + surplus); system.out.println( "請取走零錢¥" + backmoney); } } else if (savemoney > 10000 ) { system.out.println( "一次最多存入一萬元,請分批存入!" ); } else { system.out.println( "存入的鈔票是假鈔,無效沒收!" ); } break ; case 3 : // 執行查詢余額 system.out.println( "--->查詢余額" ); system.out.println( "您卡上的余額是:" + surplus); break ; case 4 : // 執行轉賬操作 system.out.println( "--->轉賬" ); system.out.println( "請輸入轉賬金額:" ); double gomoney = input.nextdouble(); // 轉賬金額 if (gomoney > 0 ) { if (gomoney <= surplus) { system.out.println( "轉賬成功!余額為¥" + (surplus - gomoney)); } else { system.out.println( "對不起,請確保卡上有足夠的余額!" ); } } else { system.out.println( "轉賬失敗!請輸入正確的金額:" ); } break ; case 5 : // 執行退出操作 // system.out.println("--->退出"); system.out.println( "謝謝您的使用!" ); return ; default : system.out.println( "對不起,您選擇的功能有誤!" ); break ; } // switch end system.out.println( "繼續嗎?y/n" ); answer = input.next().charat( 0 ); } // while end system.out.println( "謝謝您的使用!" ); } } } |
效果截圖:
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://blog.csdn.net/weixin_42517667/article/details/82049249