Java-day10 study notes

day09 review

Insert picture description here

day10

判断键盘录入的QQ号码是否符合规则:
	1. 长度为5-12位
	2. 0不能开头
	3. 只包含数字(每一个字符都为数字)
import java.util.Scanner;

/*
    判断键盘录入的QQ号码是否符合规则:
        1. 长度为5-12位
        2. 0不能开头
        3. 只包含数字(每一个字符都为数字)
 */
public class Demo {
    
    

    public static void main(String[] args) {
    
    

        Scanner sc = new Scanner(System.in);

        System.out.println("请录入您的qq号码:");
        String qqNumber = sc.nextLine();

        //先定义一个标志位,用来记录是否符合规则
        boolean b = true;

        //1. 长度为5-12位
        if(qqNumber.length() >= 5 && qqNumber.length() <= 12) {
    
    
            //2. 0不能开头
            if(!qqNumber.startsWith("0")) {
    
    
                //3. 只包含数字(每一个字符都为数字)
                //先将字符串转为字符数组,以便与获取每一个字符
                char[] chs = qqNumber.toCharArray();
                for(int i = 0; i < chs.length; i++) {
    
    
                    char ch = chs[i];
                    //对字符串中每一个字符进行判断
                    if(ch >= '0' && ch <= '9'){
    
    
                        b = true;
                    }else {
    
    
                        b = false;
                        break;      //如果有一个不是数字,那么就可以直接退出了
                    }
                }

            }else {
    
    
                b = false;
            }
        }else {
    
    
            b = false;
        }

        //最后进行打印
        if(b){
    
    
            System.out.println(qqNumber + "符合规则");
        }else {
    
    
            System.out.println(qqNumber + "不符合规则");
        }

    }
}

One, regular expression

import java.util.Scanner;

/*
    判断键盘录入的QQ号码是否符合规则:
        1. 长度为5-12位
        2. 0不能开头
        3. 只包含数字(每一个字符都为数字)
 */
public class Demo2 {
    
    

    public static void main(String[] args) {
    
    

        Scanner sc = new Scanner(System.in);

        System.out.println("请录入您的qq号码:");
        String qqNumber = sc.nextLine();

        String regex = "[1-9][0-9]{4,11}";
        boolean b = qqNumber.matches(regex);

        //最后进行打印
        if(b){
    
    
            System.out.println(qqNumber + "符合规则");
        }else {
    
    
            System.out.println(qqNumber + "不符合规则");
        }

    }
}

A regular expression is a special string that meets certain rules and can match certain strings.

What you read from the definition:

  1. Regular expressions have specific rules
  2. Regular expression is a string
  3. Regular expression can match other strings
找到正则的方式:
	1. 正则表达式在java.util.regex包下,类名叫Pattern
	2. 先找到字符串String,然后再找“正则表达式”

1.1 Composition rules

① Character

Match a single character (that is, only one character can be matched, and multiple identical characters are also false). If the rule is met, it returns true, otherwise it returns false

x      字符 x
\\	   反斜线字符

② Character class

Match a single character, but can compare multiple different characters at once

[abc]	    a、b 或 c
[^abc]	    任何字符,除了 a、b 或 c
[a-zA-Z]	a 到 z 或 A 到 Z,两头的字母包括在内
[0-9]       匹配0-9的任意单个字符,包括两头的0和9

③ Predefined character class

Match a single character. The character class is too troublesome to write, so the character class is replaced with a special symbol to simplify the character class. This is the predefined character class.

.		任何字符
\d		数字:[0-9]
\D		非数字: [^0-9]
\s		空白字符:[ \t\n\x0B\f\r]
\w		单词字符:[a-zA-Z_0-9]

⑥ Quantifier

X is a rule and can only be applied to the rule immediately before

X?		X,一次或一次也没有
X*		X,零次或多次
X+		X,一次或多次
X{n}	X,恰好 n 次
X{n,}	X,至少 n 次
X{n,m}	X,至少 n 次,但是不超过 m 次

1.2 Common functions

① Judgment function

boolean	matches(String regex)    告知此字符串是否匹配给定的正则表达式
判断手机号是否符合规则:
	1. 必须为11位的数字
	2. 开头必须是1
	3. 第二位必须为3、5、8
public class Test {
    
    

    public static void main(String[] args) {
    
    

        Scanner sc = new Scanner(System.in);

        System.out.println("请录入您的手机号:");
        String phoneNumber = sc.nextLine();

//        String regex = "1[358][0-9]{9}";
        String regex = "1[358]\\d{9}";
        boolean b = phoneNumber.matches(regex);

        if(b) {
    
    
            System.out.println(phoneNumber + "符合规则");
        }else {
    
    
            System.out.println(phoneNumber + "不符合规则");
        }
    }
}

② Separating function

String[] split(String regex)     根据给定正则表达式的匹配拆分此字符串
a-b-c
a b   c
a.b.c
public class Test2 {
    
    

