当前位置: 代码迷 >> java >> 在对象数组中找不到对象
  详细解决方案

在对象数组中找不到对象

热度:91   发布时间:2023-07-31 11:50:26.0

我有一个Banking []数组,其限制为100(101)个对象,一个用户创建一个帐户,并且当他们要登录底部运行的小方法来查找时,他们的帐户作为对象插入到数组中他们的帐户,但是它永远找不到对象。 我不确定为什么,有什么想法吗?

public static void main(String[] args) {
        Banking[] uaccounts;
        uaccounts = new Banking[100]; // setting up array
        int arrcount = 0; // setting counter for array
        int menu = 1;   // Creating a variable to loop menu
        while (menu == 1) {  //Creating the loop for the menu
            System.out.println("|=================================================|");     // drawing the menu
            System.out.println("|                                                 |");
            System.out.println("|                     MAIN MENU                   |");
            System.out.println("|  1. Create Account (BANK MANAGER RESTRICTED)    |");
            System.out.println("|  2. Check Account Balance                       |");
            System.out.println("|  3. Deposit Into Account                        |");
            System.out.println("|  4. Withdraw From Account                       |");
            System.out.println("|  5. Quit                                        |");
            System.out.println("|                                                 |");
            System.out.println("|=================================================|");

            int uinput = Integer.parseInt(JOptionPane.showInputDialog("Please select an option from above(1 - 4):"));
            if (uinput == 1) {    // initiating menu option 1
                String full_name = JOptionPane.showInputDialog("Please enter clients full name:");     // creating some variables
                String address = JOptionPane.showInputDialog("Please enter the clients address:");
                String password = JOptionPane.showInputDialog("Please enter the clients password:");
                int accid = RandomInt(100000, 999999);
                System.out.println("Customers Account ID:" + accid);
                double balance = Double.parseDouble(JOptionPane.showInputDialog("Please enter the client's starting balance:"));
                uaccounts[arrcount] = new Banking(full_name, address, accid, balance, password); // creating objectg in array
                arrcount = arrcount + 1; //adding to counter for next time

            }
            if (uinput == 2) {     // initiating menu option 2
                int acc_id_input = Integer.parseInt(JOptionPane.showInputDialog("Please enter your account ID:"));
                String password_input = JOptionPane.showInputDialog("Please enter your password:");
                int arrcount2 = arrcount - 1;
                LoginUser(acc_id_input, password_input, uaccounts, arrcount2);
            }





public static void LoginUser(int account_id, String password, Banking[] uaccounts, int arrcount) {
        boolean found = false;
        int accIndex = 0;
        for (int i = 0; i < arrcount - 1; i++) {
            if ((uaccounts[i].accid == account_id) && (uaccounts[i].password.equals(password))) {
                found = true;
                accIndex = i;
                break;
            } else {
                found = false;
            }
        }

我在上面的代码中做了很少的更改,并且工作正常。 我必须使用Math.random()而不是RendomInt()。 而且我用过useraccounts.length而不是arcount变量。

public class Test {
    public static void main(String[] args) {
        Banking[] uaccounts;
        uaccounts = new Banking[100]; // setting up array
        int arrcount = 0; // setting counter for array
        int menu = 1; // Creating a variable to loop menu
        while (menu == 1) { // Creating the loop for the menu
            System.out.println("|=================================================|"); 
            System.out.println("|                                                 |");
            System.out.println("|                     MAIN MENU                   |");
            System.out.println("|  1. Create Account (BANK MANAGER RESTRICTED)    |");
            System.out.println("|  2. Check Account Balance                       |");
            System.out.println("|  3. Deposit Into Account                        |");
            System.out.println("|  4. Withdraw From Account                       |");
            System.out.println("|  5. Quit                                        |");
            System.out.println("|                                                 |");
            System.out.println("|=================================================|");

            int uinput = Integer.parseInt(JOptionPane.showInputDialog("Please select an option from above(1 - 4):"));
            if (uinput == 1) { // initiating menu option 1
                String full_name = JOptionPane.showInputDialog("Please enter clients full name:"); // creating some
                                                                                                    // variables
                String address = JOptionPane.showInputDialog("Please enter the clients address:");
                String password = JOptionPane.showInputDialog("Please enter the clients password:");
                int accid = (int) Math.random();// t(100000, 999999);
                System.out.println("Customers Account ID:" + accid);
                double balance = Double
                        .parseDouble(JOptionPane.showInputDialog("Please enter the client's starting balance:"));
                uaccounts[arrcount] = new Banking(full_name, address, accid, balance, password); // creating objectg in
                                                                                                    // array
                arrcount = arrcount + 1; // adding to counter for next time

            }
            if (uinput == 2) { // initiating menu option 2
                int acc_id_input = Integer.parseInt(JOptionPane.showInputDialog("Please enter your account ID:"));
                String password_input = JOptionPane.showInputDialog("Please enter your password:");
                int arrcount2 = arrcount - 1;
                System.out.println("found" + LoginUser(acc_id_input, password_input, uaccounts, arrcount2));
            }

        }
    }

    public static boolean LoginUser(int account_id, String password, Banking[] uaccounts, int arrcount) {
        boolean found = false;
        int accIndex = 0;
        for (int i = 0; i < uaccounts.length; i++) {
            if ((uaccounts[i].accid == account_id) && (uaccounts[i].pass.equals(password))) {
                found = true;
                accIndex = i;
                break;
            } else {
                found = false;
            }
        }
        return found;
    }
}
  相关解决方案