A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

© lingyins 中级黑马   /  2015-11-13 17:11  /  859 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

简单的应用了集合,数组,多线程等知识来做了一个简易的登录注册功能
  1. import java.util.ArrayList;
  2. import java.util.Scanner;

  3. public class 登录 {
  4.         public static ArrayList<String> UserName = new ArrayList<>(); // 定义集合存储对应账号密码
  5.         private static ArrayList<String> PassWord = new ArrayList<>();
  6.         public static ArrayList<Integer> Age = new ArrayList<>();
  7.         public static ArrayList<Integer> Sex = new ArrayList<>();
  8.         public static int[] 账户余额 =new int[1];
  9.         private static String[] UserType = new String[3]; // 用户类型
  10.         public static Scanner sc = new Scanner(System.in); // 需要多次调用键盘输出
  11.         static String choose = null;
  12.         static int memberType =0 ;
  13.         static { // 定义默认管理员账号和用户类型
  14.                 账户余额[0] =100;
  15.                 UserName.add("admin");
  16.                 PassWord.add("admin");
  17.                 Age.add(null);
  18.                 Sex.add(null);
  19.                 UserType[0] = "管理员";
  20.                 UserType[1] = "VIP";
  21.                 UserType[2] = "普通用户";
  22.         }

  23.         public static boolean index() throws InterruptedException {
  24.                 while (true) { // 登录注册页面
  25.                         System.out.println("---------------------------------");
  26.                         System.out.println(" 请选择:/1登录 /2注册");
  27.                         choose = sc.next();

  28.                         if ("1".equals(choose)) {
  29.                                 boolean b = login(); // 调用登录方法
  30.                                 return b;
  31.                         } else if ("2".equals(choose)) {
  32.                                 register(); // 调用注册方法
  33.                         } else {
  34.                                 System.err.println("您的选择有误,请重新输入");
  35.                                 continue;
  36.                         }

  37.                 }
  38.         }

  39.         public static boolean login() throws InterruptedException {

  40.                 System.out.println("请输入您的用户名 ");
  41.                 String username = sc.next(); // 键盘录入
  42.                 System.err.println("请输入您的密码");
  43.                 String password = sc.next();
  44.                 for (int i = 0; i < UserName.size(); i++) { // 用集合的长度来判断循环次数
  45.                         if (UserName.get(i).equals(username)) { // 因为集合默认为空,所以在注册之后他们的集合账号密码索引默认是一样的,所以我们判断是用一个集合中同一个索引来判断是否与集合中所存储内容的相符
  46.                                 if (PassWord.get(i).equals(password)) {
  47.                                         memberType = i;
  48.                                         new Thread() {
  49.                                                 public void run() {
  50.                                                         for (int j = 0; j < 25; j++) {

  51.                                                                 try {
  52.                                                                         sleep(100);
  53.                                                                 } catch (InterruptedException e) {
  54.                                                                         e.printStackTrace();
  55.                                                                 }
  56.                                                                 System.out.print(".");
  57.                                                                
  58.                                                         }
  59.                                                 }
  60.                                         }.start();
  61.                                        
  62.                                         new Thread() {
  63.                                                 public void run() {
  64.                                                         synchronized (this) {

  65.                                                                 try {
  66.                                                                         sleep(3000);
  67.                                                                 } catch (InterruptedException e) {
  68.                                                                         e.printStackTrace();
  69.                                                                 }
  70.                                                                 System.out.println();
  71.                                                                
  72.                                                         }
  73.                                                 }
  74.                                         }.start();
  75.                                         Thread.sleep(3000);
  76.                                         System.out.println("登录成功");
  77.                                         return true;
  78.                                 }

  79.                         }

  80.                 }
  81.                 System.out.println("您的账号密码有误,请您重新输入");
  82.                 System.out.println("---------------------------------");
  83.                 return false;
  84.         }

  85.         public static boolean register() {
  86.                 while (true) {
  87.                         System.out.println("请输入用户名");
  88.                         String username = sc.next();
  89.                         for (int i = 0; i < UserName.size(); i++) {// 本循环用于判断本用户名是否已在数组中存在
  90.                                 if (username.equals(UserName.get(i))) {
  91.                                         System.out.println("您注册的用户名已存在");
  92.                                         return false;
  93.                                 }
  94.                         }
  95.                         System.out.println("请输入密码");
  96.                         String password = sc.next();
  97.                         System.out.println("请再次输入您的密码");
  98.                         String typepassword = sc.next();
  99.                         if (password.equals(typepassword)) {
  100.                                 System.out.println("年龄:");
  101.                                 Age.add(sc.nextInt());
  102.                                 System.out.println("性别: /1.男 /2.女");
  103.                                 while(true){
  104.                                         int f = sc.nextInt();
  105.                                         if (f == 1) {
  106.                                                 Sex.add(1);
  107.                                                 break;
  108.                                         }else if (f== 2) {
  109.                                                 Sex.add(2);
  110.                                                 break;
  111.                                         }else {
  112.                                                 System.out.println("您的性别选择有误");
  113.                                         }
  114.                                 }
  115.                                 UserName.add(username);// 将注册成功的数据分别写入用户集合和密码集合中
  116.                                 PassWord.add(typepassword);
  117.                                 System.out.println("恭喜您注册成功,欢迎您成为黑马程序员的一员,请继续填写您的个人信息");
  118.                                 return true;
  119.                         } else {
  120.                                 System.out.println("您的第二次密码与第一次不同,请您重新注册");
  121.                                 System.out.println("---------------------------------");
  122.                         }
  123.                 }
  124.         }
  125. }
复制代码


您需要登录后才可以回帖 登录 | 加入黑马