//请大家把这个接口中的所有方法实现出来(要经得住测试)
public interface Lesson3 {

//输出指定数组中的所有元素(15分)
public <F> void showArray(F[] f);
//返回从键盘输入的一行内容(10分)
public String getInputByKey() throws IOException;
//返回长度为length的大小写字母和数字组成的字符串(20分)
public String getRandomStr(int length);

//将old中的所有出现flag内容的地方补上一个flag内容,返回最后的内容(15分)
public String replaceStr(String old, String flag); //返回length位由数字组成的字符串,flag表示是否可以出现重复的数字(15分)
public String getNumber(int length, boolean flag);
//返回一个大于等于0且小于等于max的数字,若max为负数,抛异常(10分)
public int getNumber(int max ) throws Exception;
//返回sub在str中出现的次数(15分)
public int getCount(String str, String sub);
}
用最简单的方法。。有的题可能只有一句话

解决方案 »

  1.   

    此回复为自动发出,仅用于显示而已,并无任何其他特殊作用
    楼主【shijin8310】截止到2008-07-16 19:50:56的历史汇总数据(不包括此帖):
    发帖的总数量:0                        发帖的总分数:0                        每贴平均分数:0                        
    回帖的总数量:9                        得分贴总数量:1                        回帖的得分率:11%                      
    结贴的总数量:0                        结贴的总分数:0                        
    无满意结贴数:0                        无满意结贴分:0                        
    未结的帖子数:0                        未结的总分数:0                        
    结贴的百分比:---------------------结分的百分比:---------------------
    无满意结贴率:---------------------无满意结分率:---------------------
    如何结贴请参考这里:http://topic.csdn.net/u/20080501/09/ef7ba1b3-6466-49f6-9d92-36fe6d471dd1.html
      

  2.   

    我是一个初学者
       请问<F>是干什么的
      

  3.   

    先写几个: public <F>void showArray(F[] f) {
           for(F temp:f){
               System.out.print(temp+" ");
           }
            System.out.println();
        }
     public int getNumber(int max) throws Exception {
            if(max>=0){
                return max;
            }
            else
                throw new Exception("max < 0");
        }    public int getCount(String str, String sub) {
            int count=0;
            int i=-1;
            while(true){
                i=str.indexOf(sub,i+1);
                if(i==-1)
                break;
                i++;
                count++;
            }
            return count;
        }
    public String replaceStr(String old, String flag) {
            String temp=flag+flag;
            String result=temp.replaceAll(old, temp);
            return result;
        }
      

  4.   

        public int getCount(String str, String sub) {
            int count=0;
            int i=-1;
            while(true){
                i=str.indexOf(sub,i+1);
                if(i==-1)
                break;
                i++;
                count++;
            }
            return count;
        }
      

  5.   

    Java语言:
    public String getInputByKey()throws IOException{
    InputStreamReader isr=null;
    BufferedReader buf=null;
                    System.out.println("请从键盘输入内容:");
    isr=new InputStreamReader(System.in);
    buf=new BufferedReader(isr);
    String s=buf.readLine();
    return s;
    }
      

  6.   


    //输出指定数组中的所有元素(
    public <F>void showArray(F[] f) {
           for(F temp:f){
               System.out.print(temp+" ");
           }
            System.out.println();
    }//返回从键盘输入的一行内容
    public String getInputByKey()throws IOException{ 
           InputStreamReader isr=null; 
           BufferedReader buf=null;  
           isr=new InputStreamReader(System.in); 
           buf=new BufferedReader(isr); 
           String s=buf.readLine(); 
           return s; 
    }//将old中的所有出现flag内容的地方补上一个flag内容,返回最后的内容
    public String replaceStr(String old, String flag) {
            String temp=flag+flag;
            String result=temp.replaceAll(old, temp);
            return result;
    }//返回一个大于等于0且小于等于max的数字,若max为负数,抛异常
    public int getNumber(int max) throws Exception {
            if(max>=0){
                return max;
            }
            else
                throw new Exception("max < 0");
            }
    }//返回sub在str中出现的次数
    public int getCount(String str, String sub) {
            int count=0;
            int i=-1;
            while(true){
                i=str.indexOf(sub,i+1);
                if(i==-1)
                break;
                i++;
                count++;
            }
            return count;
    }
      

  7.   


    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Random;
    import java.util.Set;
    import java.util.TreeSet;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;public class Lesson implements Lesson3
    {
        // 输出指定数组中的所有元素
        public <F> void showArray(F[] f)
        {
            for(F temp: f)
            {
                System.out.println(temp);
            }
        }    // 返回从键盘输入的一行内容
        public String getInputByKey() throws IOException
        {
            InputStreamReader isr = new InputStreamReader(System.in);
            BufferedReader input = new BufferedReader(isr);
            String str = input.readLine();        isr.close();
            input.close();        return str;
        }    // 返回长度为length的大小写字母和数字组成的字符串
        public String getRandomStr(int length)
        {
            final int TOTAL = 26 * 2 + 10;
            char[] chs = new char[TOTAL];
            Random rand = new Random();
            StringBuilder sb = new StringBuilder();
            int index = 0;        for(char ch = 'A'; ch <= 'Z'; ch++)
            {
                chs[index++] = ch;
            }        for(char ch = 'a'; ch <= 'z'; ch++)
            {
                chs[index++] = ch;
            }        for(char ch = '0'; ch <= '9'; ch++)
            {
                chs[index++] = ch;
            }        for(int i = 0; i < length; i++)
            {
                sb.append(chs[rand.nextInt(TOTAL)]);
            }        return sb.toString();
        }    // 将old中的所有出现flag内容的地方补上一个flag内容,返回最后的内容
        public String replaceStr(String old, String flag)
        {
            Pattern p = Pattern.compile(flag);
            Matcher m = p.matcher(old);
            return m.replaceAll(flag + flag);
        }    // 返回length位由数字组成的字符串,flag表示是否可以出现重复的数字
        public String getNumber(int length, boolean flag)
        {
            char[] chs = new char[10];
            Random rand = new Random();
            StringBuilder sb = new StringBuilder();
            int index = 0;        for(char ch = '0'; ch <= '9'; ch++)
            {
                chs[index++] = ch;
            }        // 不可以重复
            if(!flag && length <= 10)
            {
                Set<Character> set = new TreeSet<Character>();            while(set.size() < length)
                {
                    set.add(chs[rand.nextInt(10)]);
                }            for(char ch: set)
                {
                    sb.append(ch);
                }        }
            // 可以重复
            else
            {
                List<Character> list = new ArrayList<Character>();            while(list.size() < length)
                {
                    list.add(chs[rand.nextInt(10)]);
                }            for(char ch: list)
                {
                    sb.append(ch);
                }
            }        return sb.toString();
        }    // 返回一个大于等于0且小于等于max的数字,若max为负数,抛异常
        public int getNumber(int max) throws Exception
        {
            if(max < 0)
            {
                throw new Exception();
            }
            else
            {
                return new Random().nextInt(max + 1);
            }
        }    // 返回sub在str中出现的次数
        public int getCount(String str, String sub)
        {
            int ret = 0;
            Pattern p = Pattern.compile(sub);
            Matcher m = p.matcher(str);        while(m.find())
            {
                ret++;
            }        return ret;
        }
    }
      

  8.   

    6楼和10楼的getNumber(int max)和replaceStr(String old, String flag)都错
    10楼的getInputByKey()未关闭相应的流
      

  9.   


    //狂人也搞错啦,呵呵
    public String getNumber(int length, boolean flag)中应该用HashSet的,不能用TreeSet    // 返回length位由数字组成的字符串,flag表示是否可以出现重复的数字
        public String getNumber(int length, boolean flag)
        {
            char[] chs = new char[10];
            Random rand = new Random();
            StringBuilder sb = new StringBuilder();
            int index = 0;        for(char ch = '0'; ch <= '9'; ch++)
            {
                chs[index++] = ch;
            }        // 不可以重复
            if(!flag && length <= 10)
            {
                Set<Character> set = new HashSet<Character>();            while(set.size() < length)
                {
                    set.add(chs[rand.nextInt(10)]);
                }            for(char ch: set)
                {
                    sb.append(ch);
                }        }
            // 可以重复
            else
            {
                List<Character> list = new ArrayList<Character>();            while(list.size() < length)
                {
                    list.add(chs[rand.nextInt(10)]);
                }            for(char ch: list)
                {
                    sb.append(ch);
                }
            }        return sb.toString();
        }
      

  10.   

    用LinkedHashSet也行,呵呵,其实我还是更倾向于LinkedHashSet
      

  11.   


    getNumber(int max)的要求是:返回一个大于等于0且小于等于max的数字,若max为负数,抛异常,意为在此区间内的随机数,你写的那个只是返回传给它的参数replaceStr(String old, String flag)你自己测一下就知道,参数"ababab", "a"带进去试试,按照题的要求应该返回"aabaabaab"
      

  12.   

    嗯,代码贴错了。
    public static String replaceStr(String old, String flag) {
            String temp=flag+flag;
            System.out.println(temp);
            String result=old.replaceAll(flag, temp);
            return result;
        }
    另一个没有要求随机,这样写我想不能算错吧。
      

  13.   

    那你怎么理解这句 返回一个大于等于0且小于等于max的数字
    你返回的就是max哦
      

  14.   

    我也来show下我的代码,哈哈class Lesson3 {    //输出指定数组中的所有元素(15分)
        public <F> void showArray(F[] f) {
            if (f != null) {
                for (F temp : f) {
                    System.out.print(temp+" ");
                }
            }
            System.out.println();
        }    //返回从键盘输入的一行内容(10分)
        public String getInputByKey() throws IOException {
            return new BufferedReader(new InputStreamReader(System.in)).readLine();
        }    //返回长度为length的大小写字母和数字组成的字符串(20分)
        public String getRandomStr(int length) {
            String charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
            int size = charset.length();
            Random random = new Random();
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < length; i++) {
                sb.append(charset.charAt(random.nextInt(size)));
            }
            return sb.toString();
        }    //将old中的所有出现flag内容的地方补上一个flag内容,返回最后的内容(15分)
        public String replaceStr(String old, String flag) {
            return old.replace(flag, flag + flag);
        }    //返回length位由数字组成的字符串,flag表示是否可以出现重复的数字(15分)
        public String getNumber(int length, boolean flag) {
            if(!flag && length > 10)return "length > 10 && flag = false";        String charset = "0123456789";
            int size = charset.length();
            Random random = new Random();
            StringBuffer sb = new StringBuffer();
            int ri;
            for (int i = 0; i < length; i++) {
                ri = random.nextInt(size);
                sb.append(charset.charAt(ri));
                if (!flag) { //不可重复
                    charset = charset.replace(charset.substring(ri), charset.substring(ri + 1));
                    size = charset.length();
                }
            }
            return sb.toString();
        }    //返回一个大于等于0且小于等于max的数字,若max为负数,抛异常(10分)
        public int getNumber(int max) throws Exception {
            if (max < 0) {
                throw new Exception("max < 0");
            }
            return new Random().nextInt(max + 1);
        }    //返回sub在str中出现的次数(15分)
        public int getCount(String str, String sub) {
            return new StringTokenizer(str, sub, true).countTokens() / 2;
        }
    }
      

  15.   


    我必须得指出狂人的11楼帖getCount和replaceStr都是不对的测试用例:replaceStr("ab.ab.ab", ".")//应该为"ab..ab..ab"
    getCount("ab.ab.ab", ".")//应该为2
    还有关闭流,没注意    //返回从键盘输入的一行内容(10分)
        public String getInputByKey() throws IOException {
            BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
            String line = bf.readLine();
            bf.close();
            return line;
        }
      

  16.   

    对,它满足大于等于0且小于等于max
      

  17.   

    hoho,大意了大意了,谢谢25楼指点,现在帖上第二版
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.util.ArrayList;
    import java.util.LinkedHashSet;
    import java.util.List;
    import java.util.Random;
    import java.util.Set;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;public class Lesson
    {
        // 输出指定数组中的所有元素
        public <F> void showArray(F[] f)
        {
            for(F temp: f)
            {
                System.out.println(temp);
            }
        }    // 返回从键盘输入的一行内容
        public String getInputByKey() throws IOException
        {
            InputStreamReader isr = new InputStreamReader(System.in);
            BufferedReader input = new BufferedReader(isr);
            String str = input.readLine();        isr.close();
            input.close();        return str;
        }    // 返回长度为length的大小写字母和数字组成的字符串
        public String getRandomStr(int length)
        {
            final int TOTAL = 26 * 2 + 10;
            char[] chs = new char[TOTAL];
            Random rand = new Random();
            StringBuilder sb = new StringBuilder();
            int index = 0;        for(char ch = 'A'; ch <= 'Z'; ch++)
            {
                chs[index++] = ch;
            }        for(char ch = 'a'; ch <= 'z'; ch++)
            {
                chs[index++] = ch;
            }        for(char ch = '0'; ch <= '9'; ch++)
            {
                chs[index++] = ch;
            }        for(int i = 0; i < length; i++)
            {
                sb.append(chs[rand.nextInt(TOTAL)]);
            }        return sb.toString();
        }    // 将old中的所有出现flag内容的地方补上一个flag内容,返回最后的内容
        public String replaceStr(String old, String flag)
        {
            return old.replaceAll("[" + flag + "]", flag + flag);
        }    // 返回length位由数字组成的字符串,flag表示是否可以出现重复的数字
        public String getNumber(int length, boolean flag)
        {
            char[] chs = new char[10];
            Random rand = new Random();
            StringBuilder sb = new StringBuilder();
            int index = 0;        for(char ch = '0'; ch <= '9'; ch++)
            {
                chs[index++] = ch;
            }        // 不可以重复
            if(!flag && length <= 10)
            {
                Set<Character> set = new LinkedHashSet<Character>();            while(set.size() < length)
                {
                    set.add(chs[rand.nextInt(10)]);
                }            for(char ch: set)
                {
                    sb.append(ch);
                }        }
            // 可以重复
            else
            {
                List<Character> list = new ArrayList<Character>();            while(list.size() < length)
                {
                    list.add(chs[rand.nextInt(10)]);
                }            for(char ch: list)
                {
                    sb.append(ch);
                }
            }        return sb.toString();
        }    // 返回一个大于等于0且小于等于max的数字,若max为负数,抛异常
        public int getNumber(int max) throws Exception
        {
            if(max < 0)
            {
                throw new Exception();
            }
            else
            {
                return new Random().nextInt(max + 1);
            }
        }    // 返回sub在str中出现的次数
        public int getCount(String str, String sub)
        {
            int ret = 0;
            Pattern p = Pattern.compile("[" + sub + "]");
            Matcher m = p.matcher(str);        while(m.find())
            {
                ret++;
            }        return ret;
        }
    }
      

  18.   

    帖上第三版,字符串那两个确实有点深度,hoho
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.util.ArrayList;
    import java.util.LinkedHashSet;
    import java.util.List;
    import java.util.Random;
    import java.util.Set;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;public class Lesson
    {
        // 输出指定数组中的所有元素
        public <F> void showArray(F[] f)
        {
            for(F temp: f)
            {
                System.out.println(temp);
            }
        }    // 返回从键盘输入的一行内容
        public String getInputByKey() throws IOException
        {
            InputStreamReader isr = new InputStreamReader(System.in);
            BufferedReader input = new BufferedReader(isr);
            String str = input.readLine();        isr.close();
            input.close();        return str;
        }    // 返回长度为length的大小写字母和数字组成的字符串
        public String getRandomStr(int length)
        {
            final int TOTAL = 26 * 2 + 10;
            char[] chs = new char[TOTAL];
            Random rand = new Random();
            StringBuilder sb = new StringBuilder();
            int index = 0;        for(char ch = 'A'; ch <= 'Z'; ch++)
            {
                chs[index++] = ch;
            }        for(char ch = 'a'; ch <= 'z'; ch++)
            {
                chs[index++] = ch;
            }        for(char ch = '0'; ch <= '9'; ch++)
            {
                chs[index++] = ch;
            }        for(int i = 0; i < length; i++)
            {
                sb.append(chs[rand.nextInt(TOTAL)]);
            }        return sb.toString();
        }    // 将old中的所有出现flag内容的地方补上一个flag内容,返回最后的内容
        public String replaceStr(String old, String flag)
        {
            String newFlag = getRegexStr(flag);
            return old.replaceAll(newFlag, newFlag + newFlag);
        }    // 返回length位由数字组成的字符串,flag表示是否可以出现重复的数字
        public String getNumber(int length, boolean flag)
        {
            char[] chs = new char[10];
            Random rand = new Random();
            StringBuilder sb = new StringBuilder();
            int index = 0;        for(char ch = '0'; ch <= '9'; ch++)
            {
                chs[index++] = ch;
            }        // 不可以重复
            if(!flag && length <= 10)
            {
                Set<Character> set = new LinkedHashSet<Character>();            while(set.size() < length)
                {
                    set.add(chs[rand.nextInt(10)]);
                }            for(char ch: set)
                {
                    sb.append(ch);
                }        }
            // 可以重复
            else
            {
                List<Character> list = new ArrayList<Character>();            while(list.size() < length)
                {
                    list.add(chs[rand.nextInt(10)]);
                }            for(char ch: list)
                {
                    sb.append(ch);
                }
            }        return sb.toString();
        }    // 返回一个大于等于0且小于等于max的数字,若max为负数,抛异常
        public int getNumber(int max) throws Exception
        {
            if(max < 0)
            {
                throw new Exception();
            }
            else
            {
                return new Random().nextInt(max + 1);
            }
        }    // 返回sub在str中出现的次数
        public int getCount(String str, String sub)
        {
            int ret = 0;
            Pattern p = Pattern.compile(getRegexStr(sub));
            Matcher m = p.matcher(str);        while(m.find())
            {
                ret++;
            }        return ret;
        }
        
        private String getRegexStr(String str)
        {
            StringBuilder temp = new StringBuilder();
            
            for(char ch: str.toCharArray())
            {
                if(Character.isLetter(ch) || Character.isDigit(ch))
                {
                    temp.append(ch);
                }
                else
                {
                    temp.append("\\" + ch);
                }
            }
            
            return temp.toString();
        }
    }
      

  19.   

    原来是狂人老三,请你告诉我怎么测试?
    我现在叔叔个公司里直接做JAVA WEB开发 什么都不懂就硬上,我也是狂人 嘎嘎
      

  20.   

    自己加个main方法进去就行了呀,想测哪个就调用那个比如我想测getRandomStr    public static void main(String args[])
        {
            Lesson le = new Lesson();
            System.out.println(le.getRandomStr(25));
        }
      

  21.   

    各位测一下看29楼的还有没有bug,在此帖结帖之前测出者我将开专帖散分50/人
      

  22.   

    class  
    {
    public static void main(String[] args) 
    {
    System.out.println("Hello World!");
    }
    }
      

  23.   

    public String getRandomStr(int length){                StringBuffer sb = new StringBuffer();
    Random rd = new Random(1);
    int j = 0;
    while(1==1){
    int i = rd.nextInt(123);
    if((i>=48 && i<=57) || (i<='Z' && i>='A') || (i<='z' && i>= 'a')){
    sb.append((char)i);
    j++;
    }
    if(j==10){
    break;
    }
    }
                    return sb.toString();
             }
      

  24.   

    唉!!
    怎么没人欣赏下我的public String getNumber(int length, boolean flag)方法啊,受伤ing~~~~~~~~~~~~~~~~不过,才发现我的getCount(String str)方法是错的,郁闷!!
      

  25.   

    public <F> void showArray(F[] f) {
    for(int i=0;i<f.length;i++){
    System.out.println(f[i]);
    }
    }public String getInputByKey() throws IOException {
    BufferedReader br=null;
    br=new BufferedReader(new InputStreamReader(System.in));
    String s=br.readLine();
    br.close();
    return s;
    }public String replaceStr(String old, String flag) {
    StringBuffer sb=new StringBuffer();
    int index=old.indexOf(flag);
    sb.insert(index, flag);
    return sb.toString();
    }public int getNumber(int max) throws Exception {
    if(max<0){
    throw new Exception();
    }
    int i=0;
    do{
    i=new Random().nextInt();
    if(i>=0 && i<=max){
    break;
    }
    }while(true);
    return i;
    }public int getCount(String str, String sub) {
    int count=0,index=0;
    while((index=str.indexOf(sub))!=-1){
    count++;
    str=str.substring(index+sub.length());
    }
    return count;
    }