代码段

【说明 - 写在最前】

代码段:是在随笔中引用到的代码段,因为不适于直接显示在文章中,特建立虚拟数据保存池 - 代码段用于保存用到的代码段;代码段篇幅非常长,建议使用锚点进行浏览,即本文章中的链接,

引用请写:http://www.cnblogs.com/Wfei/p/3251300.html + #Wfei + 锚点,

锚点的命名规则:Wfei + 日期 + 时间,如:Wfei201308111440,

引用:http://www.cnblogs.com/Wfei/p/3251300.html#Wfei201308111440

----------------------------------------优雅分割线----------------------------------------

一:合法文件名:判断技巧

 1     /**
 2      * 根据截取到的.xxx文件类型标志判断出文件的类型,并显示输出
 3      */
 4     public void compareFileType()
 5     {
 6         String fileType;
 7         char fileTypeChar;
 8         String tips;
 9         
10         fileType = cutFileType();
11         fileTypeChar = transformFileTypeToChar(fileType).charAt(0);
12         //上一行或者写成:fileTypeChar = fileType.charAt(fileType.length()-1);待测试
13         switch (fileTypeChar)
14         {
15         case 'a':                                    //.java文件
16             tips = "java类文件";
17             tips = reMatchTips(fileType, ".java", tips);
18             break;
19         case 's':                                    //.class文件
20             tips = "java字节码文件";
21             tips = reMatchTips(fileType, ".class", tips);
22             break;
23         case 't':                                    //.txt文件
24             tips = "TXT文本文件";
25             tips = reMatchTips(fileType, ".txt", tips);
26             break;
27         case 'g':case 'f':                            //.jpg .png .gif .bmp文件
28         case 'p':                                    //即图片
29             tips = fileType+"图片文件";
30             tips = reMatchTips(fileType, ".jpg.png.gif.bmp", tips);
31             break;
32         case 'd':                                    //.word文件
33             tips = "Word文档";
34             tips = reMatchTips(fileType, ".word", tips);
35             break;
36         default:                                    //其他文件
37             tips = "系统赞未识别,其他文件";
38             break;
39         }
40         System.out.println("【tips】您输入的文件名:"+fileName+",文件是:"+tips);
41     }
42     /**
43      * 根据终极比对结果,返回相应的输出语句
44      * @param fileType
45      * @param target
46      * @param tips
47      * @return
48      */
49     public String reMatchTips(String fileType,String target,String tips)
50     {
51         if (reMatch(fileType, target))
52         {
53             return tips;
54         }
55         else {
56             return "系统赞未识别,其他文件";
57         }
58     }
59     /**
60      * 因为截取的.xxx最后一个字符switch来比较,比较片面,再一次比对是否真实相同
61      * @param fileType
62      * @param target
63      * @return
64      */
65     public boolean reMatch(String fileType,String target)
66     {
67         if (target.indexOf(fileType) != -1)
68         {
69             return true;
70         }
71         else {
72             return false;
73         }
74     }

 二:万年历:制作技巧

 1         //判断年份:平年数、润年数
 2         for (int i = 1900; i < year; i++)
 3         {
 4             if ((i%4 == 0 && i%100 != 0) || i%400 == 0)
 5             {
 6                 leapYear++;
 7             }
 8         }
 9         nonleapYear = year-1900-leapYear;            //平年数
