zoukankan      html  css  js  c++  java
  • work07

    day08作业:

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

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

     1  public static void main(String[] args) {
     2 
     3         String s="Hello-World";
     4         System.out.println("World".equals(s));
     5         System.out.println(s.substring(6,8));
     6         System.out.println(s.charAt(3));
     7         System.out.println(s.length());
     8         System.out.println(s.substring(0,4));
     9         System.out.println(s.substring(7));
    10         System.out.println(s.replace("o","*"));
    11         String[] split = s.split("-", 5);
    12         for (int i = 0; i <split.length ; i++) {
    13             System.out.println(split[i]);
    14         }
    15         char[] chars = s.toCharArray();
    16         for (int i = 0; i < chars.length; i++) {
    17             System.out.print(chars[i]);
    18         }
    19 
    20 
    21     }


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

     1 public class Dome02 {
     2     public static void main(String[] args) {
     3 //        1.键盘录入一个字符串
     4 //        2.统计录入的字符串中的大写字母,小写字母,数字分别有多少个.
     5         Scanner sc=new Scanner(System.in);
     6         String s = sc.next();
     7         char[] chars = s.toCharArray();
     8         int big=0,small=0,num=0;
     9         for (int i = 0; i < chars.length; i++) {
    10             if ('a'<=chars[i]&&chars[i]<='z'){
    11                 small++;
    12             }else
    13                 if ('A'<=chars[i]&&chars[i]<='Z'){
    14                     big++;
    15             }else
    16                 if ('0'<=chars[i]&&chars[i]<='9'){
    17                     num++;
    18                 }
    19         }
    20         System.out.println("小写字母有"+small+"个");
    21         System.out.println("大写字母有"+big+"个");
    22         System.out.println("数字有"+num+"个");
    23 
    24 
    25     }
    26 }



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

     1 package com.hp.work07;
     2 
     3 import java.util.Scanner;
     4 
     5 public class Dome03 {
     6     public static void main(String[] args) {
     7 //        1.键盘录入5个字符串,组成一个数组
     8 //        2.统计录入的字符串数组中的大写字母,小写字母,数字分别有多少个.
     9         Scanner sc=new Scanner(System.in);
    10         String []str=new String[5];
    11         for (int i = 0; i <str.length ; i++) {
    12             System.out.println("请输入第"+(i+1)+"个字符串");
    13             String s = sc.next();
    14             str[i]=s;
    15         }
    16 
    17         int big=0,small=0,num=0;
    18         for (int j  = 0; j <str.length ; j++) {
    19             char[] chars = str[j].toCharArray();
    20             for (int i = 0; i < chars.length; i++) {
    21                 if ('a'<=chars[i]&&chars[i]<='z'){
    22                     small++;
    23                 }else
    24                 if ('A'<=chars[i]&&chars[i]<='Z'){
    25                     big++;
    26                 }else
    27                 if ('0'<=chars[i]&&chars[i]<='9'){
    28                     num++;
    29                 }
    30         }
    31 
    32         }
    33         System.out.println("小写字母有"+small+"个");
    34         System.out.println("大写字母有"+big+"个");
    35         System.out.println("数字有"+num+"个");
    36 
    37     }
    38 }

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

     1 package com.hp.work07;
     2 
     3 import java.util.Scanner;
     4 
     5 public class Dome04 {
     6 //        1.键盘录入一个字符串
     7 //    2.将该字符串变成字符数组
     8 //    3.将字符数组中的所有大写字母变成小写字母
     9 //    4.如果第一位和最后一位的内容不相同,则交换
    10 //    5.将字符数组中索引为偶数的元素变成'~'
    11 //            6.打印数组元素的内容
    12 //    ------------------------------
    13 //            【结果展示】
    14 //    请输入字符串
    15 //            abcDEf719
    16 //    最终显示的效果
    17 //                ~b~d~f~1~
    18 public static void main(String[] args) {
    19     Scanner sc=new Scanner(System.in);
    20     System.out.println("请输入一个字符串:");
    21     String s = sc.next();
    22     char[] chars = s.toCharArray();
    23     for (int i = 0; i < chars.length; i++) {
    24         if ('A'<=chars[i]&&chars[i]<'Z') {
    25             chars[i]+=32;
    26         }
    27     }
    28     if (chars[0] !=chars[chars.length-1]) {
    29         char a=chars[0];
    30         chars[0] =chars[chars.length-1];
    31         chars[chars.length-1]=a;
    32     }
    33     for (int i = 0; i < chars.length; i++) {
    34         if (i%2==0){
    35             chars[i]='~';
    36         }
    37         System.out.print(chars[i]+"");
    38     }
    39 
    40 }
    41 }

    第五题:
    1.键盘录入一个字符串
    2.从字符串中随机获取3次字符,将获取的3个字符组成一个新的字符串.打印到控制台上

     1 package com.hp.work07;
     2 
     3 import java.util.Random;
     4 import java.util.Scanner;
     5 
     6 public class Dome05 {
     7     public static void main(String[] args) {
     8 //        1.键盘录入一个字符串
     9 //        2.从字符串中随机获取3次字符,将获取的3个字符组成一个新的字符串.打印到控制台上
    10         Scanner sc=new Scanner(System.in);
    11         System.out.println("请输入一个字符串:");
    12         String next = sc.next();
    13         Random random=new Random();
    14         StringBuffer stringBuffer=new StringBuffer();
    15         for (int i = 0; i < 3; i++) {
    16             int i1 = random.nextInt(next.length());
    17             char c = next.charAt(i1);
    18             stringBuffer.append(c);
    19         }
    20         System.out.println(stringBuffer);
    21     }
    22 }


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

     1 package com.hp.work07;
     2 
     3 import java.util.ArrayList;
     4 import java.util.Scanner;
     5 
     6 public class Dome06 {
     7     public static void main(String[] args) {
     8 //        1.创建一个集合,往集合中键盘录入5个字符串
     9 //        2.遍历集合,将集合中长度大于4的元素末尾加上一个X,
    10 //                3.遍历集合,将集合打印在控制台上.
    11 //                例:键盘录入后的集合{"123","ASDFQ","qq","poiuy","asd"}
    12 //        打印到控制台上的集合{"123","ASDFQX","qq","poiuyX","asd"}
    13         ArrayList<String>list=new ArrayList<String>();
    14         Scanner scanner=new Scanner(System.in);
    15         for (int i = 0; i < 5; i++) {
    16             System.out.println("请输入第"+(i+1)+"个集合");
    17             String next = scanner.next();
    18             list.add(next);
    19         }
    20         for (int i = 0; i < list.size(); i++) {
    21             if(list.get(i).length()>4){
    22                 System.out.println(list.set(i,list.get(i)+'X'));
    23 
    24             }
    25         }
    26         System.out.println(list);
    27     }
    28 }



    第七题:
    分析以下需求,并用代码实现
    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"

     1 package com.hp.work07;
     2 
     3 public class Dome07 {
     4     public static String getPropertyGetMethodName(String property){
     5         char cha=property.charAt(0);
     6         String str2=Character.toTitleCase(cha)+property.substring(1,property.length());
     7         String str3="get"+str2;
     8         return str3;
     9     }
    10 
    11     public static String getPropertySetMethodName(String property){
    12         char cha=property.charAt(0);
    13         String str2=Character.toTitleCase(cha)+property.substring(1,property.length());
    14         String str3="set"+str2;
    15         return str3;
    16     }
    17 
    18 
    19     public static void main(String[] args) {
    20         System.out.println(getPropertyGetMethodName("name"));
    21         System.out.println(getPropertySetMethodName("name"));
    22     }
    23 }

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

     1 package com.hp.work07;
     2 
     3 import java.util.ArrayList;
     4 
     5 public class Dome08 {
     6     public static void main(String[] args) {
     7     String arr[]={"itcast","itheima","baitdu","weixin","zhifubao"};
     8         String str="it";
     9         filter(arr,str);
    10 
    11     }
    12     public static String[] filter(String[]arr,String s){
    13         ArrayList<String> list=new ArrayList<>();
    14         for (int i = 0; i < arr.length; i++) {
    15             if (arr[i].contains(s)){
    16                 list.add(arr[i]);
    17             }
    18         }
    19         System.out.println(list);
    20         String []arr1=new String[list.size()];
    21         arr1=list.toArray(arr1);
    22         return arr1;
    23     }
    24 }


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

     1 package com.hp.work07;
     2 
     3 import java.util.ArrayList;
     4 
     5 public class Dome09 {
     6     public static void main(String[] args) {
     7         String arr[]={"beijing", "shanghai", "tianjin", "chongqing"};
     8         ArrayList<String> list=handleString(arr,"a");
     9         System.out.println(list);
    10     }
    11     public static ArrayList<String> handleString(String [] arr,String str){
    12         ArrayList<String> list=new ArrayList<>();
    13         for (int i = 0; i < arr.length; i++) {
    14             if (arr[i].contains(str)){
    15                 String replace = arr[i].replace(str, "*");
    16                 list.add(replace);
    17             }
    18         }
    19         return list;
    20     }
    21 }


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

     1 package com.hp.work07;
     2 
     3 public class Dome10 {
     4     public static void main(String[] args) {
     5         int a=1; int b=4;
     6         System.out.println(MathUtils.add(a,b));
     7     }
     8 }
     9 
    10 public class MathUtils {
    11     public static  int add(int a,int b){
    12         return a+b;
    13     }
    14 }
  • 相关阅读:
    微信小程序HTTPS
    微信商城-1简介
    va_list
    Event log c++ sample.
    EVENT LOGGING
    Analyze Program Runtime Stack
    unknow table alarmtemp error when drop database (mysql)
    This application has request the Runtime to terminate it in an unusual way.
    How to check if Visual Studio 2005 SP1 is installed
    SetUnhandledExceptionFilter
  • 原文地址:https://www.cnblogs.com/lemperor/p/13843685.html
Copyright © 2011-2022 走看看