A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

本帖最后由 AndroidM 于 2015-4-12 22:27 编辑

题目一:写一个正则表达式,可以匹配手机号。规则: 第1位是1,第二位可以是数字3458其中之一,后面4位任意数字,最后5位为任意相同的数字。例如:18601088888、13912366666
  1. public class Test1
  2. {
  3.         public static void main(String[] args)   {            
  4.                 String reg = "1[3458]\\d{4}(\\d){5}";            
  5.                 String[] tel ={"13899833333",
  6.                                            "13899833d333",
  7.                                            "17899844533",
  8.                                            "15899833333",
  9.                                            "13899833333"};            
  10.                 for(int i=0; i<tel.length; i++)
  11.                         System.out.println(tel[i]+"  is  "+checkTel(tel[i],reg));
  12.         }   
  13.         static boolean checkTel(String tel,String reg)    {
  14.                 return tel.matches(reg);        
  15.         }
  16. }
复制代码

题目二:编写程序,打印1到100之内的整数,但数字中包含7的要跳过,例如:17、27、71、72
  1. public class Test2
  2. {
  3.         public static void main(String[] args){   
  4.                 String s;
  5.                 for(int i=1; i<=100; i++){
  6.                         if(Integer.toString(i).indexOf('7') == -1)
  7.                                 System.out.print(i+" ");                        
  8.                 }        
  9.         }
  10. }
复制代码

题目三:编写程序,从一个字符串中按字节数截取一部分,但不能截取出半个中文,例如:从“hello枷哇”中截取2个字节是“he,截取6个字节也要是"hello",而不要出现半个中文
  1. public class Test3{
  2.         public static void main(String[] args){
  3.                 String s = "hello枷哇";                                
  4.                 System.out.println(getByteChar(s,2));               
  5.                 System.out.println(getByteChar(s,5));        
  6.                 System.out.println(getByteChar(s,6));        
  7.                 System.out.println(getByteChar(s,7));               
  8.         }
  9.         static String getByteChar(String s, int num){
  10.                 byte[] by = s.getBytes();
  11.                 String des = null;
  12.                 int count = 0;
  13.                
  14.                 if((s==null)||(by.length <= 0 )|| num <=0 )
  15.                         return "worng !!!";        
  16.                 for(int i=0; i<num; i++)
  17.                         if(by[i] < 0)//当字节为中文字的一半时,值为负数
  18.                                 count++;               
  19.                 if(count%2 !=0)
  20.                         des = new String(by, 0,--num);
  21.             else
  22.                     des = new String(by, 0, num);                    
  23.                 return des;
  24.         }
  25. }
复制代码



代码中如果有错误,麻烦指出,大家一起交流,共同进步哈。。

题目二Test2运行结果.png (7.33 KB, 下载次数: 2)

题目二Test2运行结果.png

题目三Test3运行结果.png (3.78 KB, 下载次数: 2)

题目三Test3运行结果.png

题目一Test1运行结果.png (3.49 KB, 下载次数: 2)

题目一Test1运行结果.png

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马