用户输入数字选择操作
package Test09_Demo.Demo03;/*
@create 2020--11--25--16:12
*/
public class User {
private String name;
private String password;
private String birthday;
private String hobby;
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", password='" + password + '\'' +
", birthday='" + birthday + '\'' +
", hobby='" + hobby + '\'' +
'}';
}
public User() {
}
public User(String name, String password, String birthday, String hobby) {
this.name = name;
this.password = password;
this.birthday = birthday;
this.hobby = hobby;
}
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 getBirthday() {
return birthday;
}
public void setBirthday(String birthday) {
this.birthday = birthday;
}
public String getHobby() {
return hobby;
}
public void setHobby(String hobby) {
this.hobby = hobby;
}
}
package Test09_Demo.Demo03;/*
@create 2020--11--25--16:12
*/
import com.sun.xml.internal.ws.api.ha.StickyFeature;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
ArrayList<User> array= new ArrayList<>();
while (true) {
for (;;) {
System.out.println("请输入操作 1.注册 2.登陆 3.退出");
int l = s.nextInt();
switch (l){
case 1:
registe(array);
break;
case 2:
login(array);
break;
case 3:
s.close();
System.out.println("感谢您的使用!");
//结束程序
System.exit(0);
default:
System.out.println("输入错误11");
break;
}
}
}
}
private static void registe(ArrayList<User> array){
Scanner s = new Scanner(System.in);
System.out.println("楷书注册");
System.out.println("请输入用户名:");
String name = s.next();
System.out.println("请输入密码:");
String password = s.next();
System.out.println("请输入生日:");
String birthday = s.next();
System.out.println("请输入爱好:");
String hobby = s.next();
User S = new User();
S.setName(name);
S.setPassword(password);
S.setBirthday(birthday);
S.setHobby(hobby);
array.add(S);
}
private static void login(ArrayList<User> array){
Scanner sc = new Scanner(System.in);
System.out.println("请输入用户名");
String name = sc.next();
System.out.println("请输入密码");
String password = sc.next();
boolean fist = false;
for (User user : array) {
if (name.equals(user.getName()) && password.equals(user.getPassword())) {
System.out.println("登陆成功");
System.out.println(user);
fist = true;
break;
}
}
if (!fist) {
System.out.println("用户名或密码错误!!!");
}
}
}