Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Java program that prints 1000 times.

  1. Oct 8, 2011 #1
    Okay.

    So, I'm taking up a beginning Java course in college and we have been assigned a rather simple (?) homework.

    We have to code a program that prints a statement a thousand times, but without the use of loops, only methods. I know I can create a method that contains a number of the statement and then call it several times from the main method, but is there an easier way? Our instructor asked for the simplest way. Is the one I mentioned the simplest way when we are limited to methods?

    I'm still new to programming. How did I know about loops? Read a few further chapters ahead of our course.

    Code (Text):
    public class MyProgram
    {
        public static void main(String[] args)
        {
            printThousandTimes();
            printThousandTimes();
                    // call some more and more..
           
        }
       
        public static void printStatement
        {
            System.out.println("statement");
        }

        public static void printThousandTimes
        {
            printStatement();
            printStatement();
            printStatement();
            printStatement();
            printStatement();
            printStatement();
            printStatement();
            printStatement();
            printStatement();
            printStatement();
        }
    }
     
  2. jcsd
  3. Oct 8, 2011 #2

    AlephZero

    User Avatar
    Science Advisor
    Homework Helper

    You don't need to write a method with 1000 identical print statements.

    What do you think happens if you write a method with say 5 print statements, and call it 5 times.

    Now think about the fact that 1000 = 10 x 10 x 10.
     
  4. Oct 8, 2011 #3
    Code (Text):
    public class MyProgram
    {
        public static void main(String[] args)
        {
            printThousandTimes();
            printThousandTimes();
            printThousandTimes();
            printThousandTimes();
            printThousandTimes();
            printThousandTimes();
            printThousandTimes();
            printThousandTimes();
            printThousandTimes();
            printThousandTimes();
        }
       
        public static void printStatement()
        {
            System.out.println("statement");
            System.out.println("statement");
            System.out.println("statement");
            System.out.println("statement");
            System.out.println("statement");
            System.out.println("statement");
            System.out.println("statement");
            System.out.println("statement");
            System.out.println("statement");
            System.out.println("statement");
        }

        public static void printThousandTimes()
        {
            printStatement();
            printStatement();
            printStatement();
            printStatement();
            printStatement();
            printStatement();
            printStatement();
            printStatement();
            printStatement();
            printStatement();
        }
    }
    Like this?

    And thank you, Aleph Zero. Bare with me if my answer is not true, if you may.
     
  5. Oct 8, 2011 #4
    I think limited recursion would qualify for not being a loop. Just pass a counter variable and don't recurse when that variable exceeds 1000.
     
  6. Oct 8, 2011 #5

    AlephZero

    User Avatar
    Science Advisor
    Homework Helper

    You got it :smile:

    Maybe so, but if the OP hasn't studied loops yet, he/she probably doesn't know about recursion either.
     
  7. Oct 12, 2011 #6
    I'm not entirely sure if it could count as a loop, but there is a really simple way to do this without using any "loop" keywords(for, do, while...)
    Code (Text):
    public class MyProgram
    {
        public static void main(String[] args) {
            rec(0);
        }
       
        public static void rec (int count) {
            System.out.println(count); //Or whatever else you wish really...
            if (count<1000) rec(count++);
        }
    }
    It's called recursion. As in a function that calls itself...
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Java program that prints 1000 times.
  1. Java programs (Replies: 9)

  2. Java Programming (Replies: 12)

Loading...