zoukankan      html  css  js  c++  java
  • day07

    第一题:
    定义一个字符串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变为字符数组,遍历数组将每个字符打印在控制台上

     
    package com.ben.day08;
    
    public class Demo1 {
        public static void main(String[] args) {
            String s="Hello-World";
            System.out.println("与字符串World是否相等"+"World".equals(s));
            System.out.println("字符串Wo,在字符串s中的起始索引"+s.indexOf("Wo"));
            System.out.println("3号索引对应的字符:"+s.charAt(3));
            System.out.println("s的长度:"+s.length());
            System.out.println("获得s中的"Hell"字符串:"+s.substring(0,4));
            System.out.println("获得s中的"orld"字符串:"+s.substring(7,11));
            System.out.println("将字符串s中的所有o替换为*号"+s.replace('o','*'));
           String[] a= s.split("-");
            System.out.println("切割成"Hello"和"World":");
            for (int i = 0; i <a.length ; i++) {
                System.out.println(a[i]);
            }
    
            char[] b=s.toCharArray();
            for (int i = 0; i < b.length; i++) {
                System.out.print(b[i]);
            }
    
        }
    }
     


    第二题:
    1.键盘录入一个字符串
    2.统计录入的字符串中的大写字母,小写字母,数字分别有多少个.

     
    package com.ben.day08;
    
    import java.util.Scanner;
    
    public class Demo2 {
        public static void main(String[] args) {
            Scanner sc=new Scanner(System.in);
            System.out.print("请输入:");
            String str=sc.next();
            char[] chars = str.toCharArray();
            int daXie=0;
            int xiaoXie=0;
            int num=0;
            for (int i = 0; i < str.length(); i++) {
    
                char cha=chars[i];
                if (cha>='a'&&cha<='z') {
                    xiaoXie++;
                }else if (cha >='A'&&cha<='Z') {
                    daXie++;
                }else if (cha >= '0'&&cha<='9') {
                    num++;
                }
            }
            System.out.println("大写字符数:"+daXie);
            System.out.println("小写字符数:"+xiaoXie);
            System.out.println("数字字符数:"+num);
        }
    }
     



    第三题:
    1.键盘录入5个字符串,组成一个数组
    2.统计录入的字符串数组中的大写字母,小写字母,数字分别有多少个.

     
    package com.ben.day08;
    
    import java.util.Scanner;
    
    public class Demo3 {
        public static void main(String[] args) {
            Scanner sc=new Scanner(System.in);
            System.out.print("第一个字符串:");
            String str1=sc.next();
    
            System.out.print("第二个字符串:");
            String str2=sc.next();
    
            System.out.print("第三个字符串:");
            String str3=sc.next();
    
            System.out.print("第四个字符串:");
            String str4=sc.next();
    
            System.out.print("第五个字符串:");
            String str5=sc.next();
    
            String str6="";
                str6=str6.concat(str1);
            str6=str6.concat(str2);
            str6=str6.concat(str3);
            str6=str6.concat(str4);
            str6=str6.concat(str5);
    
            char[] chars = str6.toCharArray();
            int daXie=0;
            int xiaoXie=0;
            int num=0;
            for (int i = 0; i < str6.length(); i++) {
    
                char cha=chars[i];
                if (cha>='a'&&cha<='z') {
                    xiaoXie++;
                }else if (cha >='A'&&cha<='Z') {
                    daXie++;
                }else if (cha >= '0'&&cha<='9') {
                    num++;
                }
            }
            System.out.println("大写字符数:"+daXie);
            System.out.println("小写字符数:"+xiaoXie);
            System.out.println("数字字符数:"+num);
    
        }
    }
     

    第四题:
    1.键盘录入一个字符串
    2.将该字符串变成字符数组
    3.将字符数组中的所有大写字母变成小写字母
    4.如果第一位和最后一位的内容不相同,则交换
    5.将字符数组中索引为偶数的元素变成'~'
    6.打印数组元素的内容
    ------------------------------
    【结果展示】
    请输入字符串
    abcDEf719
    最终显示的效果
    ~b~d~f~1~

     
    package com.ben.day08;
    
    import java.util.Scanner;
    
    public class Demo4 {
        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 val=chars[0];
                chars[0]=chars[chars.length-1];
                chars[chars.length-1]=val;
            }
            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个字符组成一个新的字符串.打印到控制台上

     
    package com.ben.day08;
    
    import java.util.Random;
    import java.util.Scanner;
    
    public class Demo5 {
        public static void main(String[] args) {
            Scanner sc=new Scanner(System.in);
            System.out.print("请输入:");
            String val=sc.next();
            Random ra=new Random();
            String str="";
    
            for (int i = 0; i < 3; i++) {
                str+= val.charAt(ra.nextInt(val.length()));
            }
            System.out.println(str);
    
        }
    }
     


    第六题:
    1.创建一个集合,往集合中键盘录入5个字符串
    2.遍历集合,将集合中长度大于4的元素末尾加上一个X,
    3.遍历集合,将集合打印在控制台上.
    例:键盘录入后的集合{"123","ASDFQ","qq","poiuy","asd"}
    打印到控制台上的集合{"123","ASDFQX","qq","poiuyX","asd"}

     
    package com.ben.day08;
    
    import java.util.Scanner;
    
    public class Demo6 {
        public static void main(String[] args) {
            String[] strs=new String[5];
            Scanner sc=new Scanner(System.in);
    
            for (int i = 1; i <=strs.length ; i++) {
                System.out.print("请输入第"+i+"组字符串:");
                strs[i-1]=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 == args.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"

     
    package com.ben.day08;
    
    public class Demo7 {
        public static String getPropertyGetMethodName(String property){
            char cha=property.charAt(0);
            String str2=Character.toTitleCase(cha)+property.substring(1,property.length());
            String str3="get"+str2;
            return str3;
        }
    
        public static String getPropertySetMethodName(String property){
            char cha=property.charAt(0);
            String str2=Character.toTitleCase(cha)+property.substring(1,property.length());
            String str3="set"+str2;
            return str3;
        }
    
    
        public static void main(String[] args) {
            System.out.println(getPropertyGetMethodName("name"));
            System.out.println(getPropertySetMethodName("name"));
        }
    }
     

    第八题:
    完成下列题目要求:
    ①定义方法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"

     
    package com.ben.day08;
    
    public class Demo8 {
    
        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 ;
        }
        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]+""");
                }
            }
        }
    }
     



    第九题:
    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]

     
    package com.ben.day08;
    
    import java.util.ArrayList;
    
    public class Demo9 {
        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;
        }
    
        public static void main(String[] args) {
    
            String[] arr={"beijing", "shanghai", "tianjin", "chongqing"};
            ArrayList<String> arr2 = handleString(arr, "a");
            System.out.println(arr2);
    
        }
    }
     

    第十题:
    1.定义一个工具类MathUtils,包含一个静态方法add,功能是:求两个数之和,并将其返回.
    2.在测试类中的主方法中测试自己定义的工具类,能通过 类名.方法 调用add方法,计算两个数的和

     
    package com.ben.day08;
    
    public class MathUtils {
        static int add(int num1,int num2){
            return num1+num2;
        }
    }
     
     
    package com.ben.day08;
    
    public class Demo10 {
        public static void main(String[] args) {
    
            System.out.println(MathUtils.add(1,3));
        }
    
    }
  • 相关阅读:
    帝国 标签模板 使用程序代码 去除html标记 并 截取字符串
    iis6 伪静态 iis配置方法 【图解】
    您来自的链接不存在 帝国CMS
    帝国cms Warning: Cannot modify header information headers already sent by...错误【解决方法】
    .fr域名注册 51元注册.fr域名
    帝国网站管理系统 恢复栏目目录 建立目录不成功!请检查目录权限 Godaddy Windows 主机
    星外虚拟主机管理平台 开通数据库 出现Microsoft OLE DB Provider for SQL Server 错误 '8004' 从字符串向 datetime 转换失败
    ASP.NET 自定义控件学习研究
    CSS层叠样式表之CSS解析机制的优先级
    ASP.NET程序员工作面试网络收藏夹
  • 原文地址:https://www.cnblogs.com/rwyy/p/13848947.html
Copyright © 2011-2022 走看看