标题:帮改改这程序(模拟银行存取款) 花我半天不知错哪
只看楼主
lyzly520
Rank: 1
等 级:新手上路
帖 子:18
专家分:0
注 册:2007-5-30
 问题点数:0 回复次数:2 
帮改改这程序(模拟银行存取款) 花我半天不知错哪
程序代码:
package com.hb.bank;

import    java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

//import java.text.SimpleDateFormat;
//import java.util.Calendar;

public class Bank {
    static BufferedReader br = new BufferedReader(new InputStreamReader(
            System.in));// 输入流

    private static String name;// 定义用户名

    private static String password;// 定义用户密码

    private static long money;// 定义初始余额

    private long saveMoney;// 定义存款变量

    private long takeMoney;// 定义取款变量

    String saveDate;// 定义存款时间变量

    String takeDate;// 定义取款时间变量

    String choose;// 定义选择菜单的变量

    ArrayList saveTime = new ArrayList();
    //定义动态数组用来存储存款时间
    ArrayList takeTime = new ArrayList();
    //定义动态数组用来存储取款时间
    ArrayList save_Money = new ArrayList();
    //定义动态数组用来存储取款数目
    ArrayList take_Money = new ArrayList();
    //定义动态数组用来存储取款数目

    /**
     * 带参数:账户密码money的构造方法
     */
    public Bank(String name, String password, long money) {
        this.name = name;
        this.password = password;
        this.money = money;
    }

    /**
     * 用户登陆
     */
    public void login() {
        int i = 3;// 定义一个计数器
        // String getName = null;// 初始化输入用户名的字符
        // String getPassword = null;// 初始化输入密码的字符

        // 构造while循环判断输入直到正确
        while (true) {
            System.out.print("请输入用户名:");
            try {
                name = br.readLine();
            } catch (IOException e) {

            }
            System.out.print("请输入密码:");
            try {
                password = br.readLine();
            } catch (IOException e) {

            }
            if (name.equals("admin") && password.equals("admin")) {
                System.out.flush();
                System.out.println("恭喜你,成功登陆!");
                System.out.println("你的余额为" + money + "元");
                break;

            } else {
                i--;// 错误一次 计数器递减
                if (i == 0) {
                    System.out.println("输入超过最大次数!程序退出");
                    System.exit(0);
                    // 3次错误 程序退出
                }
                System.out.println("对不起,您输入的用户名或密码错误,请重新输入:" + "你还有" + i
                        + "次机会");

            }

        }

    }

    /**
     * 构造函数 登陆成功进行操作
     */
    public Bank()  {
        // String choose = null;
        while (true) {
            System.out.println("请选择您要进行的操作:1.存款 2.取款 3.查询 4.退出");
            try {
                choose = br.readLine();// 读取字符
            } catch (IOException e) {

            }

            // 存款
            if (("1") == 0) {
                //
                while (true) {
                    System.out.println("请输入你要存储的金额(注意请输入整数):");
                    try {
                        saveMoney = Long.parseLong(br.readLine());
                    } catch (NumberFormatException e) {
                        System.out.println("输入错误,请输入整数");
                        continue;
                    } catch (IOException e) {

                    }
                    money += saveMoney;

                    // saveDate = new Date();
                    saveDate = getTime();
                    saveTime.add(saveDate);
                    save_Money.add(saveMoney);
                    System.out.println("存款成功!已存入" + saveMoney + "元" + "你的可用余额为"
                            + money + "元");
                    break;
                }
            }

            // 取款
            else if (choose.equals("2")) {
                while (true) {
                    System.out.println("请输入你要取款的金额(注意请输入整数):");
                    try {
                        takeMoney = Long.parseLong(br.readLine());
                    } catch (NumberFormatException e) {
                        System.out.println("输入错误,请输入整数");
                        continue;
                    } catch (IOException e) {

                    }
                    if (!(money < takeMoney)) {
                        money -= takeMoney;// 余额减少
                        System.out.println("取款成功!已取出" + takeMoney + "元."
                                + "你的余额为" + money + "元");

                        // takeDate = new Date();
                        takeDate = getTime();
                        takeTime.add(takeDate);
                        take_Money.add(takeMoney);
                        break;
                    } else {
                        System.out.println("余额不足,请重新输入:");
                        
                    }
                    continue;

                }
            }

            // 输入3为查询   
            else if (("3") == 0) {
                System.out.println("你的余额为" + money + "元;");
                for (int i = 0; i <= saveTime.size(); i++) {   //这里要是登陆后直接按3这段代码怎么处理???请帮忙改改给点意见                    System.out.println("您在" + saveTime.get(i) + "存入"
                            + save_Money.get(i) + "元");
                    System.out.println("您在" + takeTime.get(i) + "取出"
                            + take_Money.get(i)+ "元");
                }
//                for (int i = 0; i <= takeTime.size(); i++) {
//                }
                continue;
            }

            // 输入4为退出
            else if (choose.equals("4")) {
                System.out.println("程序退出");
                System.exit(0);
            }

            // 输入其它的就为不合法,返回重新输入
            else {
                System.out.println("输入无效!请输入合法的");
                continue;
            }

        }
    }

    /**
     * 获得系统当前时间
     */
    public String getTime() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 设置日期格式
        return sdf.format(new Date());// new Date()为获取当前系统时间

    }

    /**
     * 入口函数
     */
    public static void main(String[] args) {

        Bank b = new Bank(name, password, money);
        b.login();// 调用登陆
        new Bank();
    }
}

