JAVA实现用户注册,登录,修改密码
class
import java.util.Arrays;import java.util.Scanner;
public class User {
String name;
String password;
String sex ;
int age ;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public User[] shuzu(User[] users) { //初始化数组,预存2人
users[0].name = "qqq";
users[0].password = "123";
users[1].name = "bbb";
users[1].password = "456";
for (int i = 0; i < users.length; i++) {
System.out.println(users[i].name+" "+users[i].password);
}
return users;
}
//______________________注册__________________________________
public User[] register(String newn,String newp,User[] users){
users= Arrays.copyOf(users, users.length+1);//数组扩容+1
User q = new User() ;
// System.out.println(users.length); //输出新数组长度,辅助校验数组扩容
int a = 0 ;
for (int i = 0; i < users.length-1; i++) { //校验用户名是否重复,重复 a+1
if(users[i].name.equals(newn) ){
a++;
}
}
if(a == 0){//a=0.用户名不重复,赋值
users[users.length-1] = q ;//新建对象存入数组最后一个位置
users[users.length-1].name = newn;
users[users.length-1].password = newp;
System.out.println("注册成功");
}else{ // 用户名重复,提示
System.out.println("用户名重复");
}
// for (int i = 0; i < users.length; i++) { //输出现有用户名,辅助校验
// System.out.println(users[i].name);
// }
return users;//返回数组
}
//______________________登录__________________________________
int flag;// 辅助循环判定
public void login(String newn,String newp,User[] users){
for (int i = 0; i < users.length; i++) {//循环遍历数组
System.out.println(users[i].name+"标记"+i+"标记"+users[i].password);//遍历输出现在数组
if(newn.equals(users[i].name) && newp.equals(users[i].password) ){
flag = 0; //同一下标的对象的用户名和密码与输入的用户名和密码都一样,flag赋值0
break;//退出循环
}else{
flag =1;//同一下标的对象的用户名和密码与输入的用户名和密码不一样,flag赋值0,说明输入的用户名和密码有问题
}
}
// System.out.println(newn + newp);
if(flag == 0){ // 给出系统提示
System.out.println("登录成功");
}else{
System.out.println("用户名或密码错误,登录失败");
}
}
//________________________________修改密码——————————————————————————————————
public User[] change(String oldn,String oldm,User[] users) {
int a = 0 ;
for (int i = 0; i < users.length; i++) {//循环遍历数组,校验输入的用户名是否存在
if(oldn.equals(users[i].name)){
a = i;
flag = 0;
}else{
flag = 1 ;
}
}
if(flag == 0){//输入的用户名存在,输入的新密码替换旧密码
users[a].password = oldm;
System.out.println("密码修改成功");
System.out.println("用户名:"+users[a].name +" "+"新密码"+users[a].password);
}else{//输入的用户名不存在,提示输入有误
System.out.println("输入的用户名未查询到");
}
return users;//返回新数组
}
}
demo
import java.util.Arrays;import java.util.Scanner;
public class Demo1{//_____________________用户名密码————————————————————
public static void main(String[] args) {
User us = new User();
User us1 = new User();
User us2 = new User();
User[] users = {us1,us2};
us.shuzu( users);//初始化数组
for (int i = 1; i > 0; i++) { //循环实现登录注册
System.out.println("请选择操作:1注册,2登录 3修改密码");
Scanner sc = new Scanner(System.in);
String num = sc.next();
if(num.equals("1")){ // 注册功能
System.out.println("_________注册__________");
System.out.println("请创建用户名");
us.setName(sc.next());
System.out.println("请创建密码");
us.setPassword(sc.next());
// System.out.println(us.getName());
users = us.register(us.getName(),us.getPassword(), users);
}else if(num.equals("2")){ // 登录功能
System.out.println("__________登录_________");
System.out.println("请输入用户名");
String newn = sc.next();
System.out.println("请输入密码");
String newp = sc.next();
us.login(newn, newp, users);
}else if(num.equals("3")){ //修改密码
System.out.println("__________修改密码_________");
System.out.println("请输入旧用户名");
us.setName(sc.next());
System.out.println("请输入新的密码");
us.setPassword(sc.next());
users = us.change(us.getName(), us.getPassword(), users);
}
else{ //输入值有误,提示
System.out.println("输入的值有误");
}
}
}
}
以上是 JAVA实现用户注册,登录,修改密码 的全部内容, 来源链接: utcz.com/z/389719.html