用户输入数字选择操作

    1. package Test09_Demo.Demo03;/*
    2. @create 2020--11--25--16:12
    3. */
    4. public class User {
    5. private String name;
    6. private String password;
    7. private String birthday;
    8. private String hobby;
    9. @Override
    10. public String toString() {
    11. return "User{" +
    12. "name='" + name + '\'' +
    13. ", password='" + password + '\'' +
    14. ", birthday='" + birthday + '\'' +
    15. ", hobby='" + hobby + '\'' +
    16. '}';
    17. }
    18. public User() {
    19. }
    20. public User(String name, String password, String birthday, String hobby) {
    21. this.name = name;
    22. this.password = password;
    23. this.birthday = birthday;
    24. this.hobby = hobby;
    25. }
    26. public String getName() {
    27. return name;
    28. }
    29. public void setName(String name) {
    30. this.name = name;
    31. }
    32. public String getPassword() {
    33. return password;
    34. }
    35. public void setPassword(String password) {
    36. this.password = password;
    37. }
    38. public String getBirthday() {
    39. return birthday;
    40. }
    41. public void setBirthday(String birthday) {
    42. this.birthday = birthday;
    43. }
    44. public String getHobby() {
    45. return hobby;
    46. }
    47. public void setHobby(String hobby) {
    48. this.hobby = hobby;
    49. }
    50. }
    1. package Test09_Demo.Demo03;/*
    2. @create 2020--11--25--16:12
    3. */
    4. import com.sun.xml.internal.ws.api.ha.StickyFeature;
    5. import java.util.ArrayList;
    6. import java.util.List;
    7. import java.util.Scanner;
    8. public class Test {
    9. public static void main(String[] args) {
    10. Scanner s = new Scanner(System.in);
    11. ArrayList<User> array= new ArrayList<>();
    12. while (true) {
    13. for (;;) {
    14. System.out.println("请输入操作 1.注册 2.登陆 3.退出");
    15. int l = s.nextInt();
    16. switch (l){
    17. case 1:
    18. registe(array);
    19. break;
    20. case 2:
    21. login(array);
    22. break;
    23. case 3:
    24. s.close();
    25. System.out.println("感谢您的使用!");
    26. //结束程序
    27. System.exit(0);
    28. default:
    29. System.out.println("输入错误11");
    30. break;
    31. }
    32. }
    33. }
    34. }
    35. private static void registe(ArrayList<User> array){
    36. Scanner s = new Scanner(System.in);
    37. System.out.println("楷书注册");
    38. System.out.println("请输入用户名:");
    39. String name = s.next();
    40. System.out.println("请输入密码:");
    41. String password = s.next();
    42. System.out.println("请输入生日:");
    43. String birthday = s.next();
    44. System.out.println("请输入爱好:");
    45. String hobby = s.next();
    46. User S = new User();
    47. S.setName(name);
    48. S.setPassword(password);
    49. S.setBirthday(birthday);
    50. S.setHobby(hobby);
    51. array.add(S);
    52. }
    53. private static void login(ArrayList<User> array){
    54. Scanner sc = new Scanner(System.in);
    55. System.out.println("请输入用户名");
    56. String name = sc.next();
    57. System.out.println("请输入密码");
    58. String password = sc.next();
    59. boolean fist = false;
    60. for (User user : array) {
    61. if (name.equals(user.getName()) && password.equals(user.getPassword())) {
    62. System.out.println("登陆成功");
    63. System.out.println(user);
    64. fist = true;
    65. break;
    66. }
    67. }
    68. if (!fist) {
    69. System.out.println("用户名或密码错误!!!");
    70. }
    71. }
    72. }