具体要求:①编写User类,该类包含userName,password两个私有的成员变量。②定义3个构造方法:参数可以为userName或者userName,password或者没有参数。③实现check()方法,功能为只要...
具体要求:
①编写User类,该类包含userName,password两个私有的成员变量。
②定义3个构造方法:参数可以为userName或者userName,password或者没有参数。
③实现check()方法,功能为只要用户名不为空并且密码为“12345678”,则打印该用户名有效,否则打印用户名无效。
④提示:字符串比较用String类的equals函数,例如判断password是否为“12345678”
用如下程序:password.equals(“12345678”)//相等返回true,不相等返回false。
这是我写的
import java.util.*;
class User{
private String userName,password;
User(){
System.out.println("输入用户名:");
Scanner reader=new Scanner(System.in);
userName=reader.nextLine();
System.out.println("输入密码:");
Scanner reader1=new Scanner(System.in);
password=reader1.next();
}
void check(){
int m=0;
int n=0;
if(userName.equals("")==true)
m=0;
if(password.equals("12345678")==true)
n=1;
if(m!=0&&n==1)
System.out.println("用户名有效");
else
System.out.println("用户名无效");
}
}
public class Users {
public static void main(String[] args){
User user=new User();
user.check();
}
}
可是运行时,在用户名为空,密码是12345678时,显示无效,正确
可是,用户名,密码都符合条件时却还显示无效,看来半天,不会改了
运行结果如图
展开