java 密码_java笔试之简单密码

packagetest;importjava.util.Scanner;public classexam13 {public static voidmain(String[] args) {

Scanner scanner= newScanner(System.in);while(scanner.hasNext()) {

String str=scanner.nextLine();//char[] pwd = str.toCharArray();//System.out.println(truepwd(str).toString());

System.out.println(truepwd2(str));

}

scanner.close();

}//方法1:if判断

public staticStringBuilder truepwd(String str) {

StringBuilder sbBuilder= newStringBuilder();for (int i = 0; i < str.length(); i++) {char ch =str.charAt(i);if (ch >= 'A' && ch < 'Z') {

ch= (char) (ch + 1 - ('A' - 'a'));

}else if (ch == 'Z') {

ch= 'a';

}else if (ch >= 'a' && ch <= 'z') {if (ch == 'a' || ch == 'b' | ch == 'c') {

ch= '2';

}else if (ch == 'd' || ch == 'e' || ch == 'f') {

ch= '3';

}else if (ch == 'g' || ch == 'h' || ch == 'i') {

ch= '4';

}else if (ch == 'j' || ch == 'k' || ch == 'l') {

ch= '5';

}else if (ch == 'm' || ch == 'n' || ch == 'o') {

ch= '6';

}else if (ch == 'p' || ch == 'q' || ch == 'r' || ch == 's') {

ch= '7';

}else if (ch == 't' || ch == 'u' || ch == 'v') {

ch= '8';

}else if (ch == 'w' || ch == 'x' || ch == 'y' || ch == 'z') {

ch= '9';

}

}

sbBuilder.append(ch);

}returnsbBuilder;

}//方法2:使用正则表达式

public staticString truepwd2(String str) {

StringBuffer sBuffer= newStringBuffer(str);for (int i = 0; i < str.length(); i++) {char ch =sBuffer.charAt(i);if (ch >= 'A' && ch < 'Z') {

ch= (char) (ch + 1 - ('A' - 'a'));

}else if (ch == 'Z') {

ch= 'a';

}else if (ch >= 'a' && ch <= 'z') {if (String.valueOf(ch).matches("[abc]")) {

ch= '2';

}else if (String.valueOf(ch).matches("[def]")) {

ch= '3';

}else if (String.valueOf(ch).matches("[ghi]")) {

ch= '4';

}else if (String.valueOf(ch).matches("[jkl]")) {

ch= '5';

}else if (String.valueOf(ch).matches("[mno]")) {

ch= '6';

}else if (String.valueOf(ch).matches("[pqrs]")) {

ch= '7';

}else if (String.valueOf(ch).matches("[tuv]")) {

ch= '8';

}else if (String.valueOf(ch).matches("[wxyz]")) {

ch= '9';

}

}

sBuffer.setCharAt(i, ch);

}returnsBuffer.toString();

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值