Java program that prints 1000 times.

  • Java
  • Thread starter Hiche
  • Start date
  • #1
84
0
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:
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();
	}
}
 

Answers and Replies

  • #2
AlephZero
Science Advisor
Homework Helper
7,002
293
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.
 
  • #3
84
0
Code:
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.
 
  • #4
437
0
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.
 
  • #5
AlephZero
Science Advisor
Homework Helper
7,002
293
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.
 
  • #6
152
0
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:
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...
 

Related Threads on Java program that prints 1000 times.

  • Last Post
Replies
11
Views
86K
Replies
6
Views
1K
Replies
2
Views
6K
  • Last Post
Replies
7
Views
19K
  • Last Post
Replies
18
Views
6K
Replies
6
Views
3K
V
Replies
6
Views
7K
  • Last Post
Replies
9
Views
3K
Top