生成验证码
package StringTest;
import java.util.Random;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Random r = new Random();
System.out.println("请输入要生成验证码的长度:");
Scanner sc = new Scanner(System.in);
int leng = sc.nextInt();
String s = "abcdefghijklmnopqrstuvwsyz123456789ABCDEFGHIJKLMNOPQRSTUVWSYZ";
String code = "";
for(int i =0;i<leng;i++) {
int index = r.nextInt(60)+1;//从验证码可能出现的字符串中抽取一个元素
char c = s.charAt(index);
code += c;
}
System.out.println(code);
}
}
运行结果
用这个方法写出的生成验证码出现数字的概率变低了好多
模拟登录
package StringTest;
import java.util.Scanner;
public class Join {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
//默认用户名和密码
String okName = "yierkang";
String okPassword = "123456";
for(int i=2;i>=0;i--) {
System.out.println("请输入用户名:");
String name = sc.nextLine();
System.out.println("请输入密码:");
String password = sc.nextLine();
if(okName.equals(name)&&okPassword.equals(password)) {
System.out.println("登录成功");
break;
}else {
System.out.println("密账号或者密码错误,您还有"+i+"次机会");
}
}
}
}
手机号码屏蔽
package StringTest;
import java.util.Scanner;
public class Telephone {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入您的手机号:");
String number = sc.nextLine();
String head = number.substring(0,3);//截取第123个数字
String end = number.substring(7);//截取第8(包括)位之后的数字
System.out.println(head+"****"+end);
}
}
运行结果