<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                >[danger] ##### 循環字符串并記錄 1. 鍵盤錄入一個字符串,使用程序實現在控制臺遍歷該字符串,統計出這個字符串的大小寫和數字的個數 * 注利用了 char 比較實際比較的是,自動類型提升為`int `查詢`ascii`碼表的技巧進行比較 ~~~ import java.util.Scanner; public class StringTest { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String str = sc.next(); // 獲取字符串長度 int strLen = str.length(); // 統計記錄 int numCount = 0; int bigCount = 0; int smallCount = 0; for (int i = 0; i < strLen; i++) { char c = str.charAt(i); if (c >= 'a' && c <= 'z') { // char類型的變量在參與計算的時候自動類型提升為int 查詢ascii碼表 smallCount++; } else if (c >= 'A' && c <= 'Z') { bigCount++; } else if (c >= '0' && c <= '9') { numCount++; } } // 3.輸出打印 System.out.println("小寫字母有:" + smallCount + "個"); System.out.println("大寫字母有:" + bigCount + "個"); System.out.println("數字字母有:" + numCount + "個"); } } ~~~ >[danger] ##### 錢轉換為中文表示 ~~~ import java.util.Scanner; public class StringTest { public static void main(String[] args) { // 1.鍵盤錄入一個金額 Scanner sc = new Scanner(System.in); int money; while (true) { System.out.println("請錄入一個金額"); money = sc.nextInt(); if (money >= 0 && money <= 9999999) { break; } else { System.out.println("金額無效"); } } // 定義一個變量用來表示錢的大寫 String moneyStr = ""; // 2.得到money里面的每一位數字,再轉成中文 while (true) {// 2135 // 從右往左獲取數據,因為右側是數據的個位 int ge = money % 10; String capitalNumber = getCapitalNumber(ge); // 把轉換之后的大寫拼接到moneyStr當中 moneyStr = capitalNumber + moneyStr; // 第一次循環 : "伍" + "" = "伍" // 第二次循環 : "叁" + "伍" = "叁伍" // 去掉剛剛獲取的數據 money = money / 10; // 如果數字上的每一位全部獲取到了,那么money記錄的就是0,此時循環結束 if (money == 0) { break; } } // 3.在前面補0,補齊7位 int count = 7 - moneyStr.length(); for (int i = 0; i < count; i++) { moneyStr = "零" + moneyStr; } System.out.println(moneyStr);// 零零零貳壹叁伍 // 4.插入單位 // 定義一個數組表示單位 String[] arr = { "佰", "拾", "萬", "仟", "佰", "拾", "元" }; // 零 零 零 貳 壹 叁 伍 // 遍歷moneyStr,依次得到 零 零 零 貳 壹 叁 伍 // 然后把arr的單位插入進去 String result = ""; for (int i = 0; i < moneyStr.length(); i++) { char c = moneyStr.charAt(i); // 把大寫數字和單位拼接到result當中 result = result + c + arr[i]; } // 5.打印最終結果 System.out.println(result); } // 定義一個方法把數字變成大寫的中文 // 1 -- 壹 public static String getCapitalNumber(int number) { // 定義數組,讓數字跟大寫的中文產生一個對應關系 String[] arr = { "零", "壹", "貳", "叁", "肆", "伍", "陸", "柒", "捌", "玖" }; // 返回結果 return arr[number]; } } ~~~ >[danger] ##### 將 字符串轉換 為數字 1. 將字符串 **"123" => 123** ~~~ public class TestStr { public static void main(String[] args) { String str = "123"; // 直接轉換 int num = Integer.parseInt(str); System.out.println(num); // 123 // 通過循環累計計算 int num1 = 0; for (int i = 0; i < str.length(); i++) { // 從高位 依次累加到 個位 num1 * 10 每次擴大都會讓 // 最先循環的數字 擴大十倍,最后擴大倍速和循環次數有關 num1 = num1 * 10 + (str.charAt(i) - '0'); // char - char = int '1' - '0' => 1 '2' - '0' => 2 ... } System.out.println(num1); // 123 } } ~~~ >[danger] ##### 數字轉換為字符串 ~~~ public class TestStr { public static void main(String[] args) { int num = 123; System.out.println(String.valueOf(num)); // 123 String str1 = "" + num; // 推薦使用 System.out.println(String.valueOf(str1)); // 123 } } ~~~ >[danger] ##### 判斷是否是回文字符串 1. 謂回文是指一個字符序列無論從左向右讀還是從右向左讀都是相同的字符串 ~~~ public class TestStr { public static void main(String[] args) { String str = "abcba"; int strLen = str.length(); for (int i = 0; i < strLen / 2; i++) { if (str.charAt(i) != str.charAt(strLen - 1 - i)) { System.out.println("非回文"); break; } } System.out.println("回文"); } } ~~~ >[danger] ##### 實現用戶偽登錄功能 ~~~ import java.util.Scanner; public class TestStr { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int counts = 3; while (counts-- > 0) { String pwd = sc.next(); String userName = sc.next(); if (pwd.equals("123") && userName.equals("admin")) { System.out.println("歡迎登陸"); break; } if (0 == counts) { System.out.println("賬戶已凍結,請聯系客服人員!"); } else { System.out.println("用戶名或密碼錯誤,您還有" + counts + "次機會!"); } } // 關閉掃描器 sc.close(); } } ~~~ >[danger] ##### 身份證轉換 ~~~ public class StringTest { public static void main(String[] args) { String id = "321281202001011234"; // 獲取年月日 String year = id.substring(6, 10); String month = id.substring(10, 12); String day = id.substring(12, 14); System.out.println("人物信息為:"); System.out.println("出生年月日:" + year + "年" + month + "月" + day + "日"); // 獲取性別 char gender = id.charAt(16);// '3' ---> 3 // 獲取的性別是一個 char 類型 為字符類型,因此字符轉換為數字雷勇ascii // 利用ASCII碼表進行轉換 // '0' ---> 48 // '1' ---> 49 // '2' ---> 50 // '3' ---> 51 // '4' ---> 52 // '5' ---> 53 // '6' ---> 54 // '7' ---> 55 // '8' ---> 56 // '9' ---> 57 int num = gender - 48; if (num % 2 == 0) { System.out.println("性別為:女"); } else { System.out.println("性別為:男"); } } } ~~~ >[danger] ##### 文字替換 ~~~ public class StringTest { public static void main(String[] args) { // 替換敏感詞 String talk = "你玩的真好,以后不要再玩了,TMD,CNM"; // 敏感詞數組 String[] strLs = { "TMD", "CNM", "GAD" }; for (int i = 0; i < strLs.length; i++) { talk = talk.replaceAll(strLs[i], "***"); } System.out.print(talk); } } ~~~ >[danger] ##### 羅馬數字轉換 ~~~ import java.util.Scanner; public class StringTest { public static void main(String[] args) { /* * 鍵盤錄入一個字符串, * 要求1:長度為小于等于9 * 要求2:只能是數字 * 將內容變成羅馬數字 * 下面是阿拉伯數字跟羅馬數字的對比關系: * Ⅰ-1、Ⅱ-2、Ⅲ-3、Ⅳ-4、Ⅴ-5、Ⅵ-6、Ⅶ-7、Ⅷ-8、Ⅸ-9 * 注意點: * 羅馬數字里面是沒有0的 * 如果鍵盤錄入的數字包含0,可以變成""(長度為0的字符串) */ Scanner sc = new Scanner(System.in); String str = sc.next(); Boolean flag = checkStr(str); // 字符串拼接使用StringBuilder StringBuilder sb = new StringBuilder(); if (flag) { for (int i = 0; i < str.length(); i++) { char c = str.charAt(i); int index = c - 48; // 利用ascii 獲取實際值 String s = changeLuoMa(index); sb.append(s); } } System.out.println(sb); } // 檢查輸入的是否是數字 public static boolean checkStr(String str) { // 判讀長度不能大于9 if (str.length() > 9) { return false; } for (int i = 0; i < str.length(); i++) { char c = str.charAt(i); if (c < '0' || c > '9') { return false; } } return true; } // 映射查詢 public static String changeLuoMa(int number) { String[] arr = { "", "Ⅰ", "Ⅱ", "Ⅲ", "Ⅳ", "Ⅴ", "Ⅵ", "Ⅶ", "Ⅷ", "Ⅸ" }; return arr[number]; } } ~~~ * 使用switch ~~~ import java.util.Scanner; public class Test1Case2 { public static void main(String[] args) { /* 鍵盤錄入一個字符串, 要求1:長度為小于等于9 要求2:只能是數字 將內容變成羅馬數字 下面是阿拉伯數字跟羅馬數字的對比關系: Ⅰ-1、Ⅱ-2、Ⅲ-3、Ⅳ-4、Ⅴ-5、Ⅵ-6、Ⅶ-7、Ⅷ-8、Ⅸ-9 注意點: 羅馬數字里面是沒有0的 如果鍵盤錄入的數字包含0,可以變成""(長度為0的字符串)*/ //1.鍵盤錄入一個字符串 //書寫Scanner的代碼 Scanner sc = new Scanner(System.in); String str; while (true) { System.out.println("請輸入一個字符串"); str = sc.next(); //2.校驗字符串是否滿足規則 boolean flag = checkStr(str); if (flag) { break; } else { System.out.println("當前的字符串不符合規則,請重新輸入"); continue; } } //將內容變成羅馬數字 //下面是阿拉伯數字跟羅馬數字的對比關系: //Ⅰ-1、Ⅱ-2、Ⅲ-3、Ⅳ-4、Ⅴ-5、Ⅵ-6、Ⅶ-7、Ⅷ-8、Ⅸ-9 //查表法:數字跟數據產生一個對應關系 StringBuilder sb = new StringBuilder(); for (int i = 0; i < str.length(); i++) { char c = str.charAt(i); String s = changeLuoMa(c); sb.append(s); } System.out.println(sb); } //利用switch進行匹配 public static String changeLuoMa(char number) { String str = switch (number) { case '0' -> ""; case '1' -> "Ⅰ"; case '2' -> "Ⅱ"; case '3' -> "Ⅲ"; case '4' -> "Ⅳ"; case '5' -> "Ⅴ"; case '6' -> "Ⅵ"; case '7' -> "Ⅶ"; case '8' -> "Ⅷ"; case '9' -> "Ⅸ"; default -> str = ""; }; return str; } public static boolean checkStr(String str) {//123456 //要求1:長度為小于等于9 if (str.length() > 9) { return false; } //要求2:只能是數字 for (int i = 0; i < str.length(); i++) { char c = str.charAt(i);//0~9 if (c < '0' || c > '9') { return false; } } //只有當字符串里面所有的字符全都判斷完畢了,我才能認為當前的字符串是符合規則 return true; } } ~~~ >[danger] ##### 字符串比較 ~~~ public class StringTest { public static void main(String[] args) { // 1.定義兩個字符串 String strA = "abcde"; String strB = "ABC"; // 2.調用方法進行比較 boolean result = check(strA, strB); // 3.輸出 System.out.println(result); } public static boolean check(String strA, String strB) { for (int i = 0; i < strA.length(); i++) { strA = rotate(strA); if (strA.equals(strB)) { return true; } } return false; } // 旋轉比較 public static String rotate(String str) { // 使用字符拼接 char first = str.charAt(0); String end = str.substring(1); return first + end; } // 選擇比較另外一種形式使用數組 public static String rotate1(String str) { // 將字符串變為數組 char[] c = str.toCharArray(); char first = c[0]; for (int i = 1; i < c.length; i++) { c[i - 1] = c[i]; } c[c.length - 1] = first; // 利用字符數組創建一個字符串對象 String result = new String(c); return result; } } ~~~ >[danger] ##### 將字符串隨機打亂 ~~~ import java.util.Random; import java.util.Scanner; public class StringTest { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String str = sc.next(); // 將字符串轉換為 char 數組 char[] c = str.toCharArray(); // 循環隨機打亂數組 Random rd = new Random(); for (int i = 0; i < c.length; i++) { int index = rd.nextInt(c.length); char temp = c[i]; c[i] = c[index]; c[index] = temp; } str = new String(c); System.out.print(str); } } ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看