Speed up development with full-stack environments for every branch.

Learn More

SDA doWhile as for [Java]

Forked from Basic Java Example.

5 Runs 18 Views 0 Copies

21.02.2017

Saved

Saved

xadi 4

xadi
published a month ago

    import java.io.*;
    
    public class HelloWorld {
    
        public static String catchOut(Runnable algo) {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            PrintStream ps = new PrintStream(baos);
            PrintStream old = System.out;
            System.setOut(ps);
            algo.run();
            System.out.flush();
            System.setOut(old);
            return baos.toString();
        }
      
      public static void main(String[] args) {
            final Runnable doWhileStrategy = new Runnable() {
                @Override
                public void run() {
                    int i = 0;
                    do {
                        System.out.println(i++);
                    } while(i<0);
                }
            };
            final Runnable forStrategy = new Runnable() {
                @Override
                public void run() {
                    for (int i = 0 /*...*/; i < 0; i++/*...*/) {
                        System.out.println(i++);
                    }
                }
            };
            assert catchOut(doWhileStrategy).equals(catchOut(forStrategy));
            System.out.println(catchOut(doWhileStrategy).equals(catchOut(forStrategy)));
      }
    }
     
    
    
    Please login/signup to get access to the terminal.

    Your session has timed out.

    Dismiss (the page may not function properly).