方法

来源:互联网 发布:棕榈大道 知乎 编辑:程序博客网 时间:2024/04/29 00:32

        方法调用在编程过程中用处很广,可以提高代码的可读性,也可以是代码重复使用,很方便,今天练习的例子:

import javax.swing.JOptionPane;




public class ATM {



public static void main(String[] args) {
JOptionPane.showMessageDialog(null, "欢迎光临,中国银行");
boolean island=Land();
if(island==false){
JOptionPane.showMessageDialog(null, "输入错误超过三次,吞卡,请你带好证件到银行办理");
System.exit(0);
}
while(true){
String x=JOptionPane.showInputDialog(null,"请选择操作:\n1、取款\n2、存款\n3、查询余额\n4、退卡\n");
int a=Integer.parseInt(x);
switch(a){
case 1:
saveMoney();
break;
case 2:
getMoney();
break;
case 3:
showMoney();
break;
case 4:
System.exit(0);
default:
JOptionPane.showMessageDialog(null, "你的选择有误,请重新选择");
break;
}
}


}

//定义全局变量,并赋值
public static int money=5000;


/*
* 取款

*/
public static void saveMoney(){
String x=JOptionPane.showInputDialog(null,"请输入存款金额:");
int a=Integer.parseInt(x);
if(a>money){
JOptionPane.showMessageDialog(null, "余额不足");
}
else{
money-=a;
int b=JOptionPane.showConfirmDialog(null, "是否查询余额");
if(b==0){
showMoney();
}
}

}


/*
* 存款

*/
public static void getMoney(){
String x=JOptionPane.showInputDialog(null,"请输入存款金额:");
int a=Integer.parseInt(x);
money+=a;
int b=JOptionPane.showConfirmDialog(null, "是否查询余额");
if(b==0){
showMoney();
}

}

/*
* 查询余额

*/
public static void showMoney(){
JOptionPane.showMessageDialog(null, "账户的余额为:"+money);



/*
* 登录

*/
public static boolean Land(){
for(int i=0;i<3;i++){
String code=JOptionPane.showInputDialog(null,"请输入你的帐号:");
String pwd=JOptionPane.showInputDialog(null,"请输入你的密码:");
if(code.equals("123456")&&pwd.equals("123")){
JOptionPane.showMessageDialog(null, "登陆成功");
return true;
}
else{
JOptionPane.showMessageDialog(null, "你输入的帐号或密码有误,请重新输入");
}
}
return false;
}


}


0 0
原创粉丝点击