请问老师这样做符合要求吗?密码用int和String哪个更合适?
public class User { // 定义属性用户名、密码 private String userName; private String password; // 设计构造函数实现对属性赋值 public User(String userName, String password) { this.setUserName(userName); this.setPassword(password); } // 设置私有属性,get/set方法实现对属性的访问 public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } } public class UserManager { // 用户信息验证的方法 public String checkUser(User one, User two) { // 判断用户名是否为空,是否一致 String result = null; if (" ".equals(one.getUserName()) && " ".equals(two.getUserName())) { result = "用户名为空!"; } else { if (one.getUserName().equals(two.getUserName())) { } else { result = "用户名不一致!"; } } // 判断密码是否为空,是否一致 if (" ".equals(one.getPassword()) && " ".equals(two.getPassword())) { result = "密码为空"; } else { if (one.getPassword().equals(two.getPassword())) { } else { result = "密码错误"; } } return result; } } public class Test { // 测试方法 public static void main(String[] args) { // 实例化对象,调用相关方法实现运行效果 User one = new User("Lucy", "123456"); User two = new User("Mike", "123456"); System.out.println("用户名:" + one.getUserName()); System.out.println("密码:" + one.getPassword()); System.out.println("用户名:" + two.getPassword()); System.out.println("密码:" + two.getPassword()); System.out.println("========================="); UserManager manager = new UserManager(); System.out.println(manager.checkUser(one, two)); } }
65
收起
正在回答
5回答
你的代码完成的不错;密码在这个题目用int和String都是可以的。祝学习愉快~
慕莱坞1975833
2018-02-18 20:47:12
题目的要求应该是要比较one 和two
package com.imooc; public class UserManager { // 用户信息验证的方法 public String checkUser(User one, User two) { // 判断用户名是否为空,是否一致 if(!one.getUser().equals(two.getUser())) { return "用户名不一致"; } // 判断密码是否为空,是否一致 if(!one.getPwd().equals(two.getPwd())) { return "密码不一致"; } return "验证成功"; } }
package com.imooc; public class User { private String user; private String pwd; public User(String user,String pwd) { this.setUser(user); this.setPwd(pwd); } public String getUser() { return this.user; } public void setUser(String user) { this.user = user; } public String getPwd() { return this.pwd; } public void setPwd(String pwd) { this.pwd = pwd; } }
package com.imooc; public class Test { public static void main(String[] args) { User one=new User("Lucy","123456"); User two=new User("Mike","123456"); UserManager user=new UserManager(); System.out.println("用户名:"+one.getUser()); System.out.println("密码:"+one.getPwd()); System.out.println("用户名:"+two.getUser()); System.out.println("密码:"+two.getPwd()); System.out.println("======================"); System.out.println(user.checkUser(one, two)); } }
我想跑路
2018-02-17 23:29:42
public class Test { // 测试方法 public static void main(String[] args) { // 实例化对象,调用相关方法实现运行效果 User one = new User("Lucy", "123456"); User two = new User("Mike", "123456"); System.out.println("用户名:" + one.getUserName()); System.out.println("密码:" + one.getPassword()); System.out.println("用户名:" + two.getUserName()); System.out.println("密码:" + two.getPassword()); System.out.println("========================="); UserManager manager = new UserManager(); System.out.println(manager.checkUser(one, two)); } }
Java零基础入门18
- 参与学习 人
- 提交作业 7317 份
- 解答问题 14452 个
想要入门学编程?多年一直活跃在编程语言排行版前列的Java是一个很好的选择。本路径将从Java基础语法、面向对象、常用工具类三部分,为你开启软件开发的大门!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星