LOADING

正在加载喵~

week5

2025/3/21 未来与梦 未标记

[collapse title”第一题”] java public class Ex4_1 { String str; public Ex4_1(String str) { this.str str; } int getlength() { return str.length(); } public static void main(String[] arg…

[collapse title=”第一题”]

public class Ex4_1 {
    String str;
    
    public Ex4_1(String str) {
        this.str = str;
    }
    
    int getlength() {
        return str.length();
    }

    public static void main(String[] args) {
        Ex4_1 test = new Ex4_1("Hello");
        System.out.println("字符串是:" + test.str + " 长度为:" + test.getlength());
    }
}    

[/collapse]

[collapse title=”第二题”]

import java.util.*;
import java.io.*;

public class Ex4_2 {
    public static void main(String[] args) {
        String str1 = new String();
        String str2 = new String();
        char ch;
        Scanner reader = new Scanner(System.in);
        System.out.println("输入字符串:");
        str1 = reader.nextLine();  //输入字符串
        System.out.println("输入要删除的字符:");
        str2 = reader.nextLine();  //输入穿要删除的字符 ,以字符串的形式输入
        ch = str2.charAt(0);   //将字符串str2转换为字符
        str1 = str1.replace(ch, ' ');  //用空格替代指定字符
        System.out.println("删除字符后的字符串  " + str1);
    }
}

[/collapse]

[collapse title=”第三题”]

class Ex4_3 {
    public static void main(String[] args) {
        String s = "cat223dog456nice25ttt98";
        boolean old_tag = false;  // 表示开始不是数字
        boolean tag = false;
        int n = 0; // 数字组计数
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            tag = c >= '0' && c <= '9';  // 是否为数字
            if (!old_tag && tag) n++;
            old_tag = tag;
        }
        System.out.print("共有数字段 " + n + " 个");
    }
}

[/collapse]