一道急用的JAVA题目

2024-11-30 13:59:38
推荐回答(1个)
回答1:

import java.util.Scanner;

public class Test {

/**
* @param args
*/
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
String out = "";
boolean flag = false;
System.out.println("请输入会员生日<月/日:00/00>:");
while (true) {
out = reader.next();
if (validateBirth(out)) {
System.out.println("该会员生日是:" + out);
System.out.println("请输入会员密码<6-10为>:");
flag = true;
} else {
System.out.println("生日输入形式错误!");
}
if (flag) {
if (reader.hasNext()) {
validatePwd(reader.next());
}
}
}
}

/**
* 验证密码
* @param password
*/
private static void validatePwd(String password) {
if (password.length() < 6 || password.length() > 10) {
System.out.println("输入密码不符合要求! ");
} else {
System.out.println("该会员的密码是: " + password);
}

}
/**
* 验证生日,建议根据需求验证规则自己添加
* @param password
*/
public static boolean validateBirth(String date) {
boolean b = true;
if (date.indexOf("/") != 2)
b = false;
String[] d = date.split("/");
try {
int mouth = Integer.parseInt(d[0]);
int day = Integer.parseInt(d[1]);
if ((mouth < 0 || mouth > 12) || (day < 0 || day > 31)) {
b = false;
}
} catch (Exception e) {
b = false;
}
return b;

}
}