PDA

View Full Version : Java program that prints 1000 times.


Hiche
Oct8-11, 10:13 AM
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.

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();
}
}

AlephZero
Oct8-11, 11:22 AM
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.

Hiche
Oct8-11, 11:46 AM
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.

fleem
Oct8-11, 12:31 PM
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.

AlephZero
Oct8-11, 02:20 PM
Like this?


You got it :smile:

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.
Maybe so, but if the OP hasn't studied loops yet, he/she probably doesn't know about recursion either.

martix
Oct12-11, 06:42 PM
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...)
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...