    public static void main(String[] args) {
    
    

        //a-b-c
//        String s = "a-b-c";
//        String[] strs = s.split("-");

        //a b   c
//        String s = "a b   c";
//        String[] strs = s.split(" +");
//        String[] strs = s.split("\\s+");

        //a.b.c
        String s = "a.b.c";
        // .本身可以匹配任何字符,加上 \之后会变成普通的.
        String[] strs = s.split("\\.");

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

            System.out.println(strs[i]);
        }
    }
}

③ Replacement function

String replaceAll(String regex, String replacement)     使用给定的 replacement 替换此字符串所有匹配给定的正则表达式的子字符串
public class Demo4 {
    
    

    public static void main(String[] args) {
    
    

        //String replaceAll(String regex, String replacement)
        String s = "helloworldfuckhellojavafuck";

        String ss = s.replaceAll("fuck", "*");
        System.out.println(ss);

    }
}

Two, classes and objects

Classes abstract things with common characteristics.

The class is defined by the keyword class.

2.1 Members in the class

In the real world, things are described through attributes and behaviors. The most basic unit in Java is classes, so we need to use classes to describe things

  类          现实事物
成员变量    相当于事物的属性
成员方法	相当于事物的行为

成员变量:就是一个变量,只不过是声明在类中方法外。
成员方法:就是一个方法,只不过我们可以不用public修饰的。

2.2 What is an object?

The object is the concrete instance of the class (that is, the concrete manifestation of the class)

如:
	类:
		汽车类
    对象:
    	五菱
    	宝马
    	奔驰
 ---------------   
    类:
    	手机类
    对象:
    	苹果
    	华为
    	小米

2.3 How to create objects?

格式:
	数据类型 对象名 = new 数据类型();

解释:
    数据类型:必须是引用数据类型,一般都是类类型,比如Person、String、Scanner类
    对象名:实际上是引用,用来存储地址(此地址指向真正的对象)
    new:用来在堆上开辟空间
    数据类型:同上
    ():构造方法(后面讲)

2.4 How to use the members of the class (member variables, member methods)

① Use class member variables

格式:
	获取值:对象名.变量名
	设置值:对象名.变量名 = 数据;

② Use class member methods

格式:
	对象名.方法名();
/*
    现实世界中:
        人类:
            属性: 姓名、年龄
            行为: 吃饭、睡觉
    Java中的类:
        人类:
            成员变量(相当于属性):姓名、年龄
            成员方法(相当于行为):吃饭、睡觉
 */
public class Person {
    
    

    //成员变量(相当于属性)
    //姓名
    String name;
    //年龄
    int age;

    //成员方法(相当于行为)
    //吃饭
    public void eat() {
    
    
        System.out.println("吃饭");
    }
    //睡觉
    public void sleep() {
    
    
        System.out.println(name + "睡觉");
    }

}

public class Demo {
    
    

    public static void main(String[] args) {
    
    

        //1. 创建对象
        //格式:数据类型 对象名 = new 数据类型();
        Person gxt = new Person();
        Person dlrb = new Person();

        //2. 使用类中的成员变量
        //格式:对象名.成员变量名
        System.out.println(gxt.name);   //null
        System.out.println(gxt.age);    //0

        //赋值
        gxt.name = "关晓彤";
        gxt.age = 25;

        //赋值之后
        System.out.println(gxt.name);
        System.out.println(gxt.age);

        //3. 使用类中的成员方法
        //格式:对象名.方法名();
        gxt.sleep();
        gxt.eat();

        //优雅的分割线
        System.out.println("----------优雅的分割线----------");
        System.out.println(dlrb.name);
        System.out.println(dlrb.age);

        dlrb.name = "迪丽热巴";

        dlrb.sleep();
        dlrb.eat();
    }
}
练习:
    创建一个手机类:
        属性:品牌brand、价格price、颜色color
        行为:发短信sendMessage、玩游戏playGame
    创建手机类的多个手机对象,并调用成员变量和成员方法
public class Phone {
    
    

    //成员变量(属性)
    //品牌
    String brand;
    //价格
    int price;
    //颜色
    String color;

    //成员方法(行为)
    //发短信
    public void sendMessage(String s) {
    
    

        System.out.println("给" + s + "发短信借100块吃饭!");
    }
    //玩游戏
    public void playGame() {
    
    

        System.out.println("玩英雄联盟!");
    }

    //输出成员变量的方法
    public void show() {
    
    
        System.out.println(brand + "---" + color + "---" + price);
    }

}

public class Demo2 {
    
    

    public static void main(String[] args) {
    
    

        //1. 创建对象
        Phone p = new Phone();
        Phone p2 = new Phone();

        //2. 给对象的成员变量赋值
        p.brand = "xiaomi";
        p.color = "black";
        p.price = 1999;

        p2.brand = "iphone 12 pro max 1T";
        p2.color = "tuhao gold";
        p2.price = 19999;

        //3. 输出成员变量值
        System.out.println(p.brand + "---" + p.color + "---" + p.price);

        p2.show();

        //4. 调用成员方法
        p.playGame();
        p.sendMessage("LBS");

        p2.playGame();
        p2.sendMessage("QBS");

    }
}

Guess you like

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