10         
11         //计算1900-(year-1)年底之间的天数
12         days = 366 * leapYear + 365 * nonleapYear;
13         
14         //对year年,1-month-1月底之间天数的累加到days上;并通过内潜逃的if语句判断month当月的天数,方便之后打印出month月日历表的for循环遍历
15         for (int i = 1; i <= month; i++)
16         {
17             switch (i)
18             {
19             case 2:
20                 if ((year%4 == 0 && year%100 != 0) || year%400 == 0)
21                 {
22                     monthDays = 29;
23                 }
24                 else {
25                     monthDays = 28;
26                 }
27                 break;
28             case 4:case 6:case 9:
29             case 11:
30                 monthDays = 30;
31                 break;
32             default:
33                 monthDays = 31;
34                 break;
35             }
36             days += monthDays;
37         }
38         //减去多加的month月的天数
39         days -= monthDays;
40         //到month月第一天的总天数
41         days++;
42         
43         //输出日历表
44         System.out.println("=================="+year+"年"+month+"月日历表==================");
45         System.out.println("日	一	二	三	四	五	六");
46         //打印空格制表符,用于分割日期之间的间距
47         for (int i = days%7; i > 0; i--)
48         {
49             System.out.print("	");
50         }
51         //输出month月每一天
52         for (int i = 1; i <= monthDays; i++)
53         {
54             if ((days+i-1)%7 == 6)
55             {
56                 System.out.println(i);
57             }else {
58                 System.out.print(i+"	");
59             }
60         }

 三:图书系统:公用方法

 1 /**
 2      * 验证用户每一步输入时是否继续输入
 3      * @param userInput:用户输入的字符串
 4      * @param tips:需要提示给用户的[关键信息]
 5      * @return:返回false表示用户不愿意继续输入
 6      */
 7     public boolean isContinueInput(String userInput,String tips)
 8     {
 9         boolean isContinueInput = true;
10         userInput = userInput.toUpperCase();        //将用户输入的,toUpperCase不会转换非字母的字符
11         
12         //处理程序
13         if (userInput.equals("EXIT"))
14         {
15             System.out.println("
【input】用户选择了,放弃"+tips+";正在返回到上级菜单......");
16             isContinueInput = false;
17         }
18         
19         return isContinueInput;
20     }
21     /*********************************************验证码相关 - 开始***********************************************/
22     /**
23      * 验证码生成器
24      * @param count:需要产生的验证码的字符个数
25      * @return:返回特定长度的验证码字符串
26      */
27     public String buildCheckCode(int count)
28     {
29         String checkCode = "";
30         StringBuffer stringBuffer = new StringBuffer();        //用于进行字符串值附加的temp,不用String更有利于性能的提升
31         
32         //处理程序
33         for (int i = 0; i < count; i++)
34         {
35             stringBuffer.append((int)(Math.random()*10));    //随机生成一个字符
36         }
37         checkCode = stringBuffer.toString();
38         return checkCode;
39     }
40     /**
41      * 验证码校验
42      * @param userInputCheckCode:用户输入的验证码
43      * @param buildedCheckCode:系统之前产生的验证码
44      * @return:验证码是否输入正确,true为正确
45      */
46     public boolean checkCheckCode(String userInputCheckCode,String buildedCheckCode)
47     {
48         boolean isSame = false;
49         
50         //处理程序
51         if (buildedCheckCode.equals(userInputCheckCode))
52         {
53             isSame = true;
54         }
55         return isSame;
56     }
57     /*********************************************验证码相关 - 结束***********************************************/

 (四)在使用集合方面,写入数据时,可能需要重写的方法:hashCode()、equals()

 1     /**
 2      * 重写hashCode()方法,因为是将该对象写入到集合的,
 3      * 因此,我来定数据是否重复写入的规则,就是:Id是否相同
 4      * 说明:重写该方法,是为了避免HahsSet集合的校验哈希吗值,而导致的错误的比较结果
 5      * @return
 6      */
 7     @Override
 8     public int hashCode()
 9     {
10         return this.getId();
11     }
12     /**
13      * 重写equals()方法,因为是将该对象写入到集合的,
14      * 因此,我来定数据是否重复写入的规则,就是:Id是否相同
15      * @param
16      * @return
17      */
18     @Override
19     public boolean equals(Object obj)
20     {
21         boolean equal = false;
22         
23         if (obj == null)
24         {
25             equal = false;
26         }
27         else {
28             People people = (People) obj;
29             if (people.getId() == this.getId())
30             {
31                 equal = true;
32             }
33         }
34         return equal;
35     }
原文地址:https://www.cnblogs.com/Wfei/p/3251300.html