Java(肆)
第五周課程總結(jié):
? ?
實(shí)驗(yàn)三?String類的應(yīng)用
- 實(shí)驗(yàn)?zāi)康?/strong>
- 掌握類String類的使用;
- 學(xué)會(huì)使用JDK幫助文檔;
- 實(shí)驗(yàn)內(nèi)容
1.已知字符串:"this is a test of java".按要求執(zhí)行以下操作:(要求源代碼、結(jié)果截圖。)
- 1.統(tǒng)計(jì)該字符串中字母s出現(xiàn)的次數(shù)。
- 實(shí)驗(yàn)源碼:
- public class text1 {
?public static void main(String[] args) {
??????? String str="this is a test of java";
??????? int count=0;
??????? char c[]=str.toCharArray();
??????? for(int i=0;i<c.length;i++){
??????????? if(c[i]=='s'){
??????????????? count++;
??????????? }??????????
???????? }
??????? System.out.println("s出現(xiàn)了"+count+"次");
???????????
??? } } - 截圖:
- 2.統(tǒng)計(jì)該字符串中子串“is”出現(xiàn)的次數(shù)。
- 實(shí)驗(yàn)源碼:
- public class texe2 {
?public static void main(String[] args) {
??????? String str = "this is a test of java";
??????? int count = 0, index = 0;
??????? while (true) {
??????????? int n = str.indexOf("is", index);
??????????? if (n != -1) {
??????????????? index = n + 1;
??????????????? count++;
??????????? } else {
??????????????? break;
??????????? } }
??????? System.out.print("is出現(xiàn)了" + count + "次");
??? }
} - 截圖:
- 3.統(tǒng)計(jì)該字符串中單詞“is”出現(xiàn)的次數(shù)
- 實(shí)驗(yàn)源碼:
- public class text3 {
?public static void main(String[] args) {
??????? int count=0;
??????? String str="this is a test of java";
??????? String s[]=str.split(" ");
??????? for(int i=0;i<s.length;i++){
??????????? if(s[i].equals("is")){
??????????????? count++;???????
??????????? } }
??????? System.out.println(count);
??? }
} - 截圖:
。
?
?
- 4.實(shí)現(xiàn)該字符串的倒序輸出。
- 實(shí)驗(yàn)源碼:
- public class text4 {
?public static void main(String[] args) {
??????? String str = "this is a test of java";
??????? char s[] = str.toCharArray();
??????? for (int i=s.length-1;i>=0;i--) {
??????????? System.out.print(s[i]); } }
} - 截圖:
- 總結(jié):老師上課的時(shí)候基本都通過(guò)eclipse為我們演示了一遍,基本操作就是這么一個(gè)情況;
2.請(qǐng)編寫一個(gè)程序,使用下述算法加密或解密用戶輸入的英文字串。要求源代碼、結(jié)果截圖。
?
實(shí)驗(yàn)源碼:
import java.util.*;public class text1 {
?public static void main(String[] args) {
??????? System.out.print("輸入一個(gè)字符串:");
??????? var scanner = new Scanner(System.in);
??String str1 = scanner.nextLine();
??????? System.out.println(str1);
??????? char c[] = str1.toCharArray();
??????? int ASCII;
??????? char c1;
??????? for (int i = 0; i < c.length; i++) {
??????????? ASCII = c[i] + 3;
??????????? // System.out.print(ASCII+" ");
??????????? c1 = (char) ASCII;
??????????? // System.out.print(c1+" ");
??????????? String s = String.valueOf(c1);
??????????? System.out.print(s); } }
} 截圖:
?
?
?3.已知字符串“ddejidsEFALDFfnef2357 3ed”。輸出字符串里的大寫字母數(shù),小寫英文字母數(shù),非英文字母數(shù)。
實(shí)驗(yàn)源碼:
public class text1 {?public static void main(String[] args) {
??????? String str = "adhadhahdhahhH 1231";
??????? char c[] = str.toCharArray();
??????? int count1 = 0, count2 = 0, count3 = 0;
??????? for (int i = 0; i < c.length; i++) {
??????????? int n = (int) c[i];
??????????? if (65 <= n && n <= 90) {
??????????????? count1++;
??????????? } else if (97 <= n && n <= 122) {
??????????????? count2++;
??????????? } else {
??????????????? count3++;
??????????? } }
??????? System.out.println("大寫字母數(shù):" + count1);
??????? System.out.println("小寫字母數(shù):" + count2);
??????? System.out.println("非英文字母數(shù):" + count3); }
?
} 截圖:
?
?
?課程總結(jié):
表格:
繼承(extends);
方法的重載與覆寫的區(qū)別:
單詞 | Overloading | Overriding |
定義 | 方法名稱相同,參數(shù)的類型或個(gè)數(shù)不同 | 方法名稱、參數(shù)類型、返回值類型全部相同 |
定義 | 對(duì)權(quán)限沒(méi)有要求 | 被覆寫的方法中不能有更嚴(yán)格的權(quán)限 |
范圍 | 發(fā)生在一個(gè)類中 | 發(fā)生在繼承中 |
super關(guān)鍵字的使用;
final關(guān)鍵字:類&屬性&方法;
.....受益匪淺.....
樸實(shí)無(wú)華且枯燥
?
轉(zhuǎn)載于:https://www.cnblogs.com/lsy2380821-/p/11598865.html
總結(jié)
- 上一篇: 常用的webpack 配置
- 下一篇: python 将数组中取某一值的元素全部