编程基本功(一)

【题目一】编写一个程序,在终端输入一个字符,输出它的的ASCII码

   1:  public class ToAsc
   2:  {
   3:      public static void main(String[] args)
   4:      {
   5:       Scanner sc=new Scanner(System.in);
   6:       String s=sc.nextLine();//扫描器扫描到输入数据
   7:     for(int i=0;i<s.length();i++)
   8:     {
   9:           char c=s.charAt(i);//从字符串中分离出字符
  10:           int a=(int)c;//将字符转化为ASCII码
  11:           System.out.println(c+"的ASCII码是"+a);
  12:        
  13:      }
  14:      }
  15:   
  16:  }
测试数据:

to be or
t的ASCII码是116
o的ASCII码是111
的ASCII码是32
b的ASCII码是98
e的ASCII码是101
的ASCII码是32
o的ASCII码是111
r的ASCII码是114

题目二编写一个程序,在一串字符串当中查找时候含有要求格式的字符串,并在用户输入的字符串当中输出重复出现的字符串和出现和仅出现一次的字符串

   1:  public class pipei
   2:  {
   3:   
   4:      public static void main(String[] args)
   5:      {
   6:              Pattern p=Pattern.compile("JAVA.*");//声明要匹配的字符串
   7:              Matcher m=p.matcher("JAVA 不是人");//匹配要与之匹配的字符串
   8:              boolean b=m.matches();
   9:              if(b)
  10:              {
  11:                  System.out.print("匹配正确");
  12:                  
  13:              }
  14:              else System.out.print("匹配错误");
  15:              Scanner sc=new Scanner(System.in);
  16:              String s1=sc.nextLine();
  17:              HashSet hash1=new HashSet();
  18:               HashSet hash2=new HashSet();
  19:               String s[]=s1.split(" ");
  20:              for(int i=0;i<s.length;i++)
  21:              {
  22:                  if(!hash1.add(s[i]))//不添加相同的字符串
  23:                      hash2.add(s[i]);//添加hash1不添加的相同字符串
  24:                  
  25:              }
  26:              hash1.removeAll(hash2);//集合hash1-hash2得到出现次数不相同的字符串
  27:              System.out.println("不同的单词有:"+hash1);
  28:              System.out.println("相同的单词有:"+hash2);
  29:      
  30:      }
  31:  }
测试:

匹配正确
to be or not to be
不同的单词有:[not, or]
相同的单词有:[to, be]

posted @ 2012-05-12 18:41  寻梦启示  阅读(1045)  评论(0编辑  收藏  举报