JAVA基础08

day08作业:

必做题:
============================================================

第一题:
定义一个字符串s = "Hello-World",利用API完成如下小需求
1.判断字符串s,与字符串"World"是否相等,并打印出来.

复制代码
public class Demo1 {
    public static void main(String[] args) {
        String s = "Hello-World";
        if(s=="World"){
            System.out.println(true);
        }else {
            System.out.println(false);
        }
    }
}
复制代码

 


2.用程序得到字符串"Wo",在字符串s中的起始索引.

    public class Demo1 {
        public static void main(String[] args) {
            String s = "Hello-World";
    
            int wo = s.indexOf("Wo");
            System.out.println(wo);
        }
    }

 


3.得到s中,3号索引对应的字符,打印到控制台上

   public class Demo1 {
        public static void main(String[] args) {
            String s = "Hello-World";
            System.out.println(s.charAt(3));
        }
    }

 


4.得到s的长度,打印在控制台上.

    public class Demo1 {
        public static void main(String[] args) {
            String s = "Hello-World";
            System.out.println(s.length());
        }
    }

 


5.获得s中的"Hell"字符串,打印在控制台上.

    public class Demo1 {
        public static void main(String[] args) {
            String s = "Hello-World";
            System.out.println(s.substring(0,4));
        }
    }

 


6.获得s中的"orld"字符串,打印在控制台上.

    public class Demo1 {
        public static void main(String[] args) {
            String s = "Hello-World";
            System.out.println(s.substring(7,11));
        }
    }

 


7.将字符串s中的所有o替换为*号.打印在控制台上

    public class Demo1 {
        public static void main(String[] args) {
            String s = "Hello-World";
            String o = s.replace('o', '*');
            System.out.println(o);
        }
    }

 


8.将字符串s切割成"Hello"和"World"两个字符串,打印在控制台上

    public class Demo1 {
        public static void main(String[] args) {
            String s = "Hello-World";
            System.out.println(s.substring(0,5));
            System.out.println(s.substring(6));
        }
    }

 


9.将字符串s变为字符数组,遍历数组将每个字符打印在控制台上

复制代码
    public class Demo1 {
        public static void main(String[] args) {
            String s = "Hello-World";
            char[] chars = s.toCharArray();
            for (int i = 0; i <s.length() ; i++) {
                System.out.println(chars[i]);
            }
        }
    }
复制代码

 


第二题:
1.键盘录入一个字符串

2.统计录入的字符串中的大写字母,小写字母,数字分别有多少个.

复制代码
    public class Demo1 {
        public static void main(String[] args) {
            String s = "Hello-World";
            char[] chars = s.toCharArray();
            int daxie=0;
            int xiaoxie=0;
            int num=0;
            int other=0;
            for (int i = 0; i <chars.length ; i++) {
                if ('a'<=chars[i]&&chars[i]<='z'){
                    xiaoxie++;
                }
                if ('A'<=chars[i]&&chars[i]<='Z'){
                    daxie++;
                }
                if ('0'<=chars[i]&&chars[i]<='9'){
                    num++;
                }
            }
            System.out.println(daxie);
            System.out.println(xiaoxie);
            System.out.println(num);
        }
    }
复制代码

 



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

复制代码
    public class Demo1 {
        public static void main(String[] args) {
            String s = "Hell0";
            char[] chars = s.toCharArray();
            int daxie=0;
            int xiaoxie=0;
            int num=0;
            int other=0;
            for (int i = 0; i <chars.length ; i++) {
                if ('a'<=chars[i]&&chars[i]<='z'){
                    xiaoxie++;
                }
                if ('A'<=chars[i]&&chars[i]<='Z'){
                    daxie++;
                }
                if ('0'<=chars[i]&&chars[i]<='9'){
                    num++;
                }
            }
            System.out.println(daxie);
            System.out.println(xiaoxie);
            System.out.println(num);

        }
    }
复制代码

 


第四题:
1.键盘录入一个字符串
2.将该字符串变成字符数组
3.将字符数组中的所有大写字母变成小写字母

4.如果第一位和最后一位的内容不相同,则交换


5.将字符数组中索引为偶数的元素变成'~'

6.打印数组元素的内容

复制代码
import java.util.Scanner;

public class Demo1 {
    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('A'<=chars[i]&&'Z'>=chars[i]){
            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]);
        }
    }
}
复制代码

 

------------------------------
【结果展示】
请输入字符串
abcDEf719
最终显示的效果
~b~d~f~1~

第五题:
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"

复制代码
public class Demo {
 public static String[] filter(String [] arr,String  str){
    
    int count=0;
    for (int i = 0; i < arr.length; i++) {
        if(arr[i].indexOf(str)>=0)
        {
            
            count++;
        }
    }
    System.out.println(count);
    
    String[] strs=new String[count];
    
    for (int i = 0; i < arr.length; i++) {
        if(arr[i].indexOf(str)>=0)
        {
            strs[i]=arr[i];
            
        }
    }
    return strs;
 }

 public static ArrayList<String>  handleString(String [] arr,String str)
 {
     ArrayList<String>stringlist=new ArrayList<String>();
     for (int i = 0; i < arr.length; i++) {
            if(arr[i].indexOf(str)>=0)
            {
                String newstring=arr[i].replace(str, "*");
                stringlist.add(newstring);
                
            }
        }
     return stringlist;
 }
}
复制代码

 



第九题:
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)); } }
复制代码

posted on   code->  阅读(161)  评论(0)    收藏  举报

编辑推荐:
· 独立开发,这条路可行吗?
· 我在厂里搞 wine 的日子
· 如何通过向量化技术比较两段文本是否相似?
· 35+程序员的转型之路:经济寒冬中的希望与策略
· JavaScript中如何遍历对象?
阅读排行:
· 独立开发,这条路可行吗?
· C#源生成器:让你的代码飞起来的黑科技
· Java简历、面试、试用期、转正
· Java开发AI项目,太爽了!LangChain4j保姆级教程
· 极大提高项目部署的生产力!分享一个半自动化的CICD实现方案
< 2025年7月 >
29 30 1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31 1 2
3 4 5 6 7 8 9

导航

点击右上角即可分享
微信分享提示