高级API,第二章章节练习

时间:2022-04-03 13:53:54

第一题

 

 1 package com.kgc.zjlx.zj2.zj201;
 2 
 3 import java.util.Scanner;
 4 
 5 public class Test {
 6     Scanner sc = new Scanner(System.in);
 7 
 8     public void shuru() {
 9         String str;
10         do {
11             System.out.println("请输入长度为6的字符串:");
12             str=sc.next();
13         } while (str.length()!= 6);
14         System.out.println("程序退出了,您输入了:"+str);
15     }
16 
17 
18     public static void main(String[] args) {
19         new Test().shuru();
20     }
21 }

 

运行结果

 高级API,第二章章节练习

 

第二题

 

 1 package com.kgc.zjlx.zj2.zj202;
 2 /**
 3  * 对录入的信息进行有效的验证
 4  */
 5 
 6 import java.util.Scanner;
 7 
 8 public class Date {
 9     Scanner sc = new Scanner(System.in);
10 
11     //判断传入的字符数量
12     public int counter(String str, char c) {
13         int counter = 0;
14         for (int i = 0; i < str.length(); i++) {
15             char ch = str.charAt(i);
16             if (ch == c) {
17                 counter++;
18             }
19         }
20         return counter;
21     }
22 
23     //判断日期格式
24     public void test() {
25         int yf1 = 0;
26         int ri1 = 0;
27         Scanner sc = new Scanner(System.in);
28         while (true) {
29             System.out.print("请输入会员生日<月/日 00/00>:");
30             String str = sc.next();
31             int i = counter(str, '/');//判断个数
32             if (i == 1) {
33                 int a = str.indexOf('/');
34                 String yf = str.substring(0, a);
35                 String ri = str.substring(a + 1, str.length());
36                 try {
37                     yf1 = Integer.parseInt(yf);
38                     ri1 = Integer.parseInt(ri);
39                 } catch (NumberFormatException e) {
40                     System.err.println("非法输入字符");
41                     continue;
42                 }
43                 if (yf1 <= 0 || yf1 > 12 || ri1 <= 0 || ri1 > 31) {
44                     System.out.println("日期超出范围了");
45                 } else {
46                     System.out.println("该会员生日是:" + str);
47                     break;
48                 }
49             } else {
50                 System.out.println("输入的格式错误");
51             }
52         }
53     }
54 
55     //设置密码
56     public void password() {
57         String password;
58         do {
59             System.out.print("请输入会员密码<6-10位>:");
60             password = sc.next();
61         } while (!(password.length() > 5 && password.length() < 11));
62         System.out.println("该会员的密码是:" + password);
63     }
64 
65     public static void main(String[] args) {
66         Date date = new Date();
67         date.test();
68         date.password();
69     }
70 }

 

运行结果

 高级API,第二章章节练习

 

第三题

 

 1 package com.kgc.zjlx.zj2.zj203;
 2 
 3 import java.util.Random;
 4 import java.util.Scanner;
 5 
 6 public class Test {
 7     public static void main(String[] args){
 8         Scanner sc=new Scanner(System.in);
 9         Random rd=new Random();
10         Mamber mb=new Mamber();
11 
12         System.out.print("请输入会员姓名:");
13         mb.setName(sc.next());
14         System.out.print("请输入会员性别:");
15         mb.setSex(sc.next());
16         System.out.print("请输入会员年龄:");
17         mb.setAge(sc.nextInt());
18 
19         System.out.println("创建会员成功!");
20         System.out.println("会员编号:"+rd.nextInt(9999));
21         System.out.println("会员详细信息:");
22         System.out.println(mb.getName()+"\t"+mb.getSex()+"\t"+mb.getAge());
23     }
24 }

 

运行结果

高级API,第二章章节练习