编写一个截取字符的函数,输入为一个字符串和字节数,输出为按字节截取的字符串,但是要保证汉字不被截半个,如(“我ABC”,4),应该截为“我AB”,输入为(“我ABC汉DEF”,6),应该截为“我ABC”,而不是“我ABC+汉的一半”编写一个应用程序,用户用键盘键入5个学生的分数,程序按照成绩排序输出学生的姓名和分数,要求用Student类封装学生的相关信息,输入5个Student类对象利用vector工具类保存并进行排序及遍历输出

解决方案 »

  1.   

    第一个论坛上好像讨论很多了,不过不是很清楚
    第二个简单,只要Student按分数定义Comparator接口就可以用list排序然后输出了
      

  2.   

    第一个
    public class CutStr{

    public static String cutString(String s , int i){
    int j=0;

    byte[] sb=s.getBytes();
    for (int m=0; m<i ;m++ )
    if (sb[m]<0)  j++;

    i=i-(j+1)/2;
    i= i>0?i:0; 

    return s.substring(0,i);
    }

    public static void main(String[] args){

    System.out.println( cutString("a我是fghj",4) );
    }
    }
      

  3.   

    第二个
    import java.util.*;
    import java.io.*;public class Student implements Comparable{
    private String name ="";
    private double score = 0;

    public Student (String name, double score){
    this.name = name;
    this.score = score;
    }

    public String getName(){
    return name;
    }

    public double getScore(){
    return score;
    }

    public int compareTo(Object o1){
    Student s1 = (Student)o1;

    if ( this.score > s1.getScore() ) return 1;
    if ( this.score < s1.getScore() ) return -1;
    return 0;
    }

    public static void main(String[] args){

    Vector stuVector = new Vector();

    try
    {
    BufferedReader in =
             new BufferedReader(
               new InputStreamReader(System.in));
        
         Student s;
         String s1="";
         String s2="";
        
         System.out.println("Please input the 5 students' name and score!");
        
         for (int i=0; i<5; i++){   
        
          
         s1 = in.readLine() ;
         s2 = in.readLine() ;
         s = new Student(s1, Double.valueOf(s2).doubleValue());
         stuVector.add(s);
        
         System.out.println(" a student added!  name:"+ s1 + "  score:"+s2);
         } }
    catch(Exception e)
    {
    e.printStackTrace();
    }

    Collections.sort(stuVector); 

    System.out.println("The ordered Student name:");

       for(int i = 0;i<stuVector.size();i++){
        Student s = (Student)stuVector.get(i);
        System.out.println("  " + s.getName());
    }

    }



    }
      

  4.   

    sorry,我想问个问题,在JAVA里 char类型包含16位(两字节)Unicode字符 为什么在这道题里 字符串里的字符是8位啊?  我是初学JAVA的,谢谢各位了.
      

  5.   

    zebra007(呼呼) 师兄  第一题 byte[] sb=s.getBytes();   
    在这里为什么中文 也就是sb[2] sb[3]...是负数啊?
      

  6.   

    回楼上
    字符既可以用16位的char表示,也可以用两个8位的byte表示阿,在Java里可以互相转换的,比如"一"可以用(char)19968或byte的-46加上-69表示
    中文用byte表示的话需要两位,且第一位大约在-127到-3之间,必小于0
      

  7.   

    是的,
    中文的byte第一位是1,所以是负数
      

  8.   

    二楼回复的第一个好象有点复杂
    “第一个
    public class CutStr{

    public static String cutString(String s , int i){
    //int j=0;

    byte[] sb=s.getBytes();
                      //for (int m=0; m<i ;m++ )
    // if (sb[m]<0)  j++;

    //i=i-(j+1)/2;
    //i= i>0?i:0; 
    //上面这些都不需要,只需加一句
                      if(sb[i]<0)  i--;
                      
    return s.substring(0,--i);
    }

    public static void main(String[] args){

    System.out.println( cutString("a我是fghj",4) );
    }
    }
      

  9.   

    public class CutStr{

    public static String cutString(String s , int i){
    //int j=0;

    byte[] sb=s.getBytes();
                      //for (int m=0; m<i ;m++ )
    // if (sb[m]<0)  j++;

    //i=i-(j+1)/2;
    //i= i>0?i:0; 
    //上面这些都不需要,只需加一句
                      if(sb[i]<0)  i--;
                      
    return s.substring(0,--i);
    }

    public static void main(String[] args){

    System.out.println( cutString("a我是fghj",4) );
    }
    }
    如果输入得值大于字符数组得长度,没有判断,
      

  10.   

    wangzhisheshou(王之射手) 少了i值的范围检查.sb[i]有可能失败.cutString(String s, int offset, int count)
    这样功能会更强一些.---初学者
      

  11.   

    二楼的理解是取出i个有意思的字符(包括汉字),单位是有意思的符号
    wangzhisheshou(王之射手) 为取i 个byte想想挺难搞的,又犯糊涂了.---初学者
      

  12.   

    wangzhisheshou(王之射手) 
    必须要检查i前面的汉字数 
    因为是一个汉字要算2个byte,而题目要求以byte计数你的方法如果 输入是
    System.out.println( cutString("我我是fghj",5) );
    则输出是“我我是”结果和题意不符
      

  13.   

    zebra007(呼呼) 
    你的方法效率不高啊.---初学者
      

  14.   

    wlmmlw(吴铭) ( ) 
    应该怎么样呢?
    给个解决方法好吗
      

  15.   

    //上面这些都不需要,只需加一句
                      if(sb[i]<0)  i--;
                      
    return s.substring(0,--i);
    方法有问题:
    如果输入的是: cutString("abc方法测试defghijkl", 12)
    输出的将是:   abc方法测试defgh
    而题意应该是: abc方法测试d
      

  16.   

    zebra007(呼呼) 写的对!
      

  17.   

    wangzhisheshou(王之射手) 
    写的是错误的!用cutString(“我是射手”,4)即可以测试一下!
      

  18.   

    在JAVA中String的汉字它也是算一个字呀!就如同字母一样使用!
      

  19.   

    看了一下,第一题做法显得有点复杂,借用他人的代码修改一下public class CutStr{

    public static String cutString(String s , int i){
    byte[] sb=s.getBytes();
                      if (i < sb.length) { //长度判断                  
    return new String(sb, 0, i);  //这样根本就不需要减来减去的
    } else {
    return s;
    }
    }

    public static void main(String[] args){

    System.out.println( cutString("a我是fghj",4) );
    }
    }
      

  20.   

    哈哈,我犯糊涂了,失策失策public class CutStr{

    public static String cutString(String s , int i){
    byte[] sb=s.getBytes();
                      if (i < sb.length) { //长度判断                  
    return new String(sb, 0, (sb[i]<0?(i-1):i)); 
    } else {
    return s;
    }
    }

    public static void main(String[] args){

    System.out.println( cutString("a我是fghj",4) );
    }
    }
      

  21.   

    taoyuming(松松) 说的char应该是1个字节才对吧
    short才是2个字节~
    还有,第一题应该是要我们自己输入数据吧,而不是指定的数据……
      

  22.   

    又看了一下这帖子,仔细看了各位的回复,发现第一题汉字个数还是需要统计的,丢人丢人啊,哈哈
    二楼的方法应该是没问题的public class CutStr{

    public static String cutString(String s , int i){
    byte[] sb=s.getBytes();
    if (i < sb.length) {
    boolean b = true;
    for (int j=0; j<i; j++) {
    if (sb[j]<0) b = !b;  //如果有半个汉字则b为false
    }
                   return new String(sb, 0, (b?i:(i-1)));
    } else {
    return s;
    }
    }

    public static void main(String[] args) { System.out.println( cutString("a我是fghj",4) );
    }
    }另外 to shallat(艾劲):
    你对函数的输入输出有所误解,函数的输入不是指自己输入数据,而是指函数的参数,函数的输出是指函数的返回值
      

  23.   

    vector????jdk1.5已经打算淘汰的容器了.这个公司真懒,还用旧题目
      

  24.   

    大家看看我的做法:
    public class Test
    {
    public String change(String str, int num)
    {
    char[] ch = new char[num];
    int j = 0;
    for (int i=0; i<str.length(); i++)
    {
    int b = (int)str.charAt(i);
    if (b > 128)
    {
    j += 2;
    if (j < num+1)
    ch[i] = str.charAt(i);
    }
    else
    {
    j += 1;
    if (j < num+1)
    ch[i] = str.charAt(i);
    }
    }
    return new String(ch);
    } public static void main(String[] args)
    {
    String s = "我ABC汉DEF";
    System.out.println(new Test().change(s, 6));
    }
    }
    简单说明一下,一般英文的字符的ASCII码是小于128。
    新手,所以思路简单,但是代码太长。
      

  25.   

    总结各位前辈的^_^
    public class CutString {
    public static void main(String args[]) {
    String str="我喜欢JAVA:)";
    for(int i=0;i<=str.length();i++) {//循环打印
    System.out.println(cutString(str,i));
    }
    }
    public static String cutString(String s,int len) {
    byte [] sb=s.getBytes();
    boolean b=true;
    for(int i=0;i<len;i++) {
    if(sb[i]<0 && b==true) {//遇到汉字len++
    len++;
    b=false;
    }else{
    b=true;
    }
    }
    return new String(sb,0,len);
    }
    }
      

  26.   

    static String cutstr(String str, int n) {
            int i;
            for (i = 0; i < n; i++) {
                if (str.charAt(i) > 127)
                    n--;
            }
            return str.substring(0, n);
        }女生写的,呵呵
      

  27.   

    rcom10002(KNIGHTRCOM) ( )
    好,这个应该是最简单的了
      

  28.   

    直接substring()函数就可以了,更本不用判断