简单的银行系统,功能有1-取款,2-存款,3-转账,4-查询,5-退出;
java实现
import java.util.Scanner;
/**
* @Author:WangQuan
* @Description
* @Date:Created in 2:54 下午 2020/11/20
* @Modified By:
**/
public class ATM {
private static int[][] acounts = {{123,123,500},{456,456,200},{789,789,1000}};
private static int[] acc;
public static boolean login(int acount,int password){
boolean result = true;
for(int[] acount0:acounts){
if(acount0[0]==acount&&acount0[1]==password){
result = true;
acc = acount0;
System.out.println("登陆成功");
break;
}else {
result = false;
}
}
if(!result){
System.out.println("登陆失败");
}
return result;
}
public static boolean qu_kuan(int money){
boolean type =true;
if(money%100==0&&money>=100&&acc[2]>money){
acc[2] -= money;
System.out.println("取款成功");
System.out.println("账号剩余"+acc[2]);
}else {
System.out.println("账户金额不够或者取款格式有误");
type = false;
}return type;
}
public static void cun_kuan(int money1){
boolean tttp = true;
if(money1%100==0&&money1>=100){
acc[2] += money1;
System.out.println("存款成功");
System.out.println("账户剩余"+acc[2]);}
else {
tttp =false;
}
}
public static void cha_xun(){
System.out.println("账户ID"+acc[0]);
System.out.println("账户余额"+acc[2]);
}
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.println("请输入账户id");
int a = sc.nextInt();
System.out.println("请输入账户密码");
int b = sc.nextInt();
boolean result = login(a,b);
on:
while (result){
System.out.println("1-取款,2-存款,3-转账,4-查询,5-退出");
int i = sc.nextInt();
switch (i){
case 1:
System.out.println("请输入取款金额:");
qu_kuan(sc.nextInt());
break;
case 2:
System.out.println("请输入存款金额:");
cun_kuan(sc.nextInt());
break;
case 3:
System.out.println("请输入需要转入的账户id:");
int s = sc.nextInt();
boolean ttype=false;
for (int i_2=0;i_2<3;i_2++) {
ttype=true;
if (s == acounts[i_2][0]) {
System.out.println("请输入想要转入的金额");
int number = sc.nextInt();
if (!qu_kuan(number)) {
acounts[i_2][2] += number;
System.out.println(acounts[i_2][2]);
} else {
ttype =false;
}
}
}if (!ttype){
System.out.println("卡里钱不够了");
// else {
// System.out.println("账户id有问题");
// break ;
// }
}
break;
case 4:
System.out.println("进入查询");
ATM.cha_xun();
break;
case 5:
break on;
default:
System.out.println("请输入正确数字");
}
}
}
}
代码是这样的,晚上前补充今日学习细节和代码中解决bug的过程;1.on和break on : break直接跳到指定位置,可以跳出多层循环,比break和continue要可操控。
2.方法调用,有return,写返回值类型;
无return 写void;
3.
for(int[] acount0:acounts)是从accounts的第一个数组循环到最后一个;