Fork me on GitHub

十三周上机练习

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

 1 package Dataclass;
 2 
 3 import java.util.Random;
 4 
 5 public class rand {
 6     
 7 
 8     public static void main(String[] args) {
 9         // 编写一个随机生成 10个 0(包括) 到 100 之间的随机正整数。
10         Random r = new Random();
11 
12         for (int j = 0; j < 10; j++) {
13             
14             int i = r.nextInt(101);
15             System.out.println(i);
16             
17         }
18     }
19 
20 }

 

 

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

 

 1 package Dataclass;
 2 
 3 import java.util.Date;
 4 import java.util.Scanner;
 5 import java.text.SimpleDateFormat;
 6 
 7 public class simpleDate {
 8 
 9     public static void main(String[] args) {
10 
11         /*
12          * 用以下格式输出 系统当前时间 公元2020年05月28日:今天是2020年的第149天,星期四
13          */
14 
15         SimpleDateFormat sdf = new SimpleDateFormat("Gyyyy年MM月dd日 E HH:mm:ss");
16 
17         Date d = new Date();
18 
19         String s = sdf.format(d);
20         System.out.println(s);
21 
22         Scanner input = new Scanner(System.in);
23         System.out.println("输入年月日");
24         int a = input.nextInt();
25         int b = input.nextInt();
26         int c = input.nextInt();
27         int allday = 0;
28         int[] days = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
29         for (int i = 0; i < b - 1; i++) {
30             allday += days[i];
31         }
32         allday += c;
33         if (((a % 4 == 0 && a % 100 == 0) || a % 400 == 0) && b > 2) {
34             allday++;
35         }
36         System.out.println("今天是" + a + "年的第" + allday + "天");
37     }
38 
39 }

 

 

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

 

 1 package Dataclass;
 2 
 3 import java.util.Scanner;
 4 
 5 public class emailAddress {
 6 
 7     public static void main(String[] args) {
 8         /*
 9          * 输入一个邮箱地址,判断是否合法.如果合法,输出用户名. 合法:必须包含@ 和 . 并且.在@的后面 (用indexof) 用户名: 例如
10          * dandan@163.com 用户名为dandan (用subString)
11          */
12 
13         Scanner input = new Scanner(System.in);
14         System.out.println("请输入一个邮箱地址");
15         String s = input.next();
16 
17         int index = s.indexOf('@');
18         int index2 = s.indexOf('.');
19         if (index < index2) {
20             System.out.println("地址合法");
21             String s2 = s.substring(0, 6);
22             System.out.println("用户名是" + s2);
23 
24         } else {
25             System.out.println("地址不合法");
26         }
27 
28     }
29 
30 }

 

posted @ 2020-05-28 11:54  世界丶已黑白  阅读(147)  评论(0编辑  收藏  举报