怎样在不使用循环及递归的情况下打印出n个 hello  (n>=1) 。api里的循环也算挖坟挖出来的个题。

解决方案 »

  1.   

    String  str="Hello";
    int  num=4;
    Object[]  obj=new  Object[num];
    Arrays.fill(obj,str);
    String  s=Arrays.toString(obj);
    s=s.replaceAll("([\\[\\]]|[,][\\s])","\n");
    System.out.println(s);
    你挖,我也挖
    javas2高人的回答
      

  2.   

    arrays.fill()是用什么实现的????                循环赋值                so
      

  3.   

    System.out.println("hello");
    System.out.println("hello");
    System.out.println("hello");
            ...
    有多少打多少吧,嘿嘿
      

  4.   

    此题无解。因为println本身已经调用循环了。
      

  5.   

    我先来一个public static void main(String[] args) {
    Timer timer = new Timer();
    timer.scheduleAtFixedRate(new TimerTask(){ @Override
    public void run() {
    System.out.println("Hello world");
    }
    }, 0, 1000);
    }
      

  6.   

    捣鼓了一下,被大胡子抢先了public class Test {
    public static void main(String[] args) throws Exception {
    new Thread(new Printer(10)).start();
    }
    }class Printer implements Runnable {
    int i; Printer(int i) {
    this.i = i;
    } public void run() {
    if (i > 0) {
    System.out.println("hello"); new Thread(new Printer(i - 1)).start();
    }
    }
    }
      

  7.   


    有Timer不用,还自己写线程
      

  8.   

    不用循环 h e l l o 怎么会出现呢?
    那个是开玩笑,因为你的题目不严谨,没说循环、递归到哪里为止。比如native的不算。另外,你那个Printer 其实也是递归
      

  9.   

    大胡子的public static void main(String[] args) {
            Timer timer = new Timer();
            timer.scheduleAtFixedRate(new TimerTask(){            @Override
                public void run() {
                    System.out.println("Hello world");
                }
            }, 0, 1000);
        }
      

  10.   

    大胡子用到循环了!!!
    java.util.TimerThread.mainLoop方法
      

  11.   


    /**
     * This "helper class" implements the timer's task execution thread, which
     * waits for tasks on the timer queue, executions them when they fire,
     * reschedules repeating tasks, and removes cancelled tasks and spent
     * non-repeating tasks from the queue.
     */
    class TimerThread extends Thread {
        /**
         * This flag is set to false by the reaper to inform us that there
         * are no more live references to our Timer object.  Once this flag
         * is true and there are no more tasks in our queue, there is no
         * work left for us to do, so we terminate gracefully.  Note that
         * this field is protected by queue's monitor!
         */
        boolean newTasksMayBeScheduled = true;    /**
         * Our Timer's queue.  We store this reference in preference to
         * a reference to the Timer so the reference graph remains acyclic.
         * Otherwise, the Timer would never be garbage-collected and this
         * thread would never go away.
         */
        private TaskQueue queue;    TimerThread(TaskQueue queue) {
            this.queue = queue;
        }    public void run() {
            try {
                mainLoop();
            } finally {
                // Someone killed this Thread, behave as if Timer cancelled
                synchronized(queue) {
                    newTasksMayBeScheduled = false;
                    queue.clear();  // Eliminate obsolete references
                }
            }
        }    /**
         * The main timer loop.  (See class comment.)
         */
        private void mainLoop() {
            while (true) {
                try {
                    TimerTask task;
                    boolean taskFired;
                    synchronized(queue) {
                        // Wait for queue to become non-empty
                        while (queue.isEmpty() && newTasksMayBeScheduled)
                            queue.wait();
                        if (queue.isEmpty())
                            break; // Queue is empty and will forever remain; die                    // Queue nonempty; look at first evt and do the right thing
                        long currentTime, executionTime;
                        task = queue.getMin();
                        synchronized(task.lock) {
                            if (task.state == TimerTask.CANCELLED) {
                                queue.removeMin();
                                continue;  // No action required, poll queue again
                            }
                            currentTime = System.currentTimeMillis();
                            executionTime = task.nextExecutionTime;
                            if (taskFired = (executionTime<=currentTime)) {
                                if (task.period == 0) { // Non-repeating, remove
                                    queue.removeMin();
                                    task.state = TimerTask.EXECUTED;
                                } else { // Repeating task, reschedule
                                    queue.rescheduleMin(
                                      task.period<0 ? currentTime   - task.period
                                                    : executionTime + task.period);
                                }
                            }
                        }
                        if (!taskFired) // Task hasn't yet fired; wait
                            queue.wait(executionTime - currentTime);
                    }
                    if (taskFired)  // Task fired; run it, holding no locks
                        task.run();
                } catch(InterruptedException e) {
                }
            }
        }
    }
      

  12.   

    just for fun. 吃饱了没事干,玩呗!
      

  13.   

    replaceAll也是循环实现,so...public String replaceAll(String replacement) {
            reset();
            boolean result = find();
            if (result) {
                StringBuffer sb = new StringBuffer();
                do {
                    appendReplacement(sb, replacement);
                    result = find();
                } while (result);
                appendTail(sb);
                return sb.toString();
            }
            return text.toString();
        }
      

  14.   

    那你上一张自己的PP吧。看你空间是个MM?
      

  15.   

    再来一个
    java代码public class Hello{
    public static void main(String[] args){
    System.out.println("hello");
    }
    }再在Hello.java旁边兴建一个文本命名为run.cmd里面写上一下内容
    @echo off
    javac Hello.java
    set var=0
    :b
    call java Hello
    set /a var+=1
    if %var% lss 10 goto b
    pause没有循环了吧
      

  16.   

    ....把循环改bat里面文件里面去了。
      

  17.   

    在API内找不到内存,如果内存够大,你可以尝试copy 27次
    int 最大2^31 - 1,hello\r\n正好8个字符2^3 31-3=28,可惜差一,所以只能27次。这个代码的重复是有限次的,你n=100w也无所谓。
        int n = 6;
        String hello = "hello\r\n";
        String str = hello;
        str = str.concat(str); // 1
        str = str.concat(str); // 2
        str = str.concat(str); // 3
        str = str.concat(str); // 4
        str = str.concat(str); // 5
        str = str.concat(str); // 6
        str = str.concat(str); // 7
        str = str.concat(str); // 8
        str = str.concat(str); // 9
        str = str.concat(str); // 10
        str = str.concat(str); // 11
        str = str.concat(str); // 12
        str = str.concat(str); // 13
        str = str.concat(str); // 14
        str = str.concat(str); // 15
        str = str.concat(str); // 16 再大的话内存恐怕不够了
        System.out.println(str.length());
        
        System.out.println(str.substring(0, hello.length() * n));
      

  18.   

    无语,这还转移了哈!放到bat中那也是循环啊!
      

  19.   

    翻遍无数API
    想过的方案包括:
    1 Formatter %ns + replace
    2 Thread.UncaughtExceptionHandler,用异常,可惜这家伙你再抛出异常就不会自己handle了
    3 char[n] System.arraycopy直到越界,由这个想到上面方案
    4 文件复制,没考虑周全
    5 管道,和文件道理差不多小绵羊的bat早想过了,类似无耻方案还包括:
    1 jni -> c goto
    2 Http重定向 将?x=5重定向到?x=4
      

  20.   

    好像只能通过多线程实现,其它的还没想到,lz v5,这个题目很经典
      

  21.   

    通过递归来..NEW 10个..THREAD的...
      

  22.   

    感觉我这可以。。int count = 0, max = 16;
    void print()
    {
        System.out.println("hello");
        ++count ;
        if(count < max)
            print();
    }
      

  23.   

    写个print hello的程序
    口中默数,100,99,98,97,96,95...5,4,3,2,1
    手中默点,数一下就双击一下程序,数一下就双击一下程序...
      

  24.   

    或者写个网页,挂在网上
    发广告让大家点,点一次出一个hello,点一次出一个hello
    直到达到指定数字
    这个方案俗称云计算...
      

  25.   

    其实答案在这里..哈哈..
    public static void main(String[] args) {     
                    System.out.println("n个 hello (n>=1) 。");
        }
      

  26.   


    洗澡时候想到的
    public class Test {
      public static void main(String[] args) {
        System.out.println(args[0]);
      }
    }
    java Test "hello hello hello"
      

  27.   

    #include<stdio.h>
    int printhello(int n)
    {static int m = 1;
    if(m++!=n)
            printf("hello\n");
    if(n==1)
    {
            printf("hello\n");
            return 1;
    }
    if(n<=0)
            return 0;
    printhello(n-1);
    return 1;
    }
    main()
    {
    printhello(5);
    }