default998 2023-01-30 03:18 采纳率: 87.5%
浏览 90
已结题

JAVA——如何使用对象数组存取数据

如何解决:
// 问题1:(registe方法) 退出循环重新加载菜单会导致索引值清零,新数据覆盖旧数据,user[1]和user[2]为null
// 问题2:(login方法) if语句和else if语句从未被执行

(更新:用static定义数组的方式可以解决问题,不过我还想试试能不能在不使用static数组的情况下完成这个功能,参考大家的建议做了一些改动,结果又出现了新的问题
合并了choice和registe方法,问题1疑似解决了
问题2依旧存在
问题3:从菜单执行到login方法后会跳转去执行registe方法中的语句,可是我并没有在login方法中设置registe方法的入口,这是怎么回事呢?)

```java
public class question9 {
    public static int cnt = 0;//注册次数
        public static int index = 0;//数组索引

    public static void main(String[] args) {
        User[] user = new User[3];
        registe(user);
    }

        public static void registe(User[] user) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("1.注册 2.登陆 3.退出");
        int choice = scanner.nextInt();

        if (choice == 1) {
            while (cnt != user.length) {
                user[index] = new User(user);
                System.out.println("用户名:");
                user[index].setUsername(scanner.next());
                System.out.println("密码:");
                user[index].setPassword(scanner.next());
                System.out.println("出生日期:");
                user[index].setBirthdate(scanner.next());
                System.out.println("爱好:");
                user[index].setHobbies(scanner.next());
                // continue;
                cnt++;
                index++;
                registe(user);
            }
//执行login方法后,无论用户名和密码如何设置,会跳转到这个位置继续执行
            System.out.println("注册人数已满,禁止注册!");
            registe(user);
        } else if (choice == 2) {
            login(user);
        } else {
            System.exit(0);
        }
    }

    public static void login(User[] user) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入用户名:");
        String userlog = scanner.next();
        System.out.println("请输入密码:");
        String passlog = scanner.next();
        // System.out.println(userlog + "..." + passlog);

                 //测试
                //System.out.println(user[0]);//执行成功
        //System.out.println(user[1]);//执行成功
        //System.out.println(user[2]);//执行成功
      //跳转到registe方法中choice==1 的 while循环的下面继续执行

                //依旧执行失败
        // 问题2:if语句和else if语句从未被执行
        if (userlog.equals(user[0].getUsername()) && passlog.equals(user[0].getPassword())) {
            user[0].toString();
        } else if (userlog.equals(user[1].getUsername()) && passlog.equals(user[1].getPassword())) {
            user[1].toString();

        } else if (userlog.equals(user[2].getUsername()) && passlog.equals(user[2].getPassword())) {
            user[2].toString();
        } else {
            System.out.println("用户名或密码错误!");// 只有本行能正常执行
        }

        // 测试
//        String s1 = "1";
//        System.out.println(s1.equals(userlog));
//        System.out.println(s1.equals(user[0].getUsername()));
//        System.out.println(userlog.equals(user[0].getUsername()));
//        System.out.println(passlog.equals(user[0].getPassword()));
    }
}

class User {
    private String username;
    private String password;
    private String birthdate;
    private String hobbies;

    /**
     * 
     */
    public User() {
        super();
    }

    /**
     * @param user
     */
    public User(User[] user) {
        // TODO Auto-generated constructor stub
    }

    /**
     * @param username
     * @param password
     * @param birthdate
     * @param hobbies
     */
    public User(String username, String password, String birthdate, String hobbies) {
        super();
        this.username = username;
        this.password = password;
        this.birthdate = birthdate;
        this.hobbies = hobbies;
    }

    /**
     * @return the username
     */
    public String getUsername() {
        return username;
    }

    /**
     * @param username the username to set
     */
    public void setUsername(String username) {
        this.username = username;
    }

    /**
     * @return the password
     */
    public String getPassword() {
        return password;
    }

    /**
     * @param password the password to set
     */
    public void setPassword(String password) {
        this.password = password;
    }

    /**
     * @return the birthdate
     */
    public String getBirthdate() {
        return birthdate;
    }

    /**
     * @param birthdate the birthdate to set
     */
    public void setBirthdate(String birthdate) {
        this.birthdate = birthdate;
    }

    /**
     * @return the hobbies
     */
    public String getHobbies() {
        return hobbies;
    }

    /**
     * @param hobbies the hobbies to set
     */
    public void setHobbies(String hobbies) {
        this.hobbies = hobbies;
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        return "User [username=" + username + ", password=" + password + ", birthdate=" + birthdate + ", hobbies="
                + hobbies + "]";
    }
}


```

  • 写回答

