各位大哥,谁给个函数
功能为得到一个字符串,共x位,该字符串的后y位为一个其他的字符串,而前x-y位用0填充
谢谢

解决方案 »

  1.   

    public String function (int x, String s) {
        String temp = "";
        for (int i = 0; i < x; i++) {
            temp = temp + "0";
        }
        s = temp + s;
        return s;
    }
      

  2.   

    public String makeYourString(int x, int y)
    {
      String s = "" ;
      for(int i = 0; i < y; i++)
        s += i ;
      makeYourString(x, y, s) ;
    }public String makeYourString(int x, int y, String s)
    {
      if((x-y) < 0)
        return null ; 
      String str = "";
      for(int i = 0; i < (x-y); i++)
        str += "0" ;
      
      str += s ;
      return str ;

      
    }
      

  3.   

    public String makeYourString(int x, int y)
    {
      String s = "" ;
      for(int i = 0; i < y; i++)
        s += i ;
      return makeYourString(x, y, s) ;
    }public String makeYourString(int x, int y, String s)
    {
      if((x-y) < 0)
        return null ; 
      String str = "";
      for(int i = 0; i < (x-y); i++)
        str += "0" ;
      
      str += s ;
      return str ;
    }
      

  4.   

    public String function (int x, String s) {
        String temp = "";
        for (int i = 0; i < x-s.length; i++) {
            temp +=  "0";
        }
        s = temp + s;
        return s;
    }
      

  5.   

    为了保险期间我觉得应该在循环之前调用函数之前先判断一下s.lenght<x是否成立
    if(s.lenght<x)
    {
      function (int x, String s);
    }
    成立的话在s 的前面补x-s.lenght个0;