System.out.println("数量"+shuliang[x]+"价格"+price[x]+"名称"+name[x]);
}
System.out.println("颜寒");
}
}
7.在注册时,需要验证用户输入的用户名、密码、性别、年龄等信息是否符合标准,如果符合标准方可进行注册。
packageyh;
importjava.util.Date;
importjava.util.Scanner;
publicclassyh{
publicstaticvoidmain(String[]args){
Datefirst=newDate();
Stringusername;
Stringpwd;
inti=0;
intj=0;
while(true)
{
Scannerinput=newScanner(System.in);
//input.useDelimiter("\n");
System.out.println("------------用户注册--------------");
System.out.println("用户名:
");
username=input.nextLine();
System.out.println("密码:
");
pwd=input.nextLine();
System.out.println("性别:
");
Stringsex=input.nextLine();
System.out.println("年龄:
");
Stringage=input.nextLine();
booleancon=Register.validateUser(username,pwd,sex,age);
if(con){
System.out.println("注册成功!
");
break;
}else{
System.out.println("注册失败!
");
j++;
if(j>2){
System.out.println("输入次数超过3次,等待20秒后再次尝试输入。
");
Datedate1=newDate();//获取当前日期
System.out.println("[发生错误时间]"+date1);
try{
Thread.sleep(2000);//暂停20秒
}catch(InterruptedExceptione){
e.printStackTrace();
}
Datedate2=newDate();
j=0;
System.out.println("现在时间为:
"+date2);
System.out.println("请重新输入:
");
}
}
}
while(true)
{
Scannerinput=newScanner(System.in);
System.out.println("------------用户登陆--------------");
System.out.println("用户名:
");
Stringlogin_name=input.nextLine();
System.out.println("密码:
");
Stringlogin_pwd=input.nextLine();
booleancen=Register.validateLogin(login_name,login_pwd,username,pwd);
if(cen){
System.out.println("登录成功!
");
break;
}else{
System.out.println("用户名或密码有误!
");
i++;
if(i>2){
System.out.println("输入次数超过3次,等待20秒后再次尝试输入。
");
Datedate1=newDate();//获取当前日期
System.out.println("[发生错误时间]"+date1);
try{
Thread.sleep(20000);//暂停20秒
}catch(InterruptedExceptione){
e.printStackTrace();
}
Datedate2=newDate();
i=0;
System.out.println("现在时间为:
"+date2);
System.out.println("请重新输入:
");
}
}
}
Datelast=newDate();
System.out.println("运行时间:
"+(last.getTime()-first.getTime())/1000+"秒");
}
}
packageyh;
publicclassRegister{
publicstaticbooleanvalidateUser(Stringuname,Stringupwd,Stringsex,Stringage){
booleanconUname=false;//用户名是否符合要求
booleanconPwd=false;//密码是否符合要求
booleanconAge=false;//年龄是否符合要求
booleanconsex=false;//性别是否符合要求
booleancon=false;//验证是否通过
Stringsexm="男";
Stringsexw="女";
if(uname.length()>0){
for(inti=0;i//验证用户名是否全部为字母,不能含有空格
if(Character.isLetterOrDigit(uname.charAt(i))){
conUname=true;
}else{
conUname=false;
System.out.println("用户名只能由字母组成,且不能含有空格!
");
break;
}
}
}else{
System.out.println("用户名不能为空!
");
}
if(upwd.length()>0){
for(intj=0;j//验证密码是否由数字和字母组成,不能含有空格
if(Character.isLetterOrDigit(upwd.charAt(j))){
conPwd=true;
}else{
conPwd=false;
System.out.println("密码只能由数字或字母组成!
");
break;
}
}
}else{
System.out.println("密码不能为空!
");
}
if(sex.length()>0){
if(sex.equals(sexm)||sex.equals(sexw)){
consex=true;
}else{
consex=false;
System.out.println("仅可输入“男”和“女”!
");
}
}else{
System.out.println("性别不能为空!
");
}
if(age.length()>0){
for(intk=0;k//验证年龄是否由数字组成
if(Character.isDigit(age.charAt(k))){
conAge=true;
}else{
conAge=false;
System.out.println("年龄输入有误!
");
break;
}
}
}else{
System.out.println("年龄必须输入!
");
}
if(conUname&&conPwd&&conAge&&consex){
con=true;
}else{
con=false;
}
returncon;
}
publicstaticbooleanvalidateLogin(Stringlogin_name,Stringlogin_pwd,Stringuname,Stringupwd){
booleancon=false;
if(uname.equals(login_name)&&upwd.equals(login_pwd)){//比较两个String对象
con=true;
}else{
con=false;
}
returncon;
}
}
要求:
1、用户名、密码可以是字母+数字,但是不能有空格以及其他字符,在登录过程中字母全部转换为小写或者忽略大小写。
2、性别、年龄需要符合要求,比如性别只能是“男”或者“女”,年龄只能是数字。
3、如果注册过程中有一个不合格,提示信息、稍等30秒后可以循环注册直至成功。
4、注册成功后需要登录一下,同时加上时间的计算。
在登录时,需要验证用户输入的用户名、密码以及年龄信息是否符合标准,如果符合标准方可进行登录。
----------------------------------------------------------------15分;
要求:
1、用户名、密码可以是字母+数字,但是不能有空格,字母全部转换为小写。
2、年龄需要大于18小于50。
3、如果登录过程中有一个不合格,提示信息,超过3次后需要等待10秒才可以循环登录直至成功。
packageyh;
importjava.util.Scanner;
publicclassaaa{
publicstaticvoidmain(String[]args){
Datefirst=newDate();
Stringusername;
Stringpwd;
inti=0;
intj=0;
while(true)
{
Scannerinput=newScanner(System.in);
//input.useDelimiter("\n");
System.out.println("------------用户注册--------------");
System.out.println("用户名:
");
username=input.nextLine();
System.out.println("密码:
");
pwd=input.nextLine();
System.out.println("性别:
");
Stringsex=input.nextLine();
System.out.println("年龄:
");
Stringage=input.nextLine();
booleancon=Register.validateUser(username,pwd,sex,age);
if(con){
System.out.println("注册成功!
");
break;
}else{
System.out.println("注册失败!
");
j++;
if(j>2){
System.out.println("输入次数超过3次,等待20秒后再次尝试输入。
");
Datedate1=newDate();//获取当前日期
System.out.println("[发生错误时间]"+date1);
try{
Thread.sleep(20000);//暂停20秒
}catch(InterruptedExceptione){
e.printStackTrace();
}
Datedate2=newDate();
j=0;
System.out.println("现在时间为:
"+date2);
System.out.println("请重新输入:
");
}
}
}
while(true)
{
Scannerinput=newScanner(System.in);
System.out.println("------------用户登陆--------------");
System.out.println("用户名:
");
Stringlogin_name=input.nextLine();
System.out.pri