4条回答 默认 最新

  • 小飞LOVE霞 2023-01-30 10:07
    关注
    
    package csdn20230130;
    
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Scanner;
    
    /**
     * @Description
     * @Author wangFei
     * @Date 2023/1/30 9:36
     * @Version 1.0
     **/
    public class question9 {
        public static int cnt = 3;
        static List<User> list = new ArrayList<>();
    
        public static void main(String[] args) {
            choice(list);
        }
    
        public static void choice(List<User> list) {
            System.out.println("1.注册 2.登陆 3.退出");
            Scanner scanner = new Scanner(System.in);
            int choice = scanner.nextInt();
             System.out.println(list);
            if (choice == 1) {
                registe(list);
            } else if (choice == 2) {
                login();
            } else {
                System.exit(0);
            }
        }
    
        public static void registe(List<User> list) {
            Scanner scanner = new Scanner(System.in);
    
            User user = new User();
            System.out.println("用户名:");
            user.setUsername(scanner.next());
            System.out.println("密码:");
            user.setPassword(scanner.next());
            ;
            System.out.println("出生日期:");
            user.setBirthdate(scanner.next());
            ;
            System.out.println("爱好:");
            user.setHobbies(scanner.next());
    
    
            // 问题1:退出循环重新加载菜单会导致索引值清零,新数据覆盖旧数据,user[1]和user[2]为null
            if (cnt > list.size()) {
                list.add(user);
                choice(list);
            } else {
                System.out.println("注册人数已满,禁止注册!");
                choice(list);
            }
        }
    
        public static void login() {
            Scanner scanner = new Scanner(System.in);
            System.out.println("请输入用户名:");
            String userlog = scanner.next();
            System.out.println("请输入密码:");
            String passlog = scanner.next();
    
            // 问题2:if语句和else if语句从未被执行
            for (User u : list) {
                String username = u.getUsername();
                String password = u.getPassword();
                if (userlog.equals(username) && passlog.equals(password)) {
                    System.out.println("登录成功,用户:" + u);
                }
            }
            choice(list);
            // 测试
    //        String s1 = "1";
    //        System.out.println(s1.equals(userlog));
    //        System.out.println(s1.equals(user[0].getUsername()));
    //        System.out.println(userlog.equals(user[0].getUsername()));
    //        System.out.println(passlog.equals(user[0].getPassword()));
        }
    
    }
    
    class User {
        private String username;
        private String password;
        private String birthdate;
        private String hobbies;
    
        /**
         *
         */
        public User() {
            super();
        }
    
        /**
         * @param user
         */
        public User(User[] user) {
            // TODO Auto-generated constructor stub
        }
    
        /**
         * @param username
         * @param password
         * @param birthdate
         * @param hobbies
         */
        public User(String username, String password, String birthdate, String hobbies) {
            super();
            this.username = username;
            this.password = password;
            this.birthdate = birthdate;
            this.hobbies = hobbies;
        }
    
        /**
         * @return the username
         */
        public String getUsername() {
            return username;
        }
    
        /**
         * @param username the username to set
         */
        public void setUsername(String username) {
            this.username = username;
        }
    
        /**
         * @return the password
         */
        public String getPassword() {
            return password;
        }
    
        /**
         * @param password the password to set
         */
        public void setPassword(String password) {
            this.password = password;
        }
    
        /**
         * @return the birthdate
         */
        public String getBirthdate() {
            return birthdate;
        }
    
        /**
         * @param birthdate the birthdate to set
         */
        public void setBirthdate(String birthdate) {
            this.birthdate = birthdate;
        }
    
        /**
         * @return the hobbies
         */
        public String getHobbies() {
            return hobbies;
        }
    
        /**
         * @param hobbies the hobbies to set
         */
        public void setHobbies(String hobbies) {
            this.hobbies = hobbies;
        }
    
        /*
         * (non-Javadoc)
         *
         * @see java.lang.Object#toString()
         */
        @Override
        public String toString() {
            return "User [username=" + username + ", password=" + password + ", birthdate=" + birthdate + ", hobbies="
                    + hobbies + "]";
        }
    }
    
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

问题事件

  • 系统已结题 2月8日
  • 已采纳回答 1月31日
  • 修改了问题 1月30日
  • 创建了问题 1月30日

悬赏问题

  • ¥15 SPSS分类模型实训题步骤
  • ¥15 求解决扩散模型代码问题
  • ¥15 工创大赛太阳能电动车项目零基础要学什么
  • ¥20 limma多组间分析最终p值只有一个
  • ¥15 nopCommerce开发问题
  • ¥15 torch.multiprocessing.spawn.ProcessExitedException: process 1 terminated with signal SIGKILL
  • ¥15 QuartusⅡ15.0编译项目后,output_files中的.jdi、.sld、.sof不更新怎么解决
  • ¥15 pycharm输出和导师的一样,但是标红
  • ¥15 想问问富文本拿到的html怎么转成docx的
  • ¥15 我看了您的文章,遇到了个问题。