Java Java program that prints 1000 times.

  • Thread starter Thread starter Hiche
  • Start date Start date
  • Tags Tags
    Java Program
Click For Summary
The discussion centers around a Java programming assignment requiring students to print a statement 1,000 times without using loops, focusing solely on methods. Participants suggest various approaches, highlighting that creating a method with multiple print statements and calling it several times is one way to achieve the goal. However, they also introduce the concept of recursion as a viable alternative. One participant proposes using a recursive method that counts up to 1,000, which avoids traditional loop constructs. This method involves a base case to stop recursion once the count exceeds 1,000. The conversation emphasizes understanding both method calls and recursion as essential programming concepts, especially for beginners.
Hiche
Messages
82
Reaction score
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();
	}
}
 
Technology news on Phys.org
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.
 
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.
 
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.
 
Hiche said:
Like this?

You got it :smile:

fleem said:
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.
 
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...
 
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

Replies
8
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 0 ·
Replies
0
Views
632
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 14 ·
Replies
14
Views
6K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
13K
  • · Replies 5 ·
Replies
5
Views
2K