zoukankan      html  css  js  c++  java
  • 第七次作业!

    
    

    必做题:

    第一题:

        定义一个字符串s = "Hello-World",利用API完成如下小需求
            1.判断字符串s,与字符串"World"是否相等,并打印出来.
            2.用程序得到字符串"Wo",在字符串s中的起始索引.
            3.得到s中,3号索引对应的字符,打印到控制台上
            4.得到s的长度,打印在控制台上.
            5.获得s中的"Hell"字符串,打印在控制台上.
            6.获得s中的"orld"字符串,打印在控制台上.
            7.将字符串s中的所有o替换为*号.打印在控制台上
            8.将字符串s切割成"Hello"和"World"两个字符串,打印在控制台上
            9.将字符串s变为字符数组,遍历数组将每个字符打印在控制台上
    public static void main(String[] args) {
            String s="Hello-World";
            System.out.println("World".equals(s));
            System.out.println(s.substring(6,8));
            //System.out.println(s.indexOf("Wo"));
            System.out.println(s.charAt(3));
            System.out.println(s.length());
            System.out.println(s.substring(0,4));
            System.out.println(s.substring(7,11));
            System.out.println(s.replace("o","*"));
            String[] sp = s.split("-");
            for (int i = 0; i < sp.length; i++) {
                System.out.println(sp[i]);
            }
            char[] ch=s.toCharArray();
            for (int i = 0; i < ch.length; i++) {
                System.out.println(ch[i]);
            }
        }
        
    第二题:
        1.键盘录入一个字符串
        2.统计录入的字符串中的大写字母,小写字母,数字分别有多少个.
    public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            System.out.println("请输入字符串:");
            String str = sc.nextLine();
            char[] chs = str.toCharArray();//toCharArray()方法将字符串转换为字符数组
            int bigNum = 0;
            int smallNum = 0;
            int numberNum = 0;
            int otherNum = 0;
            for(int i=0;i<chs.length;i++){
                if(chs[i]>='A'&&chs[i]<='Z'){
                    bigNum++;
                }else if(chs[i]>='a'&&chs[i]<='z'){
                    smallNum++;
                }else if(chs[i]>='0'&&chs[i]<='9'){
                    numberNum++;
                }else{
                    otherNum++;
                }
            }
            System.out.println("大写字母个数:"+bigNum);
            System.out.println("小写字母个数:"+smallNum);
            System.out.println("数字个数:"+numberNum);
            System.out.println("其他字符个数:"+otherNum);
        }
        
    第三题:
        1.键盘录入5个字符串,组成一个数组
        2.统计录入的字符串数组中的大写字母,小写字母,数字分别有多少个.
    public static void main(String[] args) {
            Scanner scanner=new Scanner(System.in);
            System.out.println("第一个字符串:");
            String s1 = scanner.next();
            System.out.println("第二个字符串:");
            String s2 = scanner.next();
            System.out.println("第三个字符串:");
            String s3 = scanner.next();
            System.out.println("第四个字符串:");
            String s4 = scanner.next();
            System.out.println("第五个字符串:");
            String s5 = scanner.next();
            String s6="";
            s6 = s6.concat(s1);
            s6 = s6.concat(s2);
            s6 = s6.concat(s3);
            s6 = s6.concat(s4);
            s6 = s6.concat(s5);
            char[]chars=s6.toCharArray();
            int bigNum=0;
            int smallNum=0;
            int num=0;
            for (int i = 0; i < s6.length(); i++) {
                char ch=chars[i];
                if (ch>='a'&&ch<='z') {
                    smallNum++;
                }else if (ch>='A'&&ch<='Z') {
                    bigNum++;
                }else if (ch>='0'&&ch<='9') {
                    num++;
                }
            }
            System.out.println("大写字符数:"+bigNum);
            System.out.println("小写字符数:"+smallNum);
            System.out.println("数字字符数:"+num);
        }
    
    第四题:
        1.键盘录入一个字符串
        2.将该字符串变成字符数组
        3.将字符数组中的所有大写字母变成小写字母
        4.如果第一位和最后一位的内容不相同,则交换
        5.将字符数组中索引为偶数的元素变成'~'
        6.打印数组元素的内容
        ------------------------------
        【结果展示】
                请输入字符串
                    abcDEf719
                最终显示的效果
                    ~b~d~f~1~
    public static void main(String[] args) {
            Scanner sc=new Scanner(System.in);
            System.out.println("请输入:");
            String str = sc.next();
            char[] chars = str.toCharArray();
            for (int i = 0; i < chars.length; i++) {
                if (chars[i] >= 'A'&&chars[i]<='Z') {
                    chars[i]=Character.toLowerCase(chars[i]);
                }
            }
            if (chars[0] != chars[chars.length-1]) {
                char ch=chars[0];
                chars[0] = chars[chars.length-1];
                chars[chars.length-1]=ch;
            }
            for (int i = 0; i < chars.length; i++) {
                if (i%2==0) {
                    chars[i]='~';
                }
            }
            for (int i = 0; i < chars.length; i++) {
                System.out.print(chars[i]);
            }
        }    
    
    第五题:
        1.键盘录入一个字符串
        2.从字符串中随机获取3次字符,将获取的3个字符组成一个新的字符串.打印到控制台上
    public static void main(String[] args) {
            Scanner sc=new Scanner(System.in);
            System.out.println("请输入:");
            String val = sc.next();
            Random random=new Random();
            String str="";
            for (int i = 0; i < 3; i++) {
                str+=val.charAt(random.nextInt(val.length()));
            }
            System.out.println(str);
        }
        
    第六题:
        1.创建一个集合,往集合中键盘录入5个字符串
        2.遍历集合,将集合中长度大于4的元素末尾加上一个X,
        3.遍历集合,将集合打印在控制台上.
        例:键盘录入后的集合{"123","ASDFQ","qq","poiuy","asd"}
        打印到控制台上的集合{"123","ASDFQX","qq","poiuyX","asd"}
    public static void main(String[] args) {
            String[] strs=new String[5];
            Scanner sc=new Scanner(System.in);
            for (int i = 0; i < strs.length; i++) {
                System.out.println("请输入第"+(i+1)+"组字符串:");
                strs[i]=sc.next();
            }
            for (int i = 0; i < strs.length; i++) {
                if (strs[i].length()>4) {
                    strs[i]=strs[i]+"X";
                }
            }
            System.out.print("{");
            for (int i = 0; i < strs.length; i++) {
                if (i == strs.length-1) {
                    System.out.print(strs[i]+"}");
                }else {
                    System.out.print(strs[i]+",");
                }
            }
        }
        
    第七题:
        分析以下需求,并用代码实现
        1.定义如下方法public static String getPropertyGetMethodName(String property)
            功能描述:
                (1)该方法的参数为String类型,表示用户传入的参数,返回值类型为String类型,返回值为对应的get方法的名字
                (2)如:用户调用此方法时传入参数为"name",该方法的返回值为"getName"
                                       传入参数为"age",该方法的返回值为"getAge"
                                    
        2.定义如下方法public static String getPropertySetMethodName(String property)
            功能描述:
                (1)该方法的参数为String类型,表示用户传入的参数,返回值类型为String类型,返回值为对应的set方法的名字
                (2)如:用户调用此方法时传入参数为"name",该方法的返回值为"setName"
                                       传入参数为"age",该方法的返回值为"setAge"
    public static void main(String[] args) {
            System.out.println(getPropertyGetMethodName("name"));
            System.out.println(getPropertySetMethodName("age"));
        }
        public static String getPropertyGetMethodName(String property) {
            char c = property.charAt(0);
            String str=Character.toTitleCase(c)+property.substring(1,property.length());
            String str1="get"+str;
            return str1;
        }
        public static String getPropertySetMethodName(String property){
            char c=property.charAt(0);
            String str=Character.toTitleCase(c)+property.substring(1,property.length());
            String str1="set"+str;
            return str1;
        }                           
    
    第八题:
        完成下列题目要求:
            ①定义方法filter
                要求如下:
                    参数:String [] arr,String  str
                    返回值类型:String []
                实现:遍历arr,将数组中包含参数str的元素存入另一个String 数组中并返回
                PS:返回的数组长度需要用代码获取
            ②在main方法中完成以下要求:
                定义一个String数组arr,数组元素有:"itcast","itheima","baitdu","weixin","zhifubao"
                调用1中的filter方法传入arr数组和字符串”it”,输出返回的String数组中所有元素
            示例如下:
                输出的数组中的元素:
                "itcast","itheima","baitdu"
    public static void main(String[] args) {
            String[] arr={"itcast","itheima","baitdu","weixin","zhifubao"};
            String[] filter=filter(arr,"it");
            for (int i = 0; i < filter.length; i++) {
                if (i != filter.length-1) {
                    System.out.print("""+filter[i]+"",");
                }else {
                    System.out.print("""+filter[i]+""");
                }
            }
        }
        public static String[] filter(String[] arr,String str) {
            int val=0;
            int val1=0;
            for (int i = 0; i < arr.length; i++) {
                if (arr[i].indexOf(str)!=-1) {
                    val++;
                }
            }
            String[] str1=new String[val];
            for (int i = 0; i < str1.length; i++) {
                if (arr[i].indexOf(str)!=-1) {
                    str1[val1]=arr[i];
                    val1++;
                }
            }
            return str1;
        }
                
    第九题:
        a.定义方法public static ArrayList<String>  handleString(String [] arr,String str);
            实现以下功能:
                遍历arr,将数组中包含参数str的元素,含有str的部分替换为*, 存入另一个新String 集合中,将新集合返回;
        b.在main方法中完成以下要求:
            1)定义一个String数组arr,数组元素有:"beijing", "shanghai", "tianjin", "chongqing"2)调用handleString方法传入arr数组和字符串”a”,输出返回的String集合中所有元素;
         
        示例如下:
                控制台输出元素如下:
                [sh*ngh*i,ti*njin]
    public static void main(String[] args) {
            String[] arr={"beijing", "shanghai", "tianjin", "chongqing"};
            ArrayList<String> arrayList=handleString(arr,"a");
            System.out.println(arrayList);
        }
        public static ArrayList<String> handleString(String[] arr,String str){
            ArrayList<String> arr1=new ArrayList<String>();
            for (int i = 0; i < arr.length; i++) {
                if (arr[i].indexOf(str)!=-1) {
                    arr[i]=arr[i].replace(str,"*");
                    arr1.add(arr[i]);
                }
            }
            return arr1;
        }
    
    练习题:
    ========================================================    
    第十题:
        1.定义一个工具类MathUtils,包含一个静态方法add,功能是:求两个数之和,并将其返回.
        2.在测试类中的主方法中测试自己定义的工具类,能通过 类名.方法  调用add方法,计算两个数的和
    public class Demo1 {
        public static void main(String[] args) {
            System.out.println(MathUtils.add(3,5));
        }
    }    
    public class MathUtils {
        public static int add(int num1,int num2){
            return  num1+num2;
        }
    }
  • 相关阅读:
    LLC半桥谐振变换器调试记录
    工业派学习记录
    ubuntu 命令记录
    电容单位换算
    Windows快捷键
    CAN总线学习笔记
    Scala 基础(十五):Scala 模式匹配(三)
    Scala 基础(十四):Scala 模式匹配(二)
    Scala 基础(十三):Scala 模式匹配(一)
    scala 数据结构(十一):流 Stream、视图 View、线程安全的集合、并行集合
  • 原文地址:https://www.cnblogs.com/gxt123/p/13848912.html
Copyright © 2011-2022 走看看