要求实现方法里面的代码:
1. function1()
   返回相同闊度數字型字串. 例:
   function1(“000000”) => “000001”
   function1(”0023")   => “0024”
   function1(“0009”)   => “0010”
   function1(“000099”) => “000100”
   function1(“9”) => “0” //號碼循環再用   程式接口:
   public static String function1(String num){....} 2. function2()
   將數字四捨五入至某小數位, 並返回指定位數字串
   function2(123.455,2) => 123.46
   function2(123.449,2) => 123.45
   function2(123.44,3) => 123.440  
   function2(123.4455,3) => 123.446
   function2(123.4499,3) => 123.450
   function2(123.9,0) => 124   程式接口:
   public static double function2(double value,int decimalPlaces){....} 

解决方案 »

  1.   

    第1题:
            String input = "9990";
            int length = input.length();
            String formatter = "%1$0".concat(String.valueOf(length)).concat("d");
            String output = String.format(formatter, Integer.parseInt(input) + 1);
            System.out.println(output.substring(output.length() - input.length()));code]第2题:
    [code=Java]        double input2a = 111.52346;
            int input2b = 0;
            String formatter2 = "%1$.".concat(
                    String.valueOf(input2b + String.valueOf(input2a).indexOf('.')))
                    .concat("g");
            String output2 = String.format(formatter2, input2a + 1);
            System.out.println(output2);建议看看String.format的javadoc。
    如果嫌麻烦,可以看这个:http://gceclub.sun.com.cn/Java_Docs/jdk6/html/zh_CN/api/index.html
      

  2.   

    本帖最后由 java2000_net 于 2010-03-10 06:30:14 编辑
      

  3.   

    我只会第一个嘿嘿。把代码贴出来给大家看看
    import java.io.InputStreamReader;
    public class test {
    public String function1(String str){
    String rstr="";
    int snum = Integer.parseInt(str);
    int len =str.length();
    snum++;
    rstr =String.format("%0"+len+"d",snum );
    return rstr;
    }
    public static void main(String[] args){
            System.out.println("input:");
    BufferedReader instr=new BufferedReader(new InputStreamReader
                                                              (System.in));
    test ts=new test();
    String fstr="";
    try {
    fstr=instr.readLine();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
    String outstr= ts.function1(fstr);
    System.out.println(outstr);
           }
    }