关于1-9编程问题
package com.imcoo.test; public class User { private String username; private String password; public User(String username,String password) { this.setUsername(username); this.setPassword(password); } public void setUsername(String username) { this.username=username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getUsername() { return username; } public void printMess(){ System.out.println("用户名:"+this.getUsername()); System.out.println("密码:"+this.getPassword()); } } package com.imcoo.test; public class UserManager { public String checkUser(User one, User two) { if(one.getUsername()==null||two.getUsername()==null) { return "用户名不能为空"; } if(one.getPassword()==null||two.getPassword()==null) { return "用户密码不能为空"; } if(one.getUsername().equals(two.getUsername())&&one.getPassword().equals(two.getPassword())) return "用户信息一致"; else return "用户信息不一致"; } } package com.imcoo.test; public class Test { public static void main(String[] args) { // TODO Auto-generated method stub User one =new User("Lucy","123456"); User two =new User("Mike","123456"); one.printMess(); two.printMess(); System.out.println("============================"); UserManager um= new UserManager(); String panduan= um.checkUser(one, two); System.out.println(panduan); } }
为什么我的判断为空的语句 不起作用啊???、???? 咋回事啊,求老师指导下
50
收起
正在回答
3回答
运行代码输出的应该是如下信息:
用户名:Lucy
密码:123456
用户名:Mike
密码:123456
============================
用户信息不一致
ps:判断为空,输入的不是字符串null,你要想设为null,需要改下代码
User one =new User(null,"123456");
输出的信息就是:
用户名:null
密码:123456
用户名:Mike
密码:123456
============================
用户名不能为空
代码知音
2018-01-17 10:22:14
public class Test{ // 测试方法 public static void main(String[] args) { //实例化对象,调用相关方法实现运行效果 User one=new User(); one.setUser("lucy"); one.setPassWord("123456"); User two=new User(); two.setUser("Mike"); two.setPassWord("123456"); UserManager test=new UserManager(); System.out.println("用户名:"+one.getUser()); System.out.println("密码:"+one.getPassWord()); System.out.println("用户名:"+two.getUser()); System.out.println("密码:"+two.getPassWord()); System.out.println(test.checkUser(one,two)); System.out.println(test.checkPass(one,two)); } } public class User { //定义属性用户名、密码 private String user; private String passWord; public void setUser(String user){ this.user=user; } public void setPassWord(String passWord){ this.passWord=passWord; } public String getUser(){ return user; } public String getPassWord(){ return passWord; } } public class UserManager { public String checkUser(User one, User two) { // 判断用户名是否为空,是否一致 if (one.getUser()!=""&&two.getUser()!=""){ if (one.getUser().equals(two.getUser())){ return "用户名信息一致"; }else{ return "用户名信息不一致"; } }else{ return "用户名为空,请检查"; } } public String checkPass(User one, User two){ if (one.getPassWord()!=""&&two.getPassWord()!=""){ if (one.getPassWord().equals(two.getPassWord())){ return "密码一致"; }else{ return "密码信息不一致"; } }else{ return "密码信息为空,请检查"; } // 判断密码是否为空,是否一致 } }
自己刚学到这,想了好久,终于写完了。。
Android零基础入门2018版
- 参与学习 人
- 提交作业 5461 份
- 解答问题 7238 个
此次推出的专题为Android攻城狮培养计划的第一部分语法与界面基础篇,将带大家从0开始学习Android开发。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星