Java-day09 study notes

day08 review

Insert picture description here

1. Variable parameters

Variable parameters represent zero or more parameters

格式:
	数据类型... 变量名
	
注意事项:
	1. 可变参数实际上就是一个数组
	2. 可变参数一定要定义在所有参数的最后面
public class Demo2 {
    
    

    public static void main(String[] args) {
    
    

        System.out.println(sum());
        System.out.println(sum(3));
        System.out.println(sum(3, 5));
        System.out.println(sum(3, 5, 8));
        System.out.println(sum(1, 2, 3, 4, 5));
    }

    public static int sum(int... a){
    
    

        int result = 0;
        for (int i = 0; i < a.length; i++) {
    
    

            result += a[i];
        }

        return result;
    }

}

Two, random number

Math.random();	//[0.0,1.0)

此方法返回一个大于等于0.0但是小于1.0的double类型的正数
如果求[a,b)的随机整数,其公式为:

	(int)(Math.random() * (a - b)) + a;

如果是两个闭区间,将右边的数+1就变成了半开半闭区间,其方法同上
public class Demo {
    
    

    public static void main(String[] args) {
    
    

        /*
            生成一个1-9的随机数
         */
        int number = (int)(Math.random()*9) + 1;

        System.out.println(number);
    }
}
public class Demo2 {
    
    

    public static void main(String[] args) {
    
    

        String[] strName = {
    
     "王超安", "高德", "魏翔", "方伟", "周鹏", "杨重祥", "严振伟", "刘杰", "张旺"};

        int index = (int)(Math.random()*strName.length) + 1;

        System.out.println(strName[index-1]);

    }
}

Three, String

Help document

Online version:
Open source Chinese version (1.6) [Chinese]: https://tool.oschina.net/apidocs/apidoc?api=jdk-zh
official version (1.8) [English]: https://docs.oracle.com /javase/8/docs/
Offline version: You
can download directly after logging in [jdk1.6 Chinese version and jdk1.8 Chinese and English version]: https://download.csdn.net/download/ChangeNone/14046207

Note: String is under the java.lang package

3.1 Create format

方式一:【常用】
	String 对象名 = "字符串的内容";
方式二:
	String 对象名 = new String("字符串的内容");
方式三:
	char[] chs = {元素1, 元素2, 元素3, ...};
	String 对象名 = new String(chs);
public class Demo {
    
    

    public static void main(String[] args) {
    
    

        //String 对象名 = "字符串的内容";
//        String s = "hello";

        //String 对象名 = new String("字符串的内容");
//        String s = new String("world");

        /*
            char[] chs = {元素1, 元素2, 元素3, ...};
            String 对象名 = new String(chs);
         */
        char[] chs = {
    
    '美', '国', '不', '行', '了'};
        String s = new String(chs);

        System.out.println(s);
    }

}

3.2 Common functions

① Judgment function

equals(String s)   //比较两个字符串的内容是否相同
equalsIgnoreCase(String s)	//忽略大小写的比较两个字符串的内容是否相同
contains(String s)	//判断一个字符串是否包含另一个字符串
startsWiths(String s)	// 判断一个字符串是否以传入的字符串开头
endsWiths(String s)		//判断一个字符串是否以传入的字符串结尾
isEmpty()	//判断字符串是否为空
public class Demo2 {
    
    

    public static void main(String[] args) {
    
    

        String s = "helloworld";

        //equals(String s)   比较两个字符串的内容是否相同
//        String s2 = new String("helloworld");
//        System.out.println(s == s2);  //false
//        boolean b = s.equals(s2);
//        System.out.println(b);        //true

        //equalsIgnoreCase(String s)  忽略大小写的比较两个字符串的内容是否相同
//        System.out.println(s.equals("HelloWorld"));    //false
//        boolean b = s.equalsIgnoreCase("HelloWorld");  //true
//        System.out.println(b);

        //contains(String s)          判断一个字符串是否包含另一个字符串
//        System.out.println(s.contains("llold"));  //false
//        System.out.println(s.contains("owo"));    //true
//        boolean b = s.contains("hello");  //true
//        System.out.println(b);

        //startsWith(String s)        判断一个字符串是否以传入的字符串开头
//        boolean b = s.startsWith("hello");     //true
//        System.out.println(b);

        //endsWith(String s)          判断一个字符串是否以传入的字符串结尾
//        boolean b = s.endsWith("helloworld");  //true
//        System.out.println(b);

        //isEmpty()  判断字符串是否为空
        System.out.println(s.isEmpty());         //false

    }
}

