不使用任何循环语句,递归,输出打印n条(n>1) "Hello World"。

解决方案 »

  1.   

    public void print(int n){
        System.out.println("Hello World");
        n--;
        if(n>0){
            print(n);
        }
    }
    递归就是循环
      

  2.   


        public void printHelloWorld(int i) {
            if (i >= 1) {
                printHelloWorld(i - 1);
                System.out.println("Hello World");
            }
        }
      

  3.   

    不递归?那只有System.out.println("Hello World");
    System.out.println("Hello World");
    System.out.println("Hello World");
    System.out.println("Hello World");
    System.out.println("Hello World");
    ……
      

  4.   

    网上看到有人说用构造函数,构造函数里输出“HelloWorld”,然后产生一个该对象数组,总觉得太恶心了。
      

  5.   

    感觉还是用递归
    package com.question;public class Q9 {
        public void compute(int i) {
            if (i > 0) {
                System.out.println("Number " + i);
                i --;
                compute(i);
            }
        }
        
        public static void main(String[] args) {
            Q9 q9 = new Q9();
            q9.compute(9);
        }
    }
      

  6.   

    解这道题目,利用了c++语言一个非常重要的特性:
    c++允许定义基于statck数据区的Object。由此,不由想到了Java.
    在Java的世界里,所有的类型都是引用(或者称为指针), 对象内存的分配都是通过new从heap上显式的分配,无法在Java里构建基于statck数据区的对象。所以在Java里,这道题目是无解的。
    Java之于c++,既是进步,又是倒退。
    Java语言本身的确帮助c++程序员做了很多事情,比如GC, 去掉了c++中很多复杂的特性,比如多重继承,运算符重载等。
    同时,c++本身的很多优点,也丧失了。 写了c++, 然后再写java,一个明显的感觉是,没法使用java写出像c++一样简洁的程序。
    比如, 没有了运算符重载, 你不得不使用equal方法来表达两个对象的相等。
    Java不能显式表达RAII概念,你不得不使用hard code的方法Log方法的进入和退出, 如:
    func() {
    log("enter func");
    //do something.
    log("exit func");
    }原文:http://www.cppblog.com/sherrylso/archive/2009/01/13/71831.html
      

  7.   


    public static void main(String[] args)
    {
    String org="Hello World";
    int n=10;
    Object[] strs=new Object[n];
    Arrays.fill(strs, org);
    String output=Arrays.toString(strs);
    output=output.replaceAll("([\\[\\]]|[,][\\s])", "\n");
    System.out.println(output);
    }
      

  8.   

    public static void main(String[] args)
    {
            String org="Hello World";
            int n=10;
            Object[] strs=new Object[n];
            Arrays.fill(strs, org);
            String output=Arrays.toString(strs);
            output=output.replaceAll("([\\[\\]]|[,][\\s])", "\n");
            System.out.println(output);
    }标准答案
      

  9.   

    String org="Hello World"; 
    int n=10; back1:if(n>0){
      System.out.println(org);
    }
    n--;
    if(n>0){
     continue back1;
    }
            
      

  10.   

    好像用goto也行
    我记得java好像也保留了goto关键字
      

  11.   


    这段代码有两个问题:
    1.continue不能在if中使用,至少要在循环中使用。
    2.带标签的continue,标签必须定义在希望跳出的最外层循环的前面,这里的back1:至少要放在n--的后面。
      

  12.   

     /**
         * Assigns the specified Object reference to each element of the specified
         * array of Objects.
         *
         * @param a the array to be filled
         * @param val the value to be stored in all elements of the array
         * @throws ArrayStoreException if the specified value is not of a
         *         runtime type that can be stored in the specified array
         */
        public static void fill(Object[] a, Object val) {
            fill(a, 0, a.length, val);
        }    /**
         * Assigns the specified Object reference to each element of the specified
         * range of the specified array of Objects.  The range to be filled
         * extends from index <tt>fromIndex</tt>, inclusive, to index
         * <tt>toIndex</tt>, exclusive.  (If <tt>fromIndex==toIndex</tt>, the
         * range to be filled is empty.)
         *
         * @param a the array to be filled
         * @param fromIndex the index of the first element (inclusive) to be
         *        filled with the specified value
         * @param toIndex the index of the last element (exclusive) to be
         *        filled with the specified value
         * @param val the value to be stored in all elements of the array
         * @throws IllegalArgumentException if <tt>fromIndex &gt; toIndex</tt>
         * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex &lt; 0</tt> or
         *        <tt>toIndex &gt; a.length</tt>
         * @throws ArrayStoreException if the specified value is not of a
         *         runtime type that can be stored in the specified array
         */
        public static void fill(Object[] a, int fromIndex, int toIndex, Object val) {
            rangeCheck(a.length, fromIndex, toIndex);
            for (int i=fromIndex; i<toIndex; i++)
                a[i] = val;
        }糊了个壳子不用循环?
      

  13.   

    goto Java是保留了。可是到现在还没任何用处啊
      

  14.   

     output=output.replaceAll("([\\[\\]]|[,][\\s])", "\n"); 是什么意思?是java中的正值表达式吗
      

  15.   

    是的,去除Arrays.toString()生成的"[","]",", ",并替换成换行("\n")
      

  16.   

    我自己来public static void main(String[] args) 

    String org="Hello World"; 
            int n=10; 
            Object[] strs=new Object[n]; 
            Arrays.fill(strs, org); 
            String output=Arrays.toString(strs); 
            output=output.substring(1,output.length()-1);//去除第一行和最后一行的空行
            output=output.replaceAll("([\\[\\]]|[,][\\s])", "\n"); 
            System.out.println(output); 
    }
      

  17.   

    到最后不还是用了FOR的吗?!!无聊找36楼的看吧。