第十三周上机练习

1.编写一个随机生成 10个 0(包括) 到 100 之间的随机正整数

package text;

import java.util.Random;

public class Test4 {
    public static void main(String[] args) {
        Random r = new Random();
        for (int i = 1; i <= 10; i++) {
            System.out.print(r.nextInt(101) + ",");
        }

    }
}

2.通过电子版教材或者视频,自学Date类和SimpleDateFormat类,用以下格式输出
系统当前时间
公元2020年05月28日:今天是2020年的第149天,星期四

package text;

import java.text.SimpleDateFormat;
import java.util.Date;

public class Test4 {
    public static void main(String[] args) {
        SimpleDateFormat sdf = new SimpleDateFormat("Gyyyy年MM月dd日");

        Date d = new Date();
        SimpleDateFormat sd = new SimpleDateFormat("今天是Gyyyy年的第D天,E");
        String s = sdf.format(d);
        String s1 = sd.format(d);
        System.out.println(s + ":" + s1);

    }
}

3.输入一个邮箱地址,判断是否合法.如果合法,输出用户名.
合法:必须包含@ 和 . 并且.在@的后面 (用indexof)
用户名: 例如 dandan@163.com 用户名为dandan (用subString)

package text;

import java.util.Scanner;

import org.omg.Messaging.SyncScopeHelper;

public class PersonCreate {

    public static void main(String[] args) {
        PersonCreate();
    }

    private static void PersonCreate() {
        Scanner sc = new Scanner(System.in);
        System.out.println("邮箱:");
        String mail = sc.nextLine();
        // 邮箱地址中不包含@或.
        if (mail.indexOf("@") == -1) {
            System.out.println("邮箱地址中不包含@");
        }
        if (mail.indexOf(".") == -1) {
            System.out.println("邮箱地址中不包含.");
        }
        // 邮箱地址中含有多了@或.
        int a = 0;
        for (int i = 0; i < mail.length(); i++) {
            if (mail.charAt(i) == '@') {
                a++;
            }
        }
        if (a > 1) {
            System.out.println("邮箱地址中含有多余的@");
        }
        int b = 0;
        for (int i = 0; i < mail.length(); i++) {
            if (mail.charAt(i) == '.') {
                b++;
            }
        }
        if (b > 1) {
            System.out.println("邮箱地址中含有多余的.");
        }
        // 邮箱地址中.出现在@的前面
        int index = mail.indexOf('@');
        int index1 = mail.indexOf('.');
        if (index1 <= index) {
            System.out.println("邮箱地址中.出现在@的前面");
        }
        // 用户名里有其他字符
        String R = "\w + @ \w +\.\w{2,3} ";
        if (!(mail.matches(R))) {
            System.out.println("用户名里有其他字符");
        }

    }

}
原文地址:https://www.cnblogs.com/lqh123456/p/12982289.html