Exercise: Simulate user login, the user only has three login opportunities. If the account and password are correct, it will display "Login Successfully" and jump to the corresponding interface. If the account and password are incorrect, it will prompt "How many more opportunities do you have." If you fail to log in successfully after three opportunities are used up, it will display "Your Account has been locked"

import java.util.Scanner;
public class Test2 {
    
    

    public static void main(String[] args) {
    
    

        Scanner sc = new Scanner(System.in);

        for(int i = 0; i < 3; i++) {
    
    
            System.out.println("请输入您的账号:");
            String username = sc.nextLine();
            System.out.println("请输入您的密码:");
            String password = sc.nextLine();

            if(username.equals("admin") && password.equals("123456")){
    
    
                System.out.println("登录成功");
                break;
            }else {
    
    

                //如果没有机会了(也就是0次机会),那么提示 “您的账号已被锁定”
                if(2-i == 0){
    
    
                    System.out.println("您的账号已被锁定");
                }else{
    
    
                    //登录失败,并在此提示还有几次机会
                    System.out.println("您还剩" + (2-i) + "次机会");
                }

            }
        }

    }

}

② Get function

charAt(int index)	//获取指定索引处的字符
getBytes()	//将字符串转为字节数组
subString(int startIndex)		//从指定索引处开始截取字符串(包括此索引)
subString(int startIndex, int endIndex)	  //从索引startIndex开始截取到所以办endIndex为止(包括左边,但是不包括右边)
indexOf(int ch)		//查找字符在字符串中第一次出现的位置
indexOf(int ch, int fromIndex)	//从指定索引处开始查找指定字符在字符串中第一次出现的位置
length()	//获取字符串的长度
public class Demo3 {
    
    

    public static void main(String[] args) {
    
    

        String s = "helloworld";

        //charAt(int index)    获取指定索引处的字符
//        char c = s.charAt(0);
//        System.out.println(c); //h

        //getBytes()   将字符串转为字节数组
//        byte[] bys = s.getBytes();
//        for(int i = 0; i < bys.length; i++){
    
    
//            System.out.println((char)bys[i]);
//        }

        //subString(int startIndex)   从指定索引处开始截取字符串(包括此索引)
//        String ss = s.substring(1);
//        System.out.println(ss);

        //subString(int startIndex, int endIndex)  从索引startIndex开始截取到所以办endIndex为止(包括左边,但是不包括右边)
//        String ss = s.substring(1, 3);  //el
//        System.out.println(ss);

        //indexOf(int ch)  查找字符在字符串中第一次出现的位置
//        int index = s.indexOf('e');  //1
//        System.out.println(index);
//
//        int index2 = s.indexOf('o'); //4
//        System.out.println(index2);

        //indexOf(int ch, int fromIndex)  从指定索引处开始查找指定字符在字符串中第一次出现的位置
//        int index = s.indexOf('o', 5);
//        System.out.println(index);

        //length()
        System.out.println(s.length());

    }
}
练习:
1. 键盘录入一个字符串(只包括大写字母、小写字母、数字三种字符),统计大写字母、小写字母、数字的个数
2. 统计一个字符串在某个字符串中出现的次数。(如:em在java.demo.demo.helloem中出现的次数)
/*
    键盘录入一个字符串(只包括大写字母、小写字母、数字三种字符),
    统计大写字母、小写字母、数字的个数
 */
public class Test3 {
    
    

    public static void main(String[] args) {
    
    

        Scanner sc = new Scanner(System.in);
        System.out.println("请输入一个字符串:");
        String s = sc.nextLine();

        //定义变量,用来统计个数
        int smallCount = 0;
        int bigCount = 0;
        int numberCount = 0;

        for(int i = 0; i < s.length(); i++) {
    
    

            char ch = s.charAt(i);

            if(ch >= '0' && ch <= '9'){
    
    
                numberCount++;
            }
            if(ch >= 'a' && ch <= 'z'){
    
    
                smallCount++;
            }
            if(ch >= 'A' && ch <= 'Z'){
    
    
                bigCount++;
            }
        }

        System.out.println("大写字母的个数为:" + bigCount);
        System.out.println("小写字母的个数为:" + smallCount);
        System.out.println("数字的个数为:" + numberCount);

    }
}
public class Test4 {
    
    