按3查询时这样出错 查询功能 能正常显示出来
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
    at java.util.ArrayList.RangeCheck(Unknown Source)
    at java.util.ArrayList.get(Unknown Source)
    at com.hb.bank.Bank.<init>(Bank.java:168)
    at com.hb.bank.Bank.main(Bank.java:209)
搜索更多相关主题的帖子: 银行 取款 知错 改改 
2008-05-27 21:55
sunkaidong
Rank: 4
来 自:南京师范大学
等 级:贵宾
威 望:12
帖 子:4496
专家分:141
注 册:2006-12-28
得分:0 
是数组里面没有数据引起异常的
package com.hb.bank;

import
import
import
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

//import java.text.SimpleDateFormat;
//import java.util.Calendar;

 class Bank {
        static BufferedReader br = new BufferedReader(new InputStreamReader(
                        System.in));// 输入流

        private static String name;// 定义用户名

        private static String password;// 定义用户密码

        private static long money;// 定义初始余额

        private long saveMoney;// 定义存款变量

        private long takeMoney;// 定义取款变量
        private int flag=0;

        String saveDate;// 定义存款时间变量

        String takeDate;// 定义取款时间变量

        String choose;// 定义选择菜单的变量

        ArrayList saveTime = new ArrayList(100);
        //定义动态数组用来存储存款时间
        ArrayList takeTime = new ArrayList();
        //定义动态数组用来存储取款时间
        ArrayList save_Money = new ArrayList();
        //定义动态数组用来存储取款数目
        ArrayList take_Money = new ArrayList();
        //定义动态数组用来存储取款数目

        /**
         * 带参数:账户密码money的构造方法
         */
        public Bank(String name, String password, long money) {
                this.name = name;
                this.password = password;
                this.money = money;
        }

        /**
         * 用户登陆
         */
        public void login() {
                int i = 3;// 定义一个计数器
                // String getName = null;// 初始化输入用户名的字符
                // String getPassword = null;// 初始化输入密码的字符

                // 构造while循环判断输入直到正确
                while (true) {
                        System.out.print("请输入用户名:");
                        try {
                                name = br.readLine();
                        } catch (IOException e) {

                        }
                        System.out.print("请输入密码:");
                        try {
                                password = br.readLine();
                        } catch (IOException e) {

                        }
                        if (name.equals("admin") && password.equals("admin")) {
                                System.out.flush();
                                System.out.println("恭喜你,成功登陆!");
                                System.out.println("你的余额为" + money + "元");
                                break;

                        } else {
                                i--;// 错误一次 计数器递减
                                if (i == 0) {
                                        System.out.println("输入超过最大次数!程序退出");
                                        System.exit(0);
                                        // 3次错误 程序退出
                                }
                                System.out.println("对不起,您输入的用户名或密码错误,请重新输入:" + "你还有" + i
                                                + "次机会");

                        }

                }

        }

        /**
         * 构造函数 登陆成功进行操作
         */
        public Bank()  {
                // String choose = null;
                while (true) {
                        System.out.println("请选择您要进行的操作:1.存款 2.取款 3.查询 4.退出");
                        try {
                                choose = br.readLine();// 读取字符
                        } catch (IOException e) {

                        }

                        // 存款
                        if (("1") == 0) {
                                //
                                while (true) {
                                        System.out.println("请输入你要存储的金额(注意请输入整数):");
                                        try {
                                                saveMoney = Long.parseLong(br.readLine());
                                        } catch (NumberFormatException e) {
                                                System.out.println("输入错误,请输入整数");
                                                continue;
                                        } catch (IOException e) {

                                        }
                                        money += saveMoney;

                                        // saveDate = new Date();
                                        saveDate = getTime();
                                        saveTime.add(saveDate);
                                        save_Money.add(saveMoney);
                                        System.out.println("存款成功!已存入" + saveMoney + "元" + "你的可用余额为"
                                                        + money + "元");
                                        break;
                                }
                        }

                        // 取款
                        else if (choose.equals("2")) {
                                while (true) {
                                        System.out.println("请输入你要取款的金额(注意请输入整数):");
                                        try {
                                                takeMoney = Long.parseLong(br.readLine());
                                        } catch (NumberFormatException e) {
                                                System.out.println("输入错误,请输入整数");
                                                continue;
                                        } catch (IOException e) {

                                        }
                                        if (!(money < takeMoney)) {
                                                money -= takeMoney;// 余额减少
                                                System.out.println("取款成功!已取出" + takeMoney + "元."
                                                                + "你的余额为" + money + "元");

                                                // takeDate = new Date();
                                                takeDate = getTime();
                                                takeTime.add(takeDate);
                                                take_Money.add(takeMoney);
                                                flag++;
                                                break;
                                        } else {
                                                System.out.println("余额不足,请重新输入:");
                                               
                                        }
                                        continue;

                                }
                        }

                        // 输入3为查询   
                        else if (("3") == 0) {
                                System.out.println("你的余额为" + money + "元;");
                                if(flag==0)
                                System.out.println("您没有取款纪录!");
                                else
                                for (int i = 0; i < saveTime.size(); i++) { //  这里要是登陆后直接按3这段代码怎么处理???请帮忙改改给点意见                                      
                               System.out.println("您在" + saveTime.get(i) + "存入"
                                                        + save_Money.get(i) + "元");
                                        System.out.println("您在" + takeTime.get(i) + "取出"
                                                        + take_Money.get(i)+ "元");
                                }
//                                for (int i = 0; i <= takeTime.size(); i++) {
//                                }
                                continue;
                        }

                        // 输入4为退出
                        else if (choose.equals("4")) {
                                System.out.println("程序退出");
                                System.exit(0);
                        }

                        // 输入其它的就为不合法,返回重新输入
                        else {
                                System.out.println("输入无效!请输入合法的");
                                continue;
                        }

                }
        }

        /**
         * 获得系统当前时间
         */
        public String getTime() {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 设置日期格式
                return sdf.format(new Date());// new Date()为获取当前系统时间

        }

        /**
         * 入口函数
         */
        public static void main(String[] args) {

                Bank b = new Bank(name, password, money);
                b.login();// 调用登陆
                new Bank();
        }
}

[[it] 本帖最后由 sunkaidong 于 2008-5-27 22:28 编辑 [/it]]

学习需要安静。。海盗要重新来过。。
2008-05-27 22:27
lyzly520
Rank: 1
等 级:新手上路
帖 子:18
专家分:0
注 册:2007-5-30
得分:0 
嗯 OK  谢谢啦...实在非常感谢!

i love java love english love china and love all of you !
2008-05-27 22:59



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-216223-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.396032 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved