赞
踩
引入包:
import java.util.Scanner;
import java.util.Random;
main函数:
public static void main(String[] args){
Random r=new Random();
int one=r.nextInt(10); //产生0到9的随机数
int two=r.nextInt(10);
int three=r.nextInt(10);
int four=r.nextInt(10);
String verificationCode=""+one+two+three+four;//将4个随机数组成一个字符串
System.out.println("请输入验证码:\n"+verificationCode);
Scanner s = new Scanner(System.in);
for(int i=0;i<5;i++){
String strIn= s.nextLine();
if(strIn.equals(verificationCode)){//判断用户输入与验证码是否相同
System.out.println("输入正确");
break;
}else{
if(5-i-1==0){//判断输入次数是否已经达到了5次
System.out.println("输入次数已经达到5次,请明天再来!");
}else{
System.out.println("输入错误,你还剩下"+(5-i-1)+"机会");
}
}
}
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。