编写程序实现简单的登录注册功能。程序包含以下4个功能:
(1)登录功能,用户输入正确的账号密码进行成功;
(2)注册功能,输入用户名和密码进行注册;
(3)查看功能,查看所有的用户名和密码;
(4)退出功能,退出系统。
用户可以输入对应的编号进行对应的功能操作。例如,输入2进入注册功能,输入用户名和密码进行注册。
利用容器存储数据:
package disembark;
import java.util.ArrayList;
import java.util.Scanner;
public class message {
private ArrayList<String> acount = new ArrayList<String>(); //定义ArrayList容器用于存储账号信息
private ArrayList<String> secret = new ArrayList<String>(); //定义ArrayList容器用于存储密码信息
private int num;
public void register() {
System.out.println("=======注册账号=======");
System.out.print("请输入账号:");
Scanner in = new Scanner(System.in);
String str_acount; //接收用户输入的账号信息
str_acount = in.next();
System.out.print("请输入密码:");
String str_secret; //当判定账号还未注册后,输入密码
str_secret = in.next();
boolean check = detection_acount(str_acount); //判定该账号是否存在于account容器中
if(check) {
System.out.println("账号已存在..."); //check数据类型为Boolean类型当返回为true是证明账号已经注册了
}
this.acount.add(str_secret); //将注册的账号信息加入到account容器中
this.secret.add(str_secret); //将注册的密码信息加入到secret容器中
System.out.println("注册成功...");
}
private boolean detection_acount(String acount) { //判定账号是否已经存在于account容器,即该账号是否已经注册过
boolean check;
check = this.acount.contains(acount);
num = this.acount.indexOf(acount);
return check;
}
private boolean detection_secret(String secret) {
boolean check;
check = this.secret.get(num).equals(secret);
return check;
}
public void disembark() {
String number;
String password;
System.out.println("=======登录账号=======");
System.out.print("请输入账号:");
Scanner in = new Scanner(System.in);
number = in.next();
System.out.print("请输入密码:");
password = in.next();
boolean check;
check = detection_acount(number);
if(check) {
check = detection_secret(password);
while(true) {
if(check) {
System.out.println("登录成功...");
break;
}
else {
System.out.println("密码错误,请重新输入:");
password = in.next();
check = detection_secret(password);
}
}
}
else {
System.out.println("该账号尚未注册,请先进行注册...");
}
}
//查询账号信息
public void examine() {
System.out.println("总共拥有:" + acount.size() + "个账号");
for(String k : acount) {
System.out.println("========================================");
System.out.println(k);
int i = acount.indexOf(k);
System.out.println(secret.get(i));
}
}
//输出功能代号
public void show() {
System.out.println("0:退出系统" + "," + "1:注册账号" + "," + "2:登录账号" + "," + "3:查看账号信息");
}
}
引用类生成对象
package disembark;
import java.util.Scanner;
public class disembark {
public static void main(String[] args) {
message member = new message();
OUT://标号控制break跳出的位置
for(;;) {
member.show();
int i;
System.out.print("请选择:");
Scanner in = new Scanner(System.in);
i = in.nextInt();
switch(i) {
case 0:
System.out.println("退出系统...");
break OUT;
case 1:
member.register();
break;
case 2:
member.disembark();
break;
case 3:
member.examine();
break;
default:
System.out.println("你的选择有误...");
}
}
}
}
利用数组进行操作:
package data;
import java.util.Scanner;
public class message {
private String account; //存储账号信息
private String password; //存储密码信息
int num = -1; //用于记录当账号存在时,password中与account相对应的密码存储位置
int count = 0; //记录数据的条数
//注册账号
public void register(String [] account,String []password,int i) {
System.out.println("=======注册账号=======");
Scanner in =new Scanner(System.in);
System.out.print("请输入账号:");
String str_account; //接收用户输入的账号信息
str_account = in.next();
boolean check;
check = detect_account(account, str_account); //判断该账号是否已经存在
if(check) {
System.out.println("账号已经存在...");
}
else {
account[i] = str_account; //保存账号
System.out.print("请设置密码:");
String str_password = in.next();
password[i] = str_password; //保存密码
System.out.println("注册成功...");
count++; //账号数量加1
}
}
//登录账号
public void disembark(String[] account,String[] password) {
System.out.println("=======登录账号=======");
System.out.print("请输入账号:");
Scanner in = new Scanner(System.in);
String str_account = in.next(); //输入账号
System.out.print("请输入密码:");
String str_password; //输入密码
str_password = in.next();
boolean check;
check = detect_account(account, str_account); //判断该账号是否存在
if(check) { //当账号存在时执行if内部语句
while(true) { //设置一个死循环判定用户输入的密码是否正确,如果正确就可以跳出死循环,否则需要用户继续输入密码再次进行判断
check = detect_password(password,str_password);
if(check) {
System.out.println("登录成功...");
num = -1; //改系统中账号和密码的位置(数组下标)相同,使用设置num来根据account的下标寻找密码的位置,登录成功后需要将改位置再次设置为空
break;
}
else {
System.out.println("密码错误,请重新输入:");
str_password = in.next();
check = detect_password(password,str_password); //判定输入的密码是否正确
}
}
}
else {
System.out.println("该账号尚未注册,请先进行注册..."); //如果判定account数组中未找到输入的账号信息,则改账号尚未注册
}
}
//查询内部的账号信息
public void examine(String[] account,String[] password) {
System.out.println("总共拥有:" + count + "个账号");
for(int i = 0;i<count;i++) {
System.out.println("========================================");
System.out.println(account[i]); //输出账号信息
System.out.println(password[i]); //输出密码信息
}
}
//显示操作代号
public void show() {
System.out.println("0:退出系统"+","+ "1:注册账号"+","+"2:登录账号"+","+"3:查看账号信息");
}
//判定账号信息,此处设置为私有属性,用户不能调用这一部分的功能,该功能属于管理者的操作范围
private boolean detect_account(String[] account,String str_account) {
for(int i = 0;i<count;i++) {
if(account[i].equals(str_account)) {
num = i;
return true;
}
}
return false;
}
//判断密码信息
private boolean detect_password(String[] password,String str_password) {
if(password[num].equals(str_password)) { //num作为全局变量,在前面判断账号信息时其值发生改变
return true;
}
else {
return false;
}
}
public static void main(String[] args) {
}
}
利用类生成对象:
package data;
import java.util.Scanner;
public class disembark {
public static void main(String[] args) {
String [] account = new String [25];
String [] password = new String [25];
message member = new message();
int count = 0;
OUT: //标号控制break跳出的位置
for(;;) {
member.show();
int i;
System.out.print("请选择:");
Scanner in = new Scanner(System.in);
i = in.nextInt();
switch(i) {
case 0:
System.out.println("退出系统...");
break OUT;
case 1:
member.register(account,password, count);
count++;
break;
case 2:
member.disembark(account,password);
break;
case 3:
member.examine(account, password);
break;
default:
System.out.println("你的选择有误...");
}
}
}
}
代码使用了类在进行代码复制时需要注意package的路径