5.28第十三周上机练习

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

 1 package text;
 2 
 3 import java.util.Random;
 4 
 5 public class Test4 {
 6     public static void main(String[] args) {
 7         Random r = new Random();
 8         for (int i = 1; i <= 10; i++) {
 9             System.out.print(r.nextInt(101) + ",");
10         }
11 
12     }
13 }


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

 1 package text;
 2 
 3 import java.text.SimpleDateFormat;
 4 import java.util.Date;
 5 
 6 public class Test4 {
 7     public static void main(String[] args) {
 8         SimpleDateFormat sdf = new SimpleDateFormat("Gyyyy年MM月dd日");
 9 
10         Date d = new Date();
11         SimpleDateFormat sd = new SimpleDateFormat("今天是Gyyyy年的第D天,E");
12         String s = sdf.format(d);
13         String s1 = sd.format(d);
14         System.out.println(s + ":" + s1);
15 
16     }
17 }


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

 1 package text;
 2 
 3 import java.util.Scanner;
 4 
 5 import org.omg.Messaging.SyncScopeHelper;
 6 
 7 public class PersonCreate {
 8 
 9     public static void main(String[] args) {
10         PersonCreate();
11     }
12 
13     private static void PersonCreate() {
14         Scanner sc = new Scanner(System.in);
15         System.out.println("邮箱:");
16         String mail = sc.nextLine();
17         // 邮箱地址中不包含@或.
18         if (mail.indexOf("@") == -1) {
19             System.out.println("邮箱地址中不包含@");
20         }
21         if (mail.indexOf(".") == -1) {
22             System.out.println("邮箱地址中不包含.");
23         }
24         // 邮箱地址中含有多了@或.
25         int a = 0;
26         for (int i = 0; i < mail.length(); i++) {
27             if (mail.charAt(i) == '@') {
28                 a++;
29             }
30         }
31         if (a > 1) {
32             System.out.println("邮箱地址中含有多余的@");
33         }
34         int b = 0;
35         for (int i = 0; i < mail.length(); i++) {
36             if (mail.charAt(i) == '.') {
37                 b++;
38             }
39         }
40         if (b > 1) {
41             System.out.println("邮箱地址中含有多余的.");
42         }
43         // 邮箱地址中.出现在@的前面
44         int index = mail.indexOf('@');
45         int index1 = mail.indexOf('.');
46         if (index1 <= index) {
47             System.out.println("邮箱地址中.出现在@的前面");
48         }
49         // 用户名里有其他字符
50         String R = "\w + @ \w +\.\w{2,3} ";
51         if (!(mail.matches(R))) {
52             System.out.println("用户名里有其他字符");
53         }
54 
55     }
56 
57 }

原文地址:https://www.cnblogs.com/gwz-1314/p/12982074.html