案例介绍:
编写程序实现简单的用户登录注册功能。程序包含以下4个功能:
(1)登录功能,用户输入正确的账号密码登录成功;
(2)注册功能,输入用户名和密码进行注册;
(3)查看功能,查看所有的用户名与密码;
(4)退出功能,退出系统;
用户可以输入对应的编号进行相应的功能操作。例如,输入2进入注册功能,输入用户名和密码进行注册。
运行结果:
//main()方法
//使用static修饰属性,则称该属性为静态属性(或全局属性)static Scanner scanner =new Scanner(System.in);// 存储用户名和密码public static String[] USERNAMES = new String[3];public static String[] PASSWORDS = new String[3];public static void main(String[] args){USERNAMES[0]="JAVA";PASSWORDS[0]="java";while(true){menu(); //调用menu()方法System.out.print("请选择功能:");int choice = scanner.nextInt();switch (choice){case 1:login();break; //输入1,调用login()方法case 2:register(); break; //输入2,调用register()方法case 3:show();break; //输入3,调用show()方法default:System.out.println("系统退出!");System.exit(0); //输入0,退出程序执行}}}
// 菜单:menu()方法
public static void menu(){ //菜单System.out.println("------------黑马管理系统------------");System.out.println("1.登录功能(login)");System.out.println("2.注册功能(register)");System.out.println("3.查看(show)");System.out.println("0.退出(exit)");System.out.println("------------黑马管理系统------------");}
//登录:login()方法
/*登录功能:* 键盘输入用户名与密码* 使用for循环+if语句验证输入的用户名和密码是否正确*/public static void login(){ //登录for(int retry=1;retry<=3;retry++){System.out.print("请输入注册用户名:");String usernames = scanner.next();System.out.print("请输入密码:");String passwords = scanner.next();for(int i=0;i<USERNAMES.length;i++){if(passwords!=null){if(usernames.equals(USERNAMES[i])&&passwords.equals(PASSWORDS[i])){System.out.println("登录成功!");return;}}}System.out.println("登录失败 "+retry+" 次,请重新输入!");}System.out.println("登录失败超过三次,退出登录功能!");}
//注册:register()方法
/*注册功能* 键盘输入用户名与密码,如果用户存在则要求重新输入。* 如果用户不存在,在数组中插入注册的账号密码。* 此时可能会有数组长度不够的情况,所以需要增加数组的长度。*/public static void register() {boolean isNewUser; //判断是否是新用户String usernames;do {System.out.print("请输入注册用户名:");usernames = scanner.next();isNewUser = true;/*两种表达方式:* ①for(String uname:USERNAMES)* {*System.out.println(uname);* }* ②for(int i=0;i<USERNAMES.length;i++)* {*System.out.println(USERNAMES[i]);* }*/for (int i = 0; i < USERNAMES.length; i++){if (USERNAMES[i] != null && usernames.equals(USERNAMES[i])){System.out.println("用户名已存在,请重新输入!");isNewUser = false;break;}}} while (!isNewUser); //新用户会跳出循环,不再要求重新输入System.out.print("请输入密码:");String passwords = scanner.next();boolean isFull = true; //判断当前数组是否装满for (int i = 0; i < USERNAMES.length; i++){if (USERNAMES[i] == null){isFull = false;break;}}if(isFull){String[] newUSERNAMES=new String[USERNAMES.length+10];String[] newPASSWORDS=new String[PASSWORDS.length+10];//arraycopy可以实现一个数组的指定个数元素负责到另一个数组中//将USERNAMES数组里从索引为0的元素开始,复制到newUSERNAMES数组里索引为0的位置,复制USERNAMES.length个元素System.arraycopy(USERNAMES,0,newUSERNAMES,0,USERNAMES.length);System.arraycopy(PASSWORDS,0,newPASSWORDS,0,PASSWORDS.length);USERNAMES=newUSERNAMES;PASSWORDS=newPASSWORDS;}for(int i=0;i<USERNAMES.length;i++){if(USERNAMES[i]==null){USERNAMES[i]=usernames;PASSWORDS[i]=passwords;System.out.println("注册成功!");break;}}}
//查看:show()方法
public static void show(){System.out.println("系统当前存在的用户为:");for (int i=0;i<USERNAMES.length;i++){if(USERNAMES[i]!=null){System.out.println("用户名:"+USERNAMES[i]+",密码:"+PASSWORDS[i]);}}}
完整代码:
import java.util.Scanner;public class User {static Scanner scanner =new Scanner(System.in);public static String[] USERNAMES = new String[3];public static String[] PASSWORDS = new String[3];public static void main(String[] args){USERNAMES[0]="JAVA";PASSWORDS[0]="java";while(true){menu(); //调用menu()方法System.out.print("请选择功能:");int choice = scanner.nextInt();switch (choice){case 1:login();break; //输入1,调用login()方法case 2:register(); break; //输入2,调用register()方法case 3:show();break; //输入3,调用show()方法default:System.out.println("系统退出!");System.exit(0); //输入0,退出程序执行}}}public static void menu(){ //菜单System.out.println("------------黑马管理系统------------");System.out.println("1.登录功能(login)");System.out.println("2.注册功能(register)");System.out.println("3.查看(show)");System.out.println("0.退出(exit)");System.out.println("------------黑马管理系统------------");}public static void login(){ //登录for(int retry=1;retry<=3;retry++) {System.out.print("请输入注册用户名:");String usernames = scanner.next();System.out.print("请输入密码:");String passwords = scanner.next();for(int i=0;i<USERNAMES.length;i++) {if(passwords!=null) {if(usernames.equals(USERNAMES[i])&&passwords.equals(PASSWORDS[i])) {System.out.println("登录成功!");return;}}}System.out.println("登录失败 "+retry+" 次,请重新输入!");}System.out.println("登录失败超过三次,退出登录功能!");}public static void register() { //注册boolean isNewUser; //判断是否是新用户String usernames;do {System.out.print("请输入注册用户名:");usernames = scanner.next();isNewUser = true;for (int i = 0; i < USERNAMES.length; i++) {if (USERNAMES[i] != null && usernames.equals(USERNAMES[i])) {System.out.println("用户名已存在,请重新输入!");isNewUser = false;break;}}} while (!isNewUser); //新用户会跳出循环,不再要求重新输入System.out.print("请输入密码:");String passwords = scanner.next();boolean isFull = true; //判断当前数组是否装满for (int i = 0; i < USERNAMES.length; i++) {if (USERNAMES[i] == null) {isFull = false;break;}}if(isFull) {String[] newUSERNAMES=new String[USERNAMES.length+10];String[] newPASSWORDS=new String[PASSWORDS.length+10];System.arraycopy(USERNAMES,0,newUSERNAMES,0,USERNAMES.length);System.arraycopy(PASSWORDS,0,newPASSWORDS,0,PASSWORDS.length);USERNAMES=newUSERNAMES;PASSWORDS=newPASSWORDS;}for(int i=0;i<USERNAMES.length;i++) {if(USERNAMES[i]==null) {USERNAMES[i]=usernames;PASSWORDS[i]=passwords;System.out.println("注册成功!");break;}}}public static void show(){ //查看System.out.println("系统当前存在的用户为:");for (int i=0;i<USERNAMES.length;i++) {if(USERNAMES[i]!=null) {System.out.println("用户名:"+USERNAMES[i]+",密码:"+PASSWORDS[i]);}}}}