Java第十八次作业

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

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

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

 1 package date528;
 2 
 3 import java.text.SimpleDateFormat;
 4 import java.util.Date;
 5 
 6 public class 格式化日期 {
 7     public static void main(String[] args) {
 8         Date now = new Date(); // 创建一个Date对象,获取当前时间
 9         // 指定格式化格式
10         SimpleDateFormat f = new SimpleDateFormat("公元" + "yyyy年 MM月dd日:" + "今天是" + "yyyy年的第D天,E。");
11         // yyyy:年份;
12         // MM:月份;
13         // dd:日期;
14         // D:一年中的第几天;
15         // E:星期;
16         // HH:小时;
17         // mm:分钟;
18         // ss:秒;
19         System.out.println(f.format(now)); // 将当前时间袼式化为指定的格式
20     }
21 }
 

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

 1 package date528;
 2 
 3 import java.util.Scanner;
 4 
 5 public class 判断邮箱是否合法 {
 6     public static void main(String[] args) {
 7         // TODO Auto-generated method stub
 8         Scanner input = new Scanner(System.in);
 9         System.out.println("请输入合法的邮箱:");
10         String str = input.nextLine();
11         int count = 0;// 定义一个计数器用来记录@的个数
12         int count2 = 0;// 定义一个计数器用来记录.的个数
13         int x = 0;// 用来记录出现第一个@对应的索引
14         int y = 0;// 用来记录出现第一个.对应的索引
15 
16         for (int j = 0; j < str.length() - 1; j++) {
17             String str1 = str.substring(j, j + 1);
18             if (str1.equals("@")) {
19                 count++;
20                 x = j;
21             }
22             if (str1.equals(".")) {
23                 count2++;
24                 y = j;
25             }
26 
27             // continue;
28 
29         }
30         if (count == 1 && count2 == 1 && x < (y - 1) && x != 0 && y != str.length() - 1) {
31             str.endsWith("@163.com");
32             String str1 = str.substring(0, 6);
33             System.out.println("合法,用户名为:" + str1);
34         } else {
35             System.out.println("不合法");
36         }
37 
38     }
39 
40 }
 

猜你喜欢

转载自www.cnblogs.com/tongailin/p/12980225.html