Java学习-字符串、字符、ASCII、数字的互相转换 1public class test2 {
2public static void main(String[] args) {
3
4// 字符串和字符数组互相转换
5// 字符串转字符数组
6 String str1 = "hiuzb12316";
7char[] cs1 = CharArray();
8 System.out.println(cs1);// 字符串转字符数组:toCharArray()
9
10// 字符数组转字符串
11 String str2 = "";
12for (int i = 0; i < cs1.length; i++) {
13 str2 += cs1[i];
14 }
15 System.out.println(str2);// 字符数组转字符串:遍历,拼接即可
16 //更简单的:String.valueOf(字符数组)
17// ASCII和字符互相转换
18// 0~9:48~57
19// A~Z:65~90
20// a~z:97~122
21
22char a = 'a';
23int numOfa = (int) a;// 强制转换即可,下⾯同理
24
25int x = 100;
26char chOfx = (char) x;
27 System.out.println(numOfa);
28 System.out.println(chOfx);
29
30// 数字和字符互相转换(不能强制转换!)
31// 数字转字符串
32int i = 12345;
33// System.out.println((char) i);// 结果出错,因为ASCII没有12345这么多!!!
34 String str_1 = String.valueOf(i);// 办法⼀:String.valueOf(要转的数字对象)
35 System.out.println(str_1);
36 Integer it = i;// 办法⼆:先”装箱“
37 String str_2 = it.toString();// 再调⽤toSting()即可
38 System.out.println(str_2);
39
40// 字符串转数字
41int res = Integer.parseInt(str_1);// Integer.parseInt(要转的字符串对象)
42 System.out.println(res);
43 }
44 }
进阶运⽤:创建⼀个长度是5的随机字符串,随机字符有可能是数字,⼤写字母或者⼩写字母
运⽤知识点:ASCII与数字的互相转换,随机数
8public static char rand() {
9int a1 = (int) (und(Math.random() * (122 - 97)) + 97);// a-z
10int a2 = (int) (und(Math.random() * (90 - 65)) + 65);// A-Z
11int a3 = (int) (und(Math.random() * (57 - 48)) + 48);// 0-9
12int oneOfThem = (int) und(Math.random() * 2);// 随机决定a1,a2,a3中的⼀个
13
14if (oneOfThem == 0) {
15return (char) a1;
16 } else if (oneOfThem == 1) {
17return (char) a2;
18 } else {
19return (char) a3;
20 }
21 }
22
数组转换成字符串23public static void main(String[] args) {
24char cs[] = new char[5];
25for (int i = 0; i < cs.length; i++) {
26 cs[i] = rand();
27 }
28 System.out.println(cs);
29// 若要字符串格式,把字符数组转换成字符串即可
30 String str = "";
31for (int i = 0; i < cs.length; i++) {
32 str += cs[i];
33 }
34 System.out.println("字符串格式:" + str);
35 }
36 }
运⾏结果:
进阶运⽤2:
创建⼀个长度是8的字符串数组
使⽤8个长度是5的随机字符串初始化这个数组
对这个数组进⾏排序,按照每个字符串的⾸字母排序(⽆视⼤⼩写)
1public class test {
2
3public static char rand() {
4int a1 = (int) (und(Math.random() * (122 - 97)) + 97);// a-z
5int a2 = (int) (und(Math.random() * (90 - 65)) + 65);// A-Z
6int a3 = (int) (und(Math.random() * (57 - 48)) + 48);// 0-9
7int oneOfThem = (int) und(Math.random() * 2);// 随机决定a1,a2,a3中的⼀个
8
9if (oneOfThem == 0) {
10return (char) a1;
11 } else if (oneOfThem == 1) {
12return (char) a2;
13 } else {
14return (char) a3;
15 }
16 }
17
18public static String randString() {
19char cs[] = new char[5];
20 String str = "";
21for (int i = 0; i < 5; i++) {
22 cs[i] = rand();
23 str += cs[i];
24 }
25return str;
26 }
27
28public static void StringArraySort(String str[]) {//采⽤简单选择排序
29int i, j, min;
30 String tmp;
31for (i = 0; i < str.length; i++)
32 {
33 min = i;
34for (j = i + 1; j < str.length; j++)
35 {
36if (LowerCase(str[min].charAt(0)) > LowerCase(str[j].charAt(0)))
37 {
38 min = j;
39 }
40 }
41 tmp=str[i];
42 str[i]=str[min];
43 str[min]=tmp;
44 }
45 }
46
47public static void main(String[] args) {
48
49 String str[] = new String[8];
50for (int i = 0; i < str.length; i++)
51 {
52 str[i] = randString();//⽣成随机字符串
53 }
54 System.out.println(".................排序前...................");
55for (String each : str)
56 {
57 System.out.println(each);
58 }
59 StringArraySort(str);//对随机字符串按⾸字母进⾏排序
60 System.out.println("..............排序后..............");
61for (String each : str)
62 {
63 System.out.println(each);
64 }
65 }
66 }
运⾏结果:
进阶运⽤:穷举法破解密码
1. ⽣成⼀个长度是3的随机字符串,把这个字符串作为当做密码
2. 使⽤穷举法⽣成长度是3个字符串,匹配上述⽣成的密码
要求:分别使⽤多层for循环和递归解决上述问题
1public class test2 {
2public static char randChar() {
3return (char) ((int) und(Math.random() * (126 - 33) + 33));
4 }
5
6public static String qiongJuFa(String passWord) {
7
8 String tryPassWord = "";
9for (int i = 33; i <= 126; i++) {
10for (int j = 33; j <= 126; j++) {
11for (int k = 33; k <= 126; k++) {
12char t1 = (char) i;
13char t2 = (char) j;
14char t3 = (char) k;
15 tryPassWord = "" + t1 + t2 + t3;// 前⾯必须加 ""
16if (t1 == passWord.charAt(0) && t2 == passWord.charAt(1) && t3 == passWord.charAt(2)) {
17 System.out.println("猜测的密码是:" + tryPassWord);
18 System.out.println("匹配成功!");
19
20return tryPassWord;
21 } else {
22 tryPassWord = "";
23 }
24 }
25 }
26 }
27return "";
28 }
29
30public static void recursion(String psw, char[] tpsw, int num) {// num是tpsw第⼏位
31if (num < 3) {
32int i;
33for (i = 33; i <= 126; i++) {// 取的ASCII范围是33-126
34 tpsw[num] = (char) i;
35if (psw.charAt(num) == tpsw[num]) {
36 recursion(psw, tpsw, ++num);// 第⼀位匹配成功,递归调⽤去判断下⼀位
37break;// 递归的“归”的时候,因为已经匹配成功了,⽆须再次循环,所以直接break
38 }
39 }
40 } else {
41 System.out.printf("猜测的密码是:%s%n", String.valueOf(tpsw));
42 System.out.println("猜测成功!");
43 }
44
45 }
46
47public static void main(String[] args) {
48
49 String passWord = "";
50for (int i = 0; i < 3; i++) {
51 passWord += randChar();
52 }
53 System.out.println("原密码:" + passWord);
54
55// 穷举法
56long st1 = System.currentTimeMillis();
57 String tpsw = qiongJuFa(passWord);
58long et1 = System.currentTimeMillis();
59 System.out.println("loop spent " + (et1 - st1) + "ms"); 60// 递归法
61char tryPassWord[] = new char[3];
62long st = System.currentTimeMillis();
63 recursion(passWord, tryPassWord, 0);
64long et = System.currentTimeMillis();
65 System.out.println("recursion spent " + (et - st) + "ms");
66 }
67 }
运⾏结果:
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论