    public static void main(String[] args) {
    
    

//        String s = "java.demo.demo.helloem";
//        String cc = "em";
        String s = "hehevahellojavahehedemoheheddhe";
        String cc = "hehe";

        //定义一个变量用来统计次数
        int count = 0;

        //先用indexof方法获取一个索引,通过索引是否为-1判断是否存在

        int index = s.indexOf(cc);

        //用循环改进
        while(index != -1) {
    
    

            count++;

            //从cc.length()-1开始查找,就是为了越过已经查找过的字符串
            index = s.indexOf(cc, index + cc.length()-1);

        }

        System.out.println("字符串中一共有" + count + "个" + cc);

    }
}

③ Conversion function

getBytes()	//将字符串转为字节数组
toCharArray()	//将字符串转为字符数组
toUpperCase()	//将字符串中的字母都转为大写状态
toLowerCase()	//将字符串中的字母都转为小写状态
concat(String s)	//将两个字符串拼接,谁调用concat方法谁在前
valueOf系列	//将其它数据类型(基本数据类型和引用数据类型)转为字符串类型

注意:如果一个方法被static修饰,那么此方法可以直接使用类名调用。
String ss = String.valueOf(666);  //将整数类型的666转为了字符串类型的666
public class Demo4 {
    
    

    public static void main(String[] args) {
    
    

        String s = "HelloWorld";

        //getBytes()      将字符串转为字节数组
//        byte[] bys = s.getBytes();
//        for (int i = 0; i < bys.length; i++) {
    
    
//            System.out.println((char)bys[i]);
//        }

        //toCharArray()   将字符串转为字符数组
//        char[] chs = s.toCharArray();
//        for (int i = 0; i < chs.length; i++) {
    
    
//            System.out.println(chs[i]);
//        }

        //toUpperCase()   将字符串中的字母都转为大写状态
//        String ss = s.toUpperCase();
//        System.out.println(ss);     //HELLOWORLD

        //toLowerCase()   将字符串中的字母都转为小写状态
//        String ss = s.toLowerCase();
//        System.out.println(ss);       //helloworld

        //concat(String s)   将两个字符串拼接,谁调用concat方法谁在前
//        String s2 = "java中国";
//        String ss = s.concat(s2);       //HelloWorldjava中国
//        System.out.println(ss);

        //valueOf系列   将其它数据类型(基本数据类型和引用数据类型)转为字符串类型
        //注意:如果一个方法被static修饰,那么此方法可以直接使用类名调用。
        String ss = String.valueOf(666);  //将整数类型的666转为了字符串类型的666
        System.out.println(ss);      //666

        String ss2 = String.valueOf(false);
        System.out.println(ss2);
    }
}
练习:
	有如下字符串:helLoWORld 将此字符串的首字母变为大写,其余字母变为小写,然后输出在控制台。
	
	结果为:Helloworld
public class Test5 {
    
    

    public static void main(String[] args) {
    
    

        String s = "helLoWORld";

        //方式一:【了解】
        /*
        //1. 先将字符串的所有字母都转为小写
        String ss = s.toLowerCase();
        System.out.println(ss);

        //2. 将全部转为小写的字符串的第一个字符转为大写
        char[] chs = ss.toCharArray();
        chs[0] = (char)(chs[0] - 32);

        System.out.println(chs);

         */

        //方式二:【掌握】
        //1. 先获取字符串的第一个字符,并转为大写
//        String start = s.substring(0, 1);
//        String start_1 = start.toUpperCase();
        String start = s.substring(0, 1).toUpperCase();

        //2. 再获取字符串的剩余字符,并转为小写
//        String end = s.substring(1);
//        String end_1 = end.toLowerCase();
        String end = s.substring(1).toLowerCase();

        //3. 拼接
//        String ss = start + end;
        String ss = start.concat(end);

        System.out.println(ss);

    }
}

Guess you like

Origin blog.csdn.net/ChangeNone/article/details/112370396