Java學習-字符串、字符、ASCII、數字的互相轉換


 1 public class test2 {
 2     public static void main(String[] args) {
 3 
 4         // 字符串和字符數組互相轉換
 5         // 字符串轉字符數組
 6         String str1 = "hiuzb12316";
 7         char[] cs1 = str1.toCharArray();
 8         System.out.println(cs1);// 字符串轉字符數組:toCharArray()
 9 
10         // 字符數組轉字符串
11         String str2 = "";
12         for (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 
22         char a = 'a';
23         int numOfa = (int) a;// 強制轉換即可,下面同理
24 
25         int x = 100;
26         char chOfx = (char) x;
27         System.out.println(numOfa);
28         System.out.println(chOfx);
29 
30         // 數字和字符互相轉換(不能強制轉換!)
31         // 數字轉字符串
32         int 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         // 字符串轉數字
41         int res = Integer.parseInt(str_1);// Integer.parseInt(要轉的字符串對象)
42         System.out.println(res);
43     }
44 }

 

進階運用:創建一個長度是5的隨機字符串,隨機字符有可能是數字,大寫字母或者小寫字母

運用知識點:ASCII與數字的互相轉換,隨機數

 8     public static char rand() {
 9         int a1 = (int) (Math.round(Math.random() * (122 - 97)) + 97);// a-z
10         int a2 = (int) (Math.round(Math.random() * (90 - 65)) + 65);// A-Z
11         int a3 = (int) (Math.round(Math.random() * (57 - 48)) + 48);// 0-9
12         int oneOfThem = (int) Math.round(Math.random() * 2);// 隨機決定a1,a2,a3中的一個
13 
14         if (oneOfThem == 0) {
15             return (char) a1;
16         } else if (oneOfThem == 1) {
17             return (char) a2;
18         } else {
19             return (char) a3;
20         }
21     }
22 
23     public static void main(String[] args) {
24         char cs[] = new char[5];
25         for (int i = 0; i < cs.length; i++) {
26             cs[i] = rand();
27         }
28         System.out.println(cs);
29         // 若要字符串格式,把字符數組轉換成字符串即可
30         String str = "";
31         for (int i = 0; i < cs.length; i++) {
32             str += cs[i];
33         }
34         System.out.println("字符串格式:" + str);
35     }
36 }

運行結果:

 進階運用2:

創建一個長度是8的字符串數組
使用8個長度是5的隨機字符串初始化這個數組
對這個數組進行排序,按照每個字符串的首字母排序(無視大小寫)

 1 public class test {
 2 
 3     public static char rand() {
 4         int a1 = (int) (Math.round(Math.random() * (122 - 97)) + 97);// a-z
 5         int a2 = (int) (Math.round(Math.random() * (90 - 65)) + 65);// A-Z
 6         int a3 = (int) (Math.round(Math.random() * (57 - 48)) + 48);// 0-9
 7         int oneOfThem = (int) Math.round(Math.random() * 2);// 隨機決定a1,a2,a3中的一個
 8 
 9         if (oneOfThem == 0) {
10             return (char) a1;
11         } else if (oneOfThem == 1) {
12             return (char) a2;
13         } else {
14             return (char) a3;
15         }
16     }
17 
18     public static String randString() {
19         char cs[] = new char[5];
20         String str = "";
21         for (int i = 0; i < 5; i++) {
22             cs[i] = rand();
23             str += cs[i];
24         }
25         return str;
26     }
27 
28     public static void StringArraySort(String str[]) {//采用簡單選擇排序
29         int i, j, min;
30         String tmp;
31         for (i = 0; i < str.length; i++) 
32         {
33             min = i;
34             for (j = i + 1; j < str.length; j++) 
35             {
36                 if (Character.toLowerCase(str[min].charAt(0)) > Character.toLowerCase(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 
47     public static void main(String[] args) {
48 
49         String str[] = new String[8];
50         for (int i = 0; i < str.length; i++) 
51         {
52             str[i] = randString();//生成隨機字符串
53         }
54         System.out.println(".................排序前...................");
55         for (String each : str)
56         {
57             System.out.println(each);
58         }
59         StringArraySort(str);//對隨機字符串按首字母進行排序
60         System.out.println("..............排序后..............");
61         for (String each : str) 
62         {
63             System.out.println(each);
64         }
65     }
66 }

運行結果:

 

進階運用:窮舉法破解密碼

1. 生成一個長度是3的隨機字符串,把這個字符串作為當做密碼
2. 使用窮舉法生成長度是3個字符串,匹配上述生成的密碼

要求: 分別使用多層for循環遞歸解決上述問題

 

 1 public class test2 {
 2     public static char randChar() {
 3         return (char) ((int) Math.round(Math.random() * (126 - 33) + 33));
 4     }
 5 
 6     public static String qiongJuFa(String passWord) {
 7 
 8         String tryPassWord = "";
 9         for (int i = 33; i <= 126; i++) {
10             for (int j = 33; j <= 126; j++) {
11                 for (int k = 33; k <= 126; k++) {
12                     char t1 = (char) i;
13                     char t2 = (char) j;
14                     char t3 = (char) k;
15                     tryPassWord = "" + t1 + t2 + t3;// 前面必須加 ""
16                     if (t1 == passWord.charAt(0) && t2 == passWord.charAt(1) && t3 == passWord.charAt(2)) {
17                         System.out.println("猜測的密碼是:" + tryPassWord);
18                         System.out.println("匹配成功!");
19 
20                         return tryPassWord;
21                     } else {
22                         tryPassWord = "";
23                     }
24                 }
25             }
26         }
27         return "";
28     }
29 
30     public static void recursion(String psw, char[] tpsw, int num) {// num是tpsw第幾位
31         if (num < 3) {
32             int i;
33             for (i = 33; i <= 126; i++) {// 取的ASCII范圍是33-126
34                 tpsw[num] = (char) i;
35                 if (psw.charAt(num) == tpsw[num]) {
36                     recursion(psw, tpsw, ++num);// 第一位匹配成功,遞歸調用去判斷下一位
37                     break;// 遞歸的“歸”的時候,因為已經匹配成功了,無須再次循環,所以直接break
38                 }
39             }
40         } else {
41             System.out.printf("猜測的密碼是:%s%n", String.valueOf(tpsw));
42             System.out.println("猜測成功!");
43         }
44 
45     }
46 
47     public static void main(String[] args) {
48 
49         String passWord = "";
50         for (int i = 0; i < 3; i++) {
51             passWord += randChar();
52         }
53         System.out.println("原密碼:" + passWord);
54 
55         // 窮舉法
56         long st1 = System.currentTimeMillis();
57         String tpsw = qiongJuFa(passWord);
58         long et1 = System.currentTimeMillis();
59         System.out.println("loop spent " + (et1 - st1) + "ms");
60         // 遞歸法
61         char tryPassWord[] = new char[3];
62         long st = System.currentTimeMillis();
63         recursion(passWord, tryPassWord, 0);
64         long et = System.currentTimeMillis();
65         System.out.println("recursion spent " + (et - st) + "ms");
66     }
67 }

運行結果:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM