rt

解决方案 »

  1.   

    StringBuffer space= new StringBuffer();
    for(int i= 0;i<指定的个数j;i++)
    {
       space.apend(" ");//这里是空格
    }
    System.out.println(space.toString());//ok好了
      

  2.   

    public String multipleSpaces(int n){
       String output = "";   for(int i=0; i<n; i++)
          output += " ";   return output;
    }
      

  3.   

    嗯 应该用StringBuffer效率比较高  不过也可以用字符数组
      

  4.   

    StringBuilder builder = new StringBuilder();
    //num个空格
    for(int i=0;i<num;i++){
       builder(" ");
    }
    String str = builder.toString();
      

  5.   

    //在字符串"1"前面打印8个空格
    System.out.printf("%9s\n", "1");//只打印9个空格
    System.out.printf("%9s\n", "");//在数字1前面打印8个空格
    System.out.printf("%9d\n", 1);
      

  6.   


    这个东西我也学了没有几天,现学现卖,不过这个只能在jdk1.5以后可以运行,我平时写代码很少用1.5的新特性的。
      

  7.   

    1楼效率和5楼效率差不多,比较快;2楼效率最低;4楼效率最快;5楼最简单;
    原因:2楼用字符串相加.每次相加都会创建一个新的临时内存;1楼用StringBuffer因为有线程同步的处理,所以比4楼的StringBuilder慢一些;
    鉴定完毕
      

  8.   

    有兴趣的可以看看下面这个帖子中 23 楼的回复,在那里做了些测试和分析http://topic.csdn.net/u/20080919/09/f42beecb-d099-462a-bd0f-bee849214f95.html另:如果生成的空格长度是定长的话,建议使用 char[] 数组的方式
      

  9.   

    感谢大家恢复,
    特别感谢huoyin,你的答案是我要的结贴....