随笔7

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.将字符数组中的所有大写字母变成小写字母

复制代码
    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'){
                    String s1= String.valueOf(chars[i]);
                    String s2 = s1.toLowerCase();
                    System.out.println(s2);
                }
            }

        }
    }
复制代码

 


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

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'){
String s1= String.valueOf(chars[i]);
String s2 = s1.toLowerCase();
System.out.println(s2);
}
}
if (s.charAt(0)!=s.charAt(s.length())){
s.replace(s.charAt(0),s.charAt(s.length()));
System.out.println(s);
}
}
}
???


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

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++) {
if (i%2==0){
s.replace(s.charAt(i),'`');
}
}
System.out.println(s);
}
}
???


6.打印数组元素的内容

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++) {
if (i%2==0){
s.replace(s.charAt(i),'`');
}
}
System.out.println(s);
}
}
???


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

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

import java.util.Random;

public class Demo1 {
public static void main(String[] args) {
String s = "Hello-World";
Random ra = new Random();
for (int j = 0; j <3 ; j++) {
int i = ra.nextInt(3);
for (; i <s.length() ; i++) {
String s1 = s.valueOf(s.charAt(i));
System.out.println(s1);
}
}

}
}
???


第六题:
1.创建一个集合,往集合中键盘录入5个字符串
2.遍历集合,将集合中长度大于4的元素末尾加上一个X,


3.遍历集合,将集合打印在控制台上.
例:键盘录入后的集合{"123","ASDFQ","qq","poiuy","asd"}
打印到控制台上的集合{"123","ASDFQX","qq","poiuyX","asd"}

import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

public class Demo1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
ArrayList<String> list1= new ArrayList<>();
for (int i = 0; i <5; i++) {
System.out.println("请录入数据");
list1.add(i,sc.next());
if (list1.get(i).length()>4){
list1.get(i).concat("X");
}
}
}
}
???



第七题:
分析以下需求,并用代码实现
1.定义如下方法public static String getPropertyGetMethodName(String property)
功能描述:
(1)该方法的参数为String类型,表示用户传入的参数,返回值类型为String类型,返回值为对应的get方法的名字
(2)如:用户调用此方法时传入参数为"name",该方法的返回值为"getName"
传入参数为"age",该方法的返回值为"getAge"

import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

public class Demo1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
ArrayList<String> list1= new ArrayList<>();
for (int i = 0; i <5; i++) {
System.out.println("请录入数据");
list1.add(i,sc.next());
if (list1.get(i).length()>4){
list1.get(i).concat("X");
}
}
}
}



2.定义如下方法public static String getPropertySetMethodName(String property)
功能描述:
(1)该方法的参数为String类型,表示用户传入的参数,返回值类型为String类型,返回值为对应的set方法的名字
(2)如:用户调用此方法时传入参数为"name",该方法的返回值为"setName"
传入参数为"age",该方法的返回值为"setAge"

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


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

 

 

 

import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

public class Demo1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
ArrayList<String> list1= new ArrayList<>();
for (int i = 0; i <5; i++) {
System.out.println("请录入数据");
list1.add(i,sc.next());
if (list1.get(i).length()>4){
list1.get(i).concat("X");
}
}
}
}
posted @ 2020-10-20 08:10  小朋-  阅读(119)  评论(0)    收藏  举报