public class BankCard {
String number;
long key;
double money;
public BankCard(String num, int ke, int mon) {
number=num;
key=ke;
money=mon;
}
}
import java.util.Scanner;
public class ATM {
private static BankCard card1;
private static BankCard card2;
public static void main(String args[]){
BankCard card1=new BankCard("201811",123456,5000);
BankCard card2=new BankCard("201911",654321,8000);
操作ATM();
Scanner r=new Scanner(System.in);
int i=r.nextInt();
switch(i){
case 1:
case 2:
case 3:
case 4:
case 5:
default:
}
}
/*****************************************************************************************/
private static void 输入密码(BankCard card)
{
System.out.println("请输入密码:");
Scanner r=new Scanner(System.in);
long mm=r.nextLong();
while(mm!=card.key){
System.out.println("密码不正确,是否请重新输入:"
+"1:是 其他数字:否");
int sf=r.nextInt();
if(sf!=1)
break;
long xmm=r.nextLong();
mm=xmm;
}
if(mm==card.key)
操作界面();
}
/*****************************************************************************************/
private static void 操作界面(){
System.out.println("**************\n"
+"**1:查询余额**\n"
+"**2:存钱******\n"
+"**3:取钱******\n"
+"**4:转账******\n"
+"**5:退出******\n"
+"请输入想要进行的操作(输入数字)");
}
/*****************************************************************************************/
private static void 操作ATM(){
System.out.println("请选择需要插入的银行卡(输入数字):\n"
+"1:card1 2:card2");
Scanner r=new Scanner(System.in);
int j=r.nextInt();
switch(j){
case 1:输入密码(card1);
case 2:输入密码(card2);
default:
System.out.println("抱歉!此卡不存在!");
操作ATM();
}
}
/*****************************************************************************************/
private static void 查询余额(BankCard card){
System.out.println("当前余额为:"+card.money);
}
/*****************************************************************************************/
private static void 存钱(BankCard card){
Scanner r=new Scanner(System.in);
double x=r.nextDouble();
card.money=card.money+x;
查询余额(card);
}
/*****************************************************************************************/
private static void 取钱(BankCard card){
Scanner r=new Scanner(System.in);
double x=r.nextDouble();
if(x>card.money){
System.out.println("您的余额已不足!");
}
else
card.money=card.money-x;
}
/*****************************************************************************************/
private static void 转账(BankCard card){
Scanner r=new Scanner(System.in);
double x=r.nextDouble();
if(x>card.money){
System.out.println("您的余额已不足!");
}
else{
card.money=card.money-x;
System.out.println("转账成功!");
}
}
/*****************************************************************************************/
}
在运行输入密码后,程序就无法运行了,显示如下:
请选择需要插入的银行卡(输入数字):
1:card1 2:card2
1
请输入密码:
123456
Exception in thread "main" java.lang.NullPointerException
at ATM.输入密码(ATM.java:34)
at ATM.操作ATM(ATM.java:65)
at ATM.main(ATM.java:10)