leochiang

BlogJava 联系 聚合 管理
  21 Posts :: 0 Stories :: 1 Comments :: 0 Trackbacks

在Java中有8种基本数据类型,其中6种是数值类型,另外两种分别是字符类型和布尔类型。而6种数值类型中有4种是整数类型,另外两种是浮点类型
    基本数据类型
        ......................数值型
                                        ................整数类型(byte 1字节 short 2字节 int 4字节 long 8字节)
                                        ................浮点类型 (float 4字节 double 8字节)

        ......................字符型   (char  2字节 存储unicode编码的字符)
        
        ......................布尔型   (boolean。。。true or false)

试题一

1 switch(expr)
expr只能为整数或枚举类型   类型转换由低级到高级。。。(byte short char int)
    试题 二
     编写一个截取字符串的函数,输入为一个字符串和字节数,输出为按字节截取的字符串。但是要保证汉字不被截半个,如"我ABC"4,应该截为"我AB",输入"我ABC汉DEF"6,应该输出"我ABC",而不是"我ABC+汉的半个"。
         1 package core_java;
 2 import java.util.Scanner;
 3 
 4 public class InterceptionStr {
 5 
 6     /**
 7      * @param args
 8      */
 9     static String ss;
10     static int n;
11     public static void Interception(String[] string){
12         int count = 0;
13         String m = "[\u4e00-\u9fa5]";
14         //汉字的正则表达式
15         System.out.println("以每"+ n +"字节划分的字符串如下所示:");
16         for (int i = 0; i < string.length; i++) {
17             if(string[i].matches(m)){
18                 count = count + 2;
19             }else{
20                 count = count + 1;
21             }
22             if(count<n){
23                 System.out.print(string[i]);
24             }else if(count==n){
25                 System.out.print(string[i]);
26                 count = 0;
27                 System.out.println();
28             }else{
29                 count = 0;
30                 System.out.println();
31             }
32         }
33         
34     }
35     public static String[] setValue(){
36         //此方法将字符串转化为字符串数组
37         String[] str = new String[ss.length()];
38         //创建字符数组
39         for (int i = 0; i < str.length; i++) {
40             str[i] = ss.substring(i, i+1);
41         }
42         return str;
43     }
44     public static void main(String[] args) {
45         // TODO Auto-generated method stub
46         System.out.println("请输入字符串:");
47         Scanner scStr = new Scanner(System.in);
48         ss = scStr.next();
49         System.out.println("请输入字节数:");  
50         Scanner scByte = new Scanner(System.in);
51         n = scByte.nextInt();
52         Interception(setValue());
53         
54     }
55 
56 }
57 

 得出的结果是截取的一段一段的,比题目给出的丰富了点

 

posted on 2012-07-30 14:06 leoChiang 阅读(171) 评论(0)  编辑  收藏 所属分类: 技术文档

只有注册用户登录后才能发表评论。


